Skip to content

Commit

Permalink
feat(cached.go): modify Clear() function to accept optional UUIDs for…
Browse files Browse the repository at this point in the history
… selective cache clearing
  • Loading branch information
bounoable committed Sep 21, 2023
1 parent 4b51c72 commit 49ade70
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions aggregate/repository/cached.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,18 @@ func Cached[Aggregate aggregate.TypedAggregate](repo aggregate.TypedRepository[A
}
}

// Clear empties the cache of the CachedRepository. All aggregates currently
// held in memory are removed, and subsequent fetches will retrieve aggregates
// from the underlying TypedRepository. This operation is safe for concurrent
// use.
func (repo *CachedRepository[Aggregate]) Clear() {
// Clear removes the specified aggregates from the cache of a CachedRepository.
// If no UUIDs are provided, it clears all aggregates from the cache. This
// operation is safe for concurrent use.
func (repo *CachedRepository[Aggregate]) Clear(ids ...uuid.UUID) {
repo.mux.Lock()
defer repo.mux.Unlock()
if len(ids) > 0 {
for _, id := range ids {
delete(repo.cache, id)
}
return
}
maps.Clear(repo.cache)
}

Expand Down

0 comments on commit 49ade70

Please sign in to comment.