Skip to content

Commit 896381d

Browse files
committed
添加Keys 匹配key
1 parent 2a65723 commit 896381d

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

cache/cache.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func SetGlobal(c Cache) {
2424

2525
// Cache provides the interface for cache implementations.
2626
type Cache interface {
27+
Keys(pattern string) []string
2728
// SetTTL set default ttl
2829
SetDefaultTTL(ttl time.Duration)
2930
// set cached value with key and expire time.

cache/memory/gocache.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ func (c *Cache) Get(key string, val interface{}) error {
6666
}
6767

6868
return nil
69-
7069
}
7170

7271
// Delete 删除对象
@@ -104,6 +103,18 @@ func (c *Cache) Close() error {
104103
return nil
105104
}
106105

106+
// Keys todo
107+
func (c *Cache) Keys(pattern string) []string {
108+
ks := []string{}
109+
for _, k := range c.gc.Keys(true) {
110+
if v, ok := k.(string); ok {
111+
ks = append(ks, v)
112+
}
113+
}
114+
115+
return ks
116+
}
117+
107118
// WithContext 携带上下文
108119
func (c *Cache) WithContext(ctx context.Context) cache.Cache {
109120
clone := *c

cache/memory/gocache_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func TestMemoryAdapterSuit(t *testing.T) {
3434
t.Run("PutOK", testPutOK(suit))
3535
t.Run("ExistOK", testExistOK(suit))
3636
t.Run("ExistNotOK", testExistNotOK(suit))
37+
t.Run("KeysOK", testKeysOK(suit))
3738
t.Run("DelOK", testDelOK(suit))
3839
}
3940

@@ -74,6 +75,15 @@ func testExistNotOK(a *adapterSuit) func(t *testing.T) {
7475
}
7576
}
7677

78+
func testKeysOK(a *adapterSuit) func(t *testing.T) {
79+
return func(t *testing.T) {
80+
should := require.New(t)
81+
82+
ks := a.adapter.Keys("*")
83+
should.Equal([]string{a.testKey}, ks)
84+
}
85+
}
86+
7787
func testDelOK(a *adapterSuit) func(t *testing.T) {
7888
return func(t *testing.T) {
7989
should := require.New(t)

cache/redis/redis.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ func (c *Cache) ClearAll() error {
101101
return c.client.FlushDB().Err()
102102
}
103103

104+
// Keys todo
105+
func (c *Cache) Keys(pattern string) []string {
106+
return c.client.Keys(pattern).Val()
107+
}
108+
104109
// Decr todo
105110
func (c *Cache) Decr(key string) error {
106111
return c.client.Decr(key).Err()

cache/redis/redis_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func TestRedisAdapterSuit(t *testing.T) {
3636
t.Run("ExistNotOK", testExistNotOK(suit))
3737
t.Run("GetOK", testGetOK(suit))
3838
t.Run("GetFailed", testGetFailed(suit))
39+
t.Run("KeysOK", testKeysOK(suit))
3940
t.Run("DelOK", testDelOK(suit))
4041
}
4142

@@ -94,3 +95,12 @@ func testDelOK(a *adapterSuit) func(t *testing.T) {
9495
should.NoError(err)
9596
}
9697
}
98+
99+
func testKeysOK(a *adapterSuit) func(t *testing.T) {
100+
return func(t *testing.T) {
101+
should := require.New(t)
102+
103+
ks := a.adapter.Keys("testkey*")
104+
should.Equal([]string{a.testKey}, ks)
105+
}
106+
}

0 commit comments

Comments
 (0)