fix: use singleton pattern for PhoneRistrettoCache and InMemoryCache#884
Conversation
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>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
| Duplication | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
Greptile SummaryThis PR fixes a caching bug where
Confidence Score: 5/5Safe to merge — the change is a straightforward singleton fix that prevents redundant cache creation, and it mirrors a pattern already proven in the codebase. The change is small and surgical: two fields added to the struct, two nil-check guards added to existing methods, and one wrong error string corrected. The new code is a direct copy of the UserRistrettoCache pattern that has been in production. No logic paths are removed, and no new dependencies are introduced. No files require special attention; api/pkg/di/container.go is the only changed file and the modifications are contained to three well-isolated methods. Important Files Changed
Sequence DiagramsequenceDiagram
participant C as Caller
participant CT as Container
participant Cache as Cache Instance
Note over CT: First call
C->>CT: PhoneRistrettoCache()
CT->>CT: "phoneRistrettoCache == nil?"
CT->>Cache: ristretto.NewCache(...)
Cache-->>CT: ristrettoCache
CT->>CT: store container.phoneRistrettoCache
CT-->>C: return phoneRistrettoCache
Note over CT: Subsequent calls
C->>CT: PhoneRistrettoCache()
CT->>CT: "phoneRistrettoCache != nil"
CT-->>C: return existing phoneRistrettoCache
Note over CT: Same pattern for InMemoryCache
C->>CT: InMemoryCache()
CT->>CT: "inMemoryCache == nil?"
CT->>Cache: ttlCache.New(...) + NewMemoryCache(...)
Cache-->>CT: c
CT->>CT: store container.inMemoryCache
CT-->>C: return inMemoryCache
Reviews (1): Last reviewed commit: "fix: use singleton pattern for PhoneRist..." | Re-trigger Greptile |
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Summary
Apply the same singleton pattern already used by \UserRistrettoCache()\ to both \PhoneRistrettoCache()\ and \InMemoryCache()\ in the DI container.
Problem
Previously these methods created a new cache instance on every call, meaning each consumer got an isolated cache that could not be shared — defeating the purpose of caching.
Changes
Pattern
Follows the exact same nil-check singleton pattern already used by \UserRistrettoCache():
\\go
func (container *Container) PhoneRistrettoCache() *ristretto.Cache[string, *entities.Phone] {
if container.phoneRistrettoCache != nil {
return container.phoneRistrettoCache
}
// ... create and store
container.phoneRistrettoCache = ristrettoCache
return container.phoneRistrettoCache
}
\\