Skip to content

Commit b8be003

Browse files
committed
feat(cache): Add flush function from cache
1 parent 3662d1e commit b8be003

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

cache/cache.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ type Cache interface {
1212
GetInt64(ctx context.Context, key string) (int64, error)
1313
SetInt64(ctx context.Context, key string, value int64, ttl time.Duration) error
1414
Del(ctx context.Context, key string) error
15+
Flush(ctx context.Context) error
1516
}

contrib/cache/memory/local.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@ func (s *Cache) Del(ctx context.Context, Key string) error {
7777
s.local.Delete(Key)
7878
return nil
7979
}
80+
81+
// Flush deletes all items from cache
82+
func (s *Cache) Flush(ctx context.Context) error {
83+
s.local.Flush()
84+
return nil
85+
}

contrib/cache/memory/local_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,17 @@ func TestCache_Del(t *testing.T) {
4444
got, err := myCache.GetString(context.TODO(), key)
4545
assert.Equal(t, "", got)
4646
}
47+
48+
func TestCache_Flush(t *testing.T) {
49+
var key = "test"
50+
var val = "test"
51+
myCache := NewCache()
52+
err := myCache.SetString(context.TODO(), key, val, -1)
53+
assert.NoError(t, err)
54+
55+
err = myCache.Flush(context.TODO())
56+
assert.NoError(t, err)
57+
58+
got, err := myCache.GetString(context.TODO(), key)
59+
assert.Equal(t, "", got)
60+
}

contrib/cache/redis/redis.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,9 @@ func (r *Cache) SetInt64(ctx context.Context, key string, value int64, ttl time.
4949
func (r *Cache) Del(ctx context.Context, key string) error {
5050
return r.Client.Del(ctx, key).Err()
5151
}
52+
53+
// Flush delete all cache entries
54+
func (r *Cache) Flush(ctx context.Context) error {
55+
r.Client.FlushAll(ctx)
56+
return nil
57+
}

0 commit comments

Comments
 (0)