Skip to content

Commit 535f438

Browse files
authored
Add prefix config for zcache (#48)
* add prefix feature to zcache * add tests for prefix feature * run ci for all tests
1 parent ea01e11 commit 535f438

File tree

10 files changed

+124
-45
lines changed

10 files changed

+124
-45
lines changed

.github/workflows/checks.golang.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ jobs:
3232
make lint
3333
- name: Run tests
3434
run: |
35-
make test
35+
make test-all

Makefile.local.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test-all:
2+
make test
3+
PREFIX=prefix-test make test

pkg/zcache/combined_cache_test.go

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package zcache
33
import (
44
"context"
55
"github.com/stretchr/testify/suite"
6+
"os"
67
"testing"
78
"time"
89

@@ -26,25 +27,43 @@ func (suite *CombinedCacheTestSuite) SetupSuite() {
2627
suite.Require().NoError(err)
2728
suite.mr = mr
2829

29-
suite.cacheRemoteBrokenBestEffort, err = NewCombinedCache(&CombinedConfig{Local: &LocalConfig{
30-
EvictionInSeconds: 10,
31-
}, Remote: &RemoteConfig{
32-
Addr: "0.0.0.0",
33-
}, isRemoteBestEffort: true})
30+
prefix := os.Getenv("PREFIX")
31+
suite.cacheRemoteBrokenBestEffort, err = NewCombinedCache(
32+
&CombinedConfig{
33+
Local: &LocalConfig{
34+
EvictionInSeconds: 10,
35+
},
36+
Remote: &RemoteConfig{
37+
Addr: "0.0.0.0",
38+
},
39+
isRemoteBestEffort: true,
40+
globalPrefix: prefix,
41+
})
3442
suite.Nil(err)
3543

36-
suite.cacheOkNotBestEffort, err = NewCombinedCache(&CombinedConfig{Local: &LocalConfig{
37-
EvictionInSeconds: 10,
38-
}, Remote: &RemoteConfig{
39-
Addr: mr.Addr(),
40-
}, isRemoteBestEffort: false})
44+
suite.cacheOkNotBestEffort, err = NewCombinedCache(&CombinedConfig{
45+
Local: &LocalConfig{
46+
EvictionInSeconds: 10,
47+
},
48+
Remote: &RemoteConfig{
49+
Addr: mr.Addr(),
50+
},
51+
isRemoteBestEffort: false,
52+
globalPrefix: prefix,
53+
})
4154
suite.Nil(err)
4255

43-
suite.cacheRemoteBrokenNotBestEffort, err = NewCombinedCache(&CombinedConfig{Local: &LocalConfig{
44-
EvictionInSeconds: 10,
45-
}, Remote: &RemoteConfig{
46-
Addr: "0.0.0.0",
47-
}, isRemoteBestEffort: false})
56+
suite.cacheRemoteBrokenNotBestEffort, err = NewCombinedCache(
57+
&CombinedConfig{
58+
Local: &LocalConfig{
59+
EvictionInSeconds: 10,
60+
},
61+
Remote: &RemoteConfig{
62+
Addr: "0.0.0.0",
63+
},
64+
isRemoteBestEffort: false,
65+
globalPrefix: prefix,
66+
})
4867
suite.Nil(err)
4968
}
5069

pkg/zcache/config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ type RemoteConfig struct {
2020
PoolTimeout time.Duration
2121
IdleTimeout time.Duration
2222
IdleCheckFrequency time.Duration
23+
Prefix string
2324
}
2425

2526
type LocalConfig struct {
2627
EvictionInSeconds int
28+
Prefix string
2729
}
2830

2931
func (c *RemoteConfig) ToRedisConfig() *redis.Options {
@@ -51,6 +53,7 @@ func (c *LocalConfig) ToBigCacheConfig() bigcache.Config {
5153
type CombinedConfig struct {
5254
Local *LocalConfig
5355
Remote *RemoteConfig
54-
generalTtlSeconds int
56+
globalTtlSeconds int
57+
globalPrefix string
5558
isRemoteBestEffort bool
5659
}

pkg/zcache/local_cache.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,34 @@ type LocalCache interface {
1515

1616
type localCache struct {
1717
client *bigcache.BigCache
18+
prefix string
1819
metricsServer *metrics.TaskMetrics
1920
appName string
2021
}
2122

2223
func (c *localCache) Set(_ context.Context, key string, value interface{}, _ time.Duration) error {
24+
realKey := getKeyWithPrefix(c.prefix, key)
25+
2326
val, err := json.Marshal(value)
2427
if err != nil {
2528
return err
2629
}
27-
return c.client.Set(key, val)
30+
return c.client.Set(realKey, val)
2831
}
2932

3033
func (c *localCache) Get(_ context.Context, key string, data interface{}) error {
31-
val, err := c.client.Get(key)
34+
realKey := getKeyWithPrefix(c.prefix, key)
35+
36+
val, err := c.client.Get(realKey)
3237
if err != nil {
3338
return err
3439
}
3540
return json.Unmarshal(val, &data)
3641
}
3742

3843
func (c *localCache) Delete(_ context.Context, key string) error {
39-
return c.client.Delete(key)
44+
realKey := getKeyWithPrefix(c.prefix, key)
45+
return c.client.Delete(realKey)
4046
}
4147

4248
func (c *localCache) GetStats() ZCacheStats {

pkg/zcache/local_cache_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package zcache
22

33
import (
44
"context"
5-
"github.com/allegro/bigcache/v3"
65
"github.com/stretchr/testify/suite"
6+
"os"
77
"testing"
8-
"time"
98
)
109

1110
func TestLocalCacheTestSuite(t *testing.T) {
@@ -18,11 +17,14 @@ type LocalCacheTestSuite struct {
1817
}
1918

2019
func (suite *LocalCacheTestSuite) SetupSuite() {
21-
bigCacheConfig := bigcache.DefaultConfig(10 * time.Minute)
22-
client, err := bigcache.New(context.Background(), bigCacheConfig)
23-
suite.Require().NoError(err)
24-
25-
suite.cache = &localCache{client: client}
20+
prefix := os.Getenv("PREFIX")
21+
var err error
22+
config := LocalConfig{
23+
EvictionInSeconds: 100000,
24+
Prefix: prefix,
25+
}
26+
suite.cache, err = NewLocalCache(&config)
27+
suite.Nil(err)
2628
}
2729

2830
func (suite *LocalCacheTestSuite) TestSetAndGet() {

pkg/zcache/redis_cache.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,68 +30,83 @@ type RemoteCache interface {
3030

3131
type redisCache struct {
3232
client *redis.Client
33+
prefix string
3334
metricsServer *metrics.TaskMetrics
3435
appName string
3536
}
3637

3738
func (c *redisCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error {
39+
realKey := getKeyWithPrefix(c.prefix, key)
40+
3841
val, err := json.Marshal(value)
3942
if err != nil {
4043
return err
4144
}
42-
return c.client.Set(ctx, key, val, ttl).Err()
45+
return c.client.Set(ctx, realKey, val, ttl).Err()
4346
}
4447

4548
func (c *redisCache) Get(ctx context.Context, key string, data interface{}) error {
46-
val, err := c.client.Get(ctx, key).Result()
49+
realKey := getKeyWithPrefix(c.prefix, key)
50+
51+
val, err := c.client.Get(ctx, realKey).Result()
4752
if err != nil {
4853
return err
4954
}
5055
return json.Unmarshal([]byte(val), &data)
5156
}
5257

5358
func (c *redisCache) Delete(ctx context.Context, key string) error {
54-
return c.client.Del(ctx, key).Err()
59+
realKey := getKeyWithPrefix(c.prefix, key)
60+
return c.client.Del(ctx, realKey).Err()
5561
}
5662

5763
func (c *redisCache) Exists(ctx context.Context, keys ...string) (int64, error) {
58-
return c.client.Exists(ctx, keys...).Result()
64+
realKeys := getKeysWithPrefix(c.prefix, keys)
65+
return c.client.Exists(ctx, realKeys...).Result()
5966
}
6067

6168
func (c *redisCache) Incr(ctx context.Context, key string) (int64, error) {
62-
return c.client.Incr(ctx, key).Result()
69+
realKey := getKeyWithPrefix(c.prefix, key)
70+
return c.client.Incr(ctx, realKey).Result()
6371
}
6472

6573
func (c *redisCache) Decr(ctx context.Context, key string) (int64, error) {
66-
return c.client.Decr(ctx, key).Result()
74+
realKey := getKeyWithPrefix(c.prefix, key)
75+
return c.client.Decr(ctx, realKey).Result()
6776
}
6877

6978
func (c *redisCache) FlushAll(ctx context.Context) error {
7079
return c.client.FlushAll(ctx).Err()
7180
}
7281

7382
func (c *redisCache) LPush(ctx context.Context, key string, values ...interface{}) (int64, error) {
74-
return c.client.LPush(ctx, key, values...).Result()
83+
realKey := getKeyWithPrefix(c.prefix, key)
84+
return c.client.LPush(ctx, realKey, values...).Result()
7585
}
7686

7787
func (c *redisCache) RPush(ctx context.Context, key string, values ...interface{}) (int64, error) {
78-
return c.client.RPush(ctx, key, values...).Result()
88+
realKey := getKeyWithPrefix(c.prefix, key)
89+
return c.client.RPush(ctx, realKey, values...).Result()
7990
}
8091

8192
func (c *redisCache) SMembers(ctx context.Context, key string) ([]string, error) {
82-
return c.client.SMembers(ctx, key).Result()
93+
realKey := getKeyWithPrefix(c.prefix, key)
94+
return c.client.SMembers(ctx, realKey).Result()
8395
}
8496

8597
func (c *redisCache) SAdd(ctx context.Context, key string, members ...interface{}) (int64, error) {
86-
return c.client.SAdd(ctx, key, members...).Result()
98+
realKey := getKeyWithPrefix(c.prefix, key)
99+
return c.client.SAdd(ctx, realKey, members...).Result()
87100
}
88101

89102
func (c *redisCache) HSet(ctx context.Context, key string, values ...interface{}) (int64, error) {
90-
return c.client.HSet(ctx, key, values...).Result()
103+
realKey := getKeyWithPrefix(c.prefix, key)
104+
return c.client.HSet(ctx, realKey, values...).Result()
91105
}
92106

93107
func (c *redisCache) HGet(ctx context.Context, key, field string) (string, error) {
94-
return c.client.HGet(ctx, key, field).Result()
108+
realKey := getKeyWithPrefix(c.prefix, key)
109+
return c.client.HGet(ctx, realKey, field).Result()
95110
}
96111

97112
func (c *redisCache) GetStats() ZCacheStats {

pkg/zcache/redis_cache_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package zcache
33
import (
44
"context"
55
"github.com/stretchr/testify/suite"
6+
"os"
67
"testing"
78
"time"
89

910
"github.com/alicebob/miniredis/v2"
1011
)
1112

12-
func TestZCacheTestSuite(t *testing.T) {
13+
func TestRedisTestSuite(t *testing.T) {
1314
suite.Run(t, new(RedisCacheTestSuite))
1415
}
1516

@@ -24,8 +25,10 @@ func (suite *RedisCacheTestSuite) SetupSuite() {
2425
suite.Require().NoError(err)
2526
suite.mr = mr
2627

28+
prefix := os.Getenv("PREFIX")
2729
config := &RemoteConfig{
28-
Addr: mr.Addr(),
30+
Addr: mr.Addr(),
31+
Prefix: prefix,
2932
}
3033

3134
suite.cache, err = NewRemoteCache(config)

pkg/zcache/utils.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package zcache
2+
3+
import "fmt"
4+
5+
const KeySplitter = "/"
6+
7+
func getKeyWithPrefix(prefix, key string) string {
8+
if prefix != "" {
9+
return fmt.Sprintf("%s%s%s", prefix, KeySplitter, key)
10+
}
11+
return key
12+
}
13+
14+
func getKeysWithPrefix(prefix string, keys []string) []string {
15+
if prefix != "" {
16+
var newKeys []string
17+
for _, key := range keys {
18+
newKeys = append(newKeys, fmt.Sprintf("%s%s%s", prefix, KeySplitter, key))
19+
}
20+
return newKeys
21+
}
22+
23+
return keys
24+
}

pkg/zcache/zcache.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,29 @@ type ZCache interface {
2424
func NewLocalCache(config *LocalConfig) (LocalCache, error) {
2525
bigCacheConfig := config.ToBigCacheConfig()
2626
client, err := bigcache.New(context.Background(), bigCacheConfig)
27-
return &localCache{client: client}, err
27+
return &localCache{client: client, prefix: config.Prefix}, err
2828
}
2929

3030
func NewRemoteCache(config *RemoteConfig) (RemoteCache, error) {
3131
redisOptions := config.ToRedisConfig()
3232
client := redis.NewClient(redisOptions)
33-
return &redisCache{client: client}, nil
33+
return &redisCache{client: client, prefix: config.Prefix}, nil
3434
}
3535

3636
func NewCombinedCache(combinedConfig *CombinedConfig) (CombinedCache, error) {
3737
localCacheConfig := combinedConfig.Local
3838
remoteCacheConfig := combinedConfig.Remote
3939

40+
// Set global configs on remote cache config
41+
remoteCacheConfig.Prefix = combinedConfig.globalPrefix
4042
remoteClient, err := NewRemoteCache(remoteCacheConfig)
4143
if err != nil {
4244
return nil, err
4345
}
4446

45-
localCacheConfig.EvictionInSeconds = combinedConfig.generalTtlSeconds
47+
// Set global configs on local cache config
48+
localCacheConfig.EvictionInSeconds = combinedConfig.globalTtlSeconds
49+
localCacheConfig.Prefix = combinedConfig.globalPrefix
4650
localClient, err := NewLocalCache(localCacheConfig)
4751
if err != nil {
4852
return nil, err
@@ -52,6 +56,6 @@ func NewCombinedCache(combinedConfig *CombinedConfig) (CombinedCache, error) {
5256
remoteCache: remoteClient,
5357
localCache: localClient,
5458
isRemoteBestEffort: combinedConfig.isRemoteBestEffort,
55-
ttl: time.Duration(combinedConfig.generalTtlSeconds) * time.Second,
59+
ttl: time.Duration(combinedConfig.globalTtlSeconds) * time.Second,
5660
}, nil
5761
}

0 commit comments

Comments
 (0)