diff --git a/cache/local.go b/cache/local.go index 9f4d6b6..03c6199 100644 --- a/cache/local.go +++ b/cache/local.go @@ -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 diff --git a/cache/local_test.go b/cache/local_test.go index eefbb84..26fadc9 100644 --- a/cache/local_test.go +++ b/cache/local_test.go @@ -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() }