Skip to content

Commit

Permalink
Add cache.Local.Clear()
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Jan 24, 2024
1 parent dc46b70 commit ab20b9a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cache/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func (c *Local[K, V]) Get(ctx context.Context, key K) (V, error) {
return item.Value(), nil
}

// Clear removes all items from the cache.
func (c *Local[K, V]) Clear() {
c.cache.DeleteAll()
}

func (c *Local[K, V]) fetchAndSetSynced(ctx context.Context, key K) (*ttlcache.Item[K, V], error) {
ii, err, _ := c.fetchSync.Do(string(key), func() (any, error) {
// there's always a chance a different thread completed a fetch before we got here
Expand Down
4 changes: 4 additions & 0 deletions cache/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,9 @@ func TestLocal(t *testing.T) {
assert.Equal(t, map[string]int{"x": 2, "y": 1, "z": 1, "fast": 1, "slow": 1, "error": 1}, fetchCounts)
assert.Equal(t, 1, cache.Len())

cache.Clear()

assert.Equal(t, 0, cache.Len())

cache.Stop()
}

0 comments on commit ab20b9a

Please sign in to comment.