Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

Reduce TTL for cache

Acho Arnolde58f6e22a8e6
diff --git a/api/pkg/di/container.go b/api/pkg/di/container.go
index 9594c791..07bce2c3 100644
--- a/api/pkg/di/container.go
+++ b/api/pkg/di/container.go
@@ -722,6 +722,7 @@ func (container *Container) PhoneRepository() (repository repositories.PhoneRepo
container.Logger(),
container.Tracer(),
container.DB(),
+ container.PhoneRistrettoCache(),
)
}
@@ -1525,6 +1526,20 @@ func (container *Container) UserRepository() repositories.UserRepository {
)
}
+// PhoneRistrettoCache creates an in-memory *ristretto.Cache[string, *entities.Phone]
+func (container *Container) PhoneRistrettoCache() (cache *ristretto.Cache[string, *entities.Phone]) {
+ container.logger.Debug(fmt.Sprintf("creating %T", cache))
+ ristrettoCache, err := ristretto.NewCache[string, *entities.Phone](&ristretto.Config[string, *entities.Phone]{
+ MaxCost: 5000,
+ NumCounters: 5000 * 10,
+ BufferItems: 64,
+ })
+ if err != nil {
+ container.logger.Fatal(stacktrace.Propagate(err, "cannot create user ristretto cache"))
+ }
+ return ristrettoCache
+}
+
// UserRistrettoCache creates an in-memory *ristretto.Cache[string, entities.AuthContext]
func (container *Container) UserRistrettoCache() (cache *ristretto.Cache[string, entities.AuthContext]) {
container.logger.Debug(fmt.Sprintf("creating %T", cache))
diff --git a/api/pkg/repositories/gorm_phone_repository.go b/api/pkg/repositories/gorm_phone_repository.go
index b45f7cff..40c29d30 100644
--- a/api/pkg/repositories/gorm_phone_repository.go
+++ b/api/pkg/repositories/gorm_phone_repository.go
@@ -4,9 +4,11 @@ import (
"context"
"errors"
"fmt"
+ "time"
"github.com/NdoleStudio/httpsms/pkg/entities"
"github.com/NdoleStudio/httpsms/pkg/telemetry"
+ "github.com/dgraph-io/ristretto/v2"
"github.com/google/uuid"
"github.com/palantir/stacktrace"
"gorm.io/gorm"
@@ -16,6 +18,7 @@ import (
type gormPhoneRepository struct {
logger telemetry.Logger
tracer telemetry.Tracer
+ cache *ristretto.Cache[string, *entities.Phone]
db *gorm.DB
}
@@ -24,11 +27,13 @@ func NewGormPhoneRepository(
logger telemetry.Logger,
tracer telemetry.Tracer,
db *gorm.DB,
+ cache *ristretto.Cache[string, *entities.Phone],
) PhoneRepository {
return &gormPhoneRepository{
logger: logger.WithService(fmt.Sprintf("%T", &gormPhoneRepository{})),
tracer: tracer,
db: db,
+ cache: cache,
}
}
@@ -41,6 +46,7 @@ func (repository *gormPhoneRepository) DeleteAllForUser(ctx context.Context, use
return repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
+ repository.cache.Clear()
return nil
}
@@ -81,6 +87,7 @@ func (repository *gormPhoneRepository) Delete(ctx context.Context, userID entiti
return repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
+ repository.cache.Clear()
return nil
}
@@ -106,14 +113,20 @@ func (repository *gormPhoneRepository) Save(ctx context.Context, phone *entities
return repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
+ repository.cache.Del(repository.getCacheKey(phone.UserID, phone.PhoneNumber))
return nil
}
// Load a phone based on entities.UserID and phoneNumber
func (repository *gormPhoneRepository) Load(ctx context.Context, userID entities.UserID, phoneNumber string) (*entities.Phone, error) {
- ctx, span := repository.tracer.Start(ctx)
+ ctx, span, ctxLogger := repository.tracer.StartWithLogger(ctx, repository.logger)
defer span.End()
+ if phone, found := repository.cache.Get(repository.getCacheKey(userID, phoneNumber)); found {
+ ctxLogger.Info(fmt.Sprintf("cache hit for [%T] with ID [%s]", phone, userID))
+ return phone, nil
+ }
+
phone := new(entities.Phone)
err := repository.db.WithContext(ctx).Where("user_id = ?", userID).Where("phone_number = ?", phoneNumber).First(phone).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -126,6 +139,11 @@ func (repository *gormPhoneRepository) Load(ctx context.Context, userID entities
return nil, repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
+ if result := repository.cache.SetWithTTL(repository.getCacheKey(userID, phoneNumber), phone, 1, 30*time.Minute); !result {
+ msg := fmt.Sprintf("cannot cache [%T] with ID [%s] and result [%t]", phone, phone.ID, result)
+ ctxLogger.Error(repository.tracer.WrapErrorSpan(span, stacktrace.NewError(msg)))
+ }
+
return phone, nil
}
@@ -147,3 +165,7 @@ func (repository *gormPhoneRepository) Index(ctx context.Context, userID entitie
return phones, nil
}
+
+func (repository *gormPhoneRepository) getCacheKey(userID entities.UserID, phoneNumber string) string {
+ return fmt.Sprintf("user:%s:phone:%s", userID, phoneNumber)
+}