From 8c74bba94d9fe43398b94956202e2c3aaa5dcbde Mon Sep 17 00:00:00 2001 From: Acho Arnold Date: Wed, 13 May 2026 23:35:52 +0300 Subject: [PATCH 1/2] fix: use singleton pattern for PhoneRistrettoCache and InMemoryCache Apply the same singleton pattern already used by UserRistrettoCache to both PhoneRistrettoCache and InMemoryCache in the DI container. Previously these methods created a new cache instance on every call, meaning each consumer got an isolated cache that could not be shared. Changes: - Add phoneRistrettoCache and inMemoryCache fields to Container struct - Return cached instance on subsequent calls instead of creating new ones - Fix error message in PhoneRistrettoCache (was 'user ristretto cache') Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- api/pkg/di/container.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/api/pkg/di/container.go b/api/pkg/di/container.go index de180a0a..327639ac 100644 --- a/api/pkg/di/container.go +++ b/api/pkg/di/container.go @@ -87,6 +87,8 @@ type Container struct { logger telemetry.Logger attachmentRepository repositories.AttachmentRepository userRistrettoCache *ristretto.Cache[string, entities.AuthContext] + phoneRistrettoCache *ristretto.Cache[string, *entities.Phone] + inMemoryCache cache.Cache } // NewLiteContainer creates a Container without any routes or listeners @@ -413,9 +415,13 @@ func (container *Container) FirebaseApp() (app *firebase.App) { // InMemoryCache creates a new instance of the in memory cache.Cache func (container *Container) InMemoryCache() cache.Cache { + if container.inMemoryCache != nil { + return container.inMemoryCache + } container.logger.Debug("creating an in memory cache") c := ttlCache.New(time.Hour, time.Hour*2) - return cache.NewMemoryCache(container.Tracer(), c) + container.inMemoryCache = cache.NewMemoryCache(container.Tracer(), c) + return container.inMemoryCache } // Cache creates a new instance of cache.Cache @@ -1717,17 +1723,21 @@ 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)) +func (container *Container) PhoneRistrettoCache() *ristretto.Cache[string, *entities.Phone] { + if container.phoneRistrettoCache != nil { + return container.phoneRistrettoCache + } + container.logger.Debug(fmt.Sprintf("creating %T", container.phoneRistrettoCache)) 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")) + container.logger.Fatal(stacktrace.Propagate(err, "cannot create phone ristretto cache")) } - return ristrettoCache + container.phoneRistrettoCache = ristrettoCache + return container.phoneRistrettoCache } // UserRistrettoCache creates an in-memory *ristretto.Cache[string, entities.AuthContext] From 1eafad795203f3f18a46461121dd93734968cb44 Mon Sep 17 00:00:00 2001 From: Acho Arnold Date: Wed, 13 May 2026 23:40:16 +0300 Subject: [PATCH 2/2] Update api/pkg/di/container.go Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> --- api/pkg/di/container.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/pkg/di/container.go b/api/pkg/di/container.go index 327639ac..39fba5fc 100644 --- a/api/pkg/di/container.go +++ b/api/pkg/di/container.go @@ -413,7 +413,7 @@ func (container *Container) FirebaseApp() (app *firebase.App) { return app } -// InMemoryCache creates a new instance of the in memory cache.Cache +// InMemoryCache returns the shared in-memory cache.Cache, creating it on the first call. func (container *Container) InMemoryCache() cache.Cache { if container.inMemoryCache != nil { return container.inMemoryCache