Skip to content

Commit 303fe45

Browse files
committed
cache list key
1 parent 896381d commit 303fe45

File tree

5 files changed

+54
-11
lines changed

5 files changed

+54
-11
lines changed

cache/cache.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package cache
33
import (
44
"context"
55
"time"
6+
7+
"github.com/infraboard/mcube/http/request"
68
)
79

810
var (
@@ -24,7 +26,7 @@ func SetGlobal(c Cache) {
2426

2527
// Cache provides the interface for cache implementations.
2628
type Cache interface {
27-
Keys(pattern string) []string
29+
ListKey(*ListKeyRequest) (*ListKeyResponse, error)
2830
// SetTTL set default ttl
2931
SetDefaultTTL(ttl time.Duration)
3032
// set cached value with key and expire time.
@@ -48,3 +50,36 @@ type Cache interface {
4850
// 携带上下文
4951
WithContext(ctx context.Context) Cache
5052
}
53+
54+
// NewListKeyRequest todo
55+
func NewListKeyRequest(pattern string, ps uint, pn uint) *ListKeyRequest {
56+
return &ListKeyRequest{
57+
pattern: pattern,
58+
PageRequest: request.NewPageRequest(ps, pn),
59+
}
60+
}
61+
62+
// ListKeyRequest todo
63+
type ListKeyRequest struct {
64+
pattern string
65+
*request.PageRequest
66+
}
67+
68+
// Pattern tood
69+
func (req *ListKeyRequest) Pattern() string {
70+
return req.pattern
71+
}
72+
73+
// NewListKeyResponse todo
74+
func NewListKeyResponse(keys []string, total uint64) *ListKeyResponse {
75+
return &ListKeyResponse{
76+
Keys: keys,
77+
Total: total,
78+
}
79+
}
80+
81+
// ListKeyResponse todo
82+
type ListKeyResponse struct {
83+
Keys []string
84+
Total uint64
85+
}

cache/memory/gocache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,16 @@ func (c *Cache) Close() error {
103103
return nil
104104
}
105105

106-
// Keys todo
107-
func (c *Cache) Keys(pattern string) []string {
106+
// ListKey todo
107+
func (c *Cache) ListKey(req *cache.ListKeyRequest) (*cache.ListKeyResponse, error) {
108108
ks := []string{}
109109
for _, k := range c.gc.Keys(true) {
110110
if v, ok := k.(string); ok {
111111
ks = append(ks, v)
112112
}
113113
}
114114

115-
return ks
115+
return cache.NewListKeyResponse(ks, uint64(len(ks))), nil
116116
}
117117

118118
// WithContext 携带上下文

cache/memory/gocache_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ func testKeysOK(a *adapterSuit) func(t *testing.T) {
7979
return func(t *testing.T) {
8080
should := require.New(t)
8181

82-
ks := a.adapter.Keys("*")
83-
should.Equal([]string{a.testKey}, ks)
82+
ks, err := a.adapter.ListKey(cache.NewListKeyRequest("*", 10, 1))
83+
should.NoError(err)
84+
should.Equal([]string{a.testKey}, ks.Keys)
8485
}
8586
}
8687

cache/redis/redis.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package redis
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"time"
78

89
"github.com/go-redis/redis"
@@ -101,9 +102,14 @@ func (c *Cache) ClearAll() error {
101102
return c.client.FlushDB().Err()
102103
}
103104

104-
// Keys todo
105-
func (c *Cache) Keys(pattern string) []string {
106-
return c.client.Keys(pattern).Val()
105+
// ListKey todo
106+
func (c *Cache) ListKey(req *cache.ListKeyRequest) (*cache.ListKeyResponse, error) {
107+
fmt.Println(uint64(req.Offset()), req.Pattern(), int64(req.PageSize))
108+
ks, total, err := c.client.Scan(uint64(req.Offset()), req.Pattern(), int64(req.PageSize)).Result()
109+
if err != nil {
110+
return nil, err
111+
}
112+
return cache.NewListKeyResponse(ks, total), nil
107113
}
108114

109115
// Decr todo

cache/redis/redis_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ func testKeysOK(a *adapterSuit) func(t *testing.T) {
100100
return func(t *testing.T) {
101101
should := require.New(t)
102102

103-
ks := a.adapter.Keys("testkey*")
104-
should.Equal([]string{a.testKey}, ks)
103+
ks, err := a.adapter.ListKey(cache.NewListKeyRequest("testkey*", 3, 1))
104+
should.NoError(err)
105+
should.Equal([]string{a.testKey}, ks.Keys)
105106
}
106107
}

0 commit comments

Comments
 (0)