Skip to content

Commit 5e0f801

Browse files
AchoArnoldCopilot
andcommitted
fix: make PhoneRistrettoCache a singleton to prevent stale cache entries
Same bug pattern as UserRistrettoCache — PhoneRistrettoCache() created a new ristretto cache on every call. PhoneRepository is used by PhoneService, PhoneAPIKeyService, and NotificationService, each getting a separate cache. Cache invalidations (Clear/Del) in one service had no effect on the others, leading to stale phone data. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent a3889cd commit 5e0f801

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

api/pkg/di/container.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ type Container struct {
8787
logger telemetry.Logger
8888
attachmentRepository repositories.AttachmentRepository
8989
userRistrettoCache *ristretto.Cache[string, entities.AuthContext]
90+
phoneRistrettoCache *ristretto.Cache[string, *entities.Phone]
9091
}
9192

9293
// NewLiteContainer creates a Container without any routes or listeners
@@ -1717,8 +1718,11 @@ func (container *Container) UserRepository() repositories.UserRepository {
17171718
}
17181719

17191720
// PhoneRistrettoCache creates an in-memory *ristretto.Cache[string, *entities.Phone]
1720-
func (container *Container) PhoneRistrettoCache() (cache *ristretto.Cache[string, *entities.Phone]) {
1721-
container.logger.Debug(fmt.Sprintf("creating %T", cache))
1721+
func (container *Container) PhoneRistrettoCache() *ristretto.Cache[string, *entities.Phone] {
1722+
if container.phoneRistrettoCache != nil {
1723+
return container.phoneRistrettoCache
1724+
}
1725+
container.logger.Debug(fmt.Sprintf("creating %T", container.phoneRistrettoCache))
17221726
ristrettoCache, err := ristretto.NewCache[string, *entities.Phone](&ristretto.Config[string, *entities.Phone]{
17231727
MaxCost: 5000,
17241728
NumCounters: 5000 * 10,
@@ -1727,6 +1731,7 @@ func (container *Container) PhoneRistrettoCache() (cache *ristretto.Cache[string
17271731
if err != nil {
17281732
container.logger.Fatal(stacktrace.Propagate(err, "cannot create user ristretto cache"))
17291733
}
1734+
container.phoneRistrettoCache = ristrettoCache
17301735
return ristrettoCache
17311736
}
17321737

0 commit comments

Comments
 (0)