Skip to content
This repository was archived by the owner on May 31, 2025. It is now read-only.

Commit 29a9277

Browse files
committed
feat(cache): add GetPrefixKeysCtx
feat(cache): add GetPrefixKeysCtx
1 parent 873fe67 commit 29a9277

File tree

6 files changed

+107
-0
lines changed

6 files changed

+107
-0
lines changed

cache/cache.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ type Cache interface {
1111

1212
// SetNoExpireCtx Because zero cache set ctx has default expire, so jzero add this method
1313
SetNoExpireCtx(ctx context.Context, key string, val any) error
14+
15+
// GetPrefixKeysCtx get prefix key, give prefix key return all matched key
16+
GetPrefixKeysCtx(ctx context.Context, prefix string) ([]string, error)
1417
}

cache/redis.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ func (c redisNode) SetNoExpireCtx(ctx context.Context, key string, val any) erro
2323
return c.rds.SetCtx(ctx, key, string(data))
2424
}
2525

26+
func (c redisNode) GetPrefixKeysCtx(ctx context.Context, keyPrefix string) ([]string, error) {
27+
var (
28+
cursor uint64
29+
allKeys []string
30+
err error
31+
)
32+
33+
for {
34+
var keys []string
35+
keys, cursor, err = c.rds.ScanCtx(ctx, cursor, keyPrefix+"*", 100)
36+
if err != nil {
37+
return nil, err
38+
}
39+
allKeys = append(allKeys, keys...)
40+
if cursor == 0 {
41+
break
42+
}
43+
}
44+
return allKeys, nil
45+
}
46+
2647
func (c redisNode) Del(keys ...string) error {
2748
return c.node.Del(keys...)
2849
}

cache/redis_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cache
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/alicebob/miniredis/v2"
8+
"github.com/pkg/errors"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/zeromicro/go-zero/core/stores/redis"
11+
)
12+
13+
func TestRedisNode(t *testing.T) {
14+
r, err := miniredis.Run()
15+
assert.NoError(t, err)
16+
defer r.Close()
17+
18+
cache := NewRedisNode(redis.New(r.Addr()), errors.New("not found"))
19+
20+
err = cache.SetCtx(context.Background(), "JWT_ADMIN_AUTH:1:abc", "abc")
21+
assert.NoError(t, err)
22+
23+
err = cache.SetCtx(context.Background(), "JWT_ADMIN_AUTH:1:def", "def")
24+
assert.NoError(t, err)
25+
26+
err = cache.SetCtx(context.Background(), "JWT_ADMIN_AUTH:1:ghi", "ghi")
27+
assert.NoError(t, err)
28+
29+
keys, err := cache.GetPrefixKeysCtx(context.Background(), "JWT_ADMIN_AUTH:1:")
30+
assert.NoError(t, err)
31+
32+
assert.Equal(t, []string{"JWT_ADMIN_AUTH:1:abc", "JWT_ADMIN_AUTH:1:def", "JWT_ADMIN_AUTH:1:ghi"}, keys)
33+
}

cache/sync_map.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ type (
2222
}
2323
)
2424

25+
func (sm *syncMap) GetPrefixKeysCtx(ctx context.Context, prefix string) ([]string, error) {
26+
var allKeys []string
27+
28+
sm.storage.Range(func(key, value any) bool {
29+
keyStr, ok := key.(string)
30+
if !ok {
31+
return true
32+
}
33+
34+
if len(keyStr) >= len(prefix) && keyStr[:len(prefix)] == prefix {
35+
allKeys = append(allKeys, keyStr)
36+
}
37+
return true
38+
})
39+
40+
return allKeys, nil
41+
}
42+
2543
func (sm *syncMap) SetNoExpireCtx(ctx context.Context, key string, val any) error {
2644
return sm.SetCtx(ctx, key, val)
2745
}

cache/sync_map_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cache
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/pkg/errors"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestSyncMap(t *testing.T) {
12+
t.Run("TestSyncMap", func(t *testing.T) {
13+
cache := NewSyncMap(errors.New("not found"))
14+
15+
err := cache.SetCtx(context.Background(), "JWT_ADMIN_AUTH:1:abc", "abc")
16+
assert.NoError(t, err)
17+
err = cache.SetCtx(context.Background(), "JWT_ADMIN_AUTH:1:def", "def")
18+
assert.NoError(t, err)
19+
err = cache.SetCtx(context.Background(), "JWT_ADMIN_AUTH:1:ghi", "ghi")
20+
assert.NoError(t, err)
21+
22+
keys, err := cache.GetPrefixKeysCtx(context.Background(), "JWT_ADMIN_AUTH:1:")
23+
assert.NoError(t, err)
24+
assert.Equal(t, []string{"JWT_ADMIN_AUTH:1:abc", "JWT_ADMIN_AUTH:1:def", "JWT_ADMIN_AUTH:1:ghi"}, keys)
25+
})
26+
}

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@ go 1.22.3
55
require (
66
github.com/Masterminds/sprig/v3 v3.2.3
77
github.com/a8m/envsubst v1.4.2
8+
github.com/alicebob/miniredis/v2 v2.33.0
89
github.com/eddieowens/opts v0.1.0
910
github.com/fsnotify/fsnotify v1.8.0
1011
github.com/guregu/null/v5 v5.0.0
1112
github.com/huandu/go-assert v1.1.6
1213
github.com/huandu/go-sqlbuilder v1.28.0
1314
github.com/pkg/errors v0.9.1
1415
github.com/spf13/cast v1.5.1
16+
github.com/stretchr/testify v1.9.0
1517
github.com/zeromicro/go-zero v1.7.4
1618
)
1719

1820
require (
1921
filippo.io/edwards25519 v1.1.0 // indirect
2022
github.com/Masterminds/goutils v1.1.1 // indirect
2123
github.com/Masterminds/semver/v3 v3.2.0 // indirect
24+
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302 // indirect
2225
github.com/beorn7/perks v1.0.1 // indirect
2326
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
2427
github.com/cespare/xxhash/v2 v2.3.0 // indirect
@@ -46,13 +49,15 @@ require (
4649
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
4750
github.com/openzipkin/zipkin-go v0.4.3 // indirect
4851
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
52+
github.com/pmezard/go-difflib v1.0.0 // indirect
4953
github.com/prometheus/client_golang v1.20.5 // indirect
5054
github.com/prometheus/client_model v0.6.1 // indirect
5155
github.com/prometheus/common v0.55.0 // indirect
5256
github.com/prometheus/procfs v0.15.1 // indirect
5357
github.com/redis/go-redis/v9 v9.7.0 // indirect
5458
github.com/shopspring/decimal v1.2.0 // indirect
5559
github.com/spaolacci/murmur3 v1.1.0 // indirect
60+
github.com/yuin/gopher-lua v1.1.1 // indirect
5661
go.etcd.io/etcd/api/v3 v3.5.15 // indirect
5762
go.etcd.io/etcd/client/pkg/v3 v3.5.15 // indirect
5863
go.etcd.io/etcd/client/v3 v3.5.15 // indirect
@@ -80,4 +85,5 @@ require (
8085
google.golang.org/grpc v1.65.0 // indirect
8186
google.golang.org/protobuf v1.35.2 // indirect
8287
gopkg.in/yaml.v2 v2.4.0 // indirect
88+
gopkg.in/yaml.v3 v3.0.1 // indirect
8389
)

0 commit comments

Comments
 (0)