Skip to content

Commit c430b94

Browse files
committed
feat: breaking change! support custom redis cache key prefix
#11 Done
1 parent 0858efe commit c430b94

File tree

6 files changed

+34
-17
lines changed

6 files changed

+34
-17
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func main() {
2929

3030
cache, _ := cache.NewGorm2Cache(&config.CacheConfig{
3131
CacheLevel: config.CacheLevelAll,
32-
CacheStorage: storage.NewRedisWithClient(redisClient),
32+
CacheStorage: storage.NewRedis(&storage.RedisStoreConfig{Client: redisClient}),
3333
InvalidateWhenUpdate: true, // when you create/update/delete objects, invalidate cache
3434
CacheTTL: 5000, // 5000 ms
3535
CacheMaxItemCnt: 50, // if length of objects retrieved one single time

cache/cache.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ func (c *Gorm2Cache) AttachToDB(db *gorm.DB) {
7676
func (c *Gorm2Cache) Init() error {
7777
c.InstanceId = util.GenInstanceId()
7878

79-
prefix := util.GormCachePrefix + ":" + c.InstanceId
80-
8179
if c.cache != nil {
8280
c.cache = c.Config.CacheStorage
8381
} else {
@@ -94,7 +92,7 @@ func (c *Gorm2Cache) Init() error {
9492
TTL: c.Config.CacheTTL,
9593
Debug: c.Config.DebugMode,
9694
Logger: c.Logger,
97-
}, prefix)
95+
})
9896
if err != nil {
9997
c.Logger.CtxError(context.Background(), "[Init] cache init error: %v", err)
10098
return err

storage/gcache.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type Gcache struct {
2626
once sync.Once
2727
}
2828

29-
func (g *Gcache) Init(config *Config, prefix string) error {
29+
func (g *Gcache) Init(config *Config) error {
3030
g.once.Do(func() {
3131
if config.TTL != 0 {
3232
g.builder.Expiration(time.Duration(config.TTL) * time.Microsecond)

storage/interface.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Config struct {
1717
}
1818

1919
type DataStorage interface {
20-
Init(config *Config, prefix string) error
20+
Init(config *Config) error
2121
CleanCache(ctx context.Context) error
2222

2323
// read

storage/memory.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"github.com/asjdf/gorm-cache/util"
1111
)
1212

13+
var _ DataStorage = &Memory{}
14+
1315
type MemStoreConfig struct {
1416
MaxSize int64 // maximal items in primary cache
1517
}
@@ -18,8 +20,11 @@ var DefaultMemStoreConfig = &MemStoreConfig{
1820
MaxSize: 1000,
1921
}
2022

21-
func NewMem(config *MemStoreConfig) *Memory {
22-
return &Memory{config: config}
23+
func NewMem(config ...*MemStoreConfig) *Memory {
24+
if len(config) == 0 {
25+
config = append(config, DefaultMemStoreConfig)
26+
}
27+
return &Memory{config: config[0]}
2328
}
2429

2530
type Memory struct {
@@ -31,7 +36,7 @@ type Memory struct {
3136
once sync.Once
3237
}
3338

34-
func (m *Memory) Init(conf *Config, prefix string) error {
39+
func (m *Memory) Init(conf *Config) error {
3540
m.once.Do(func() {
3641
c := ccache.New(ccache.Configure[string]().MaxSize(m.config.MaxSize))
3742
m.cache = c

storage/redis.go

+22-8
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,29 @@ import (
1111

1212
var _ DataStorage = &Redis{}
1313

14-
func NewRedisWithClient(client *redis.Client) *Redis {
15-
return &Redis{
16-
client: client,
17-
}
14+
type RedisStoreConfig struct {
15+
KeyPrefix string // key prefix will be random if not set
16+
17+
Client *redis.Client // if Client is not nil, Options will be ignored
18+
Options *redis.Options
1819
}
1920

20-
func NewRedisWithOptions(options *redis.Options) *Redis {
21-
return NewRedisWithClient(redis.NewClient(options))
21+
func NewRedis(config ...*RedisStoreConfig) *Redis {
22+
if len(config) == 0 {
23+
panic("redis config is required")
24+
}
25+
if config[0].KeyPrefix == "" {
26+
config[0].KeyPrefix = util.GormCachePrefix + ":" + util.GenInstanceId()
27+
}
28+
r := &Redis{
29+
keyPrefix: config[0].KeyPrefix,
30+
}
31+
if config[0].Client != nil {
32+
r.client = config[0].Client
33+
return r
34+
}
35+
r.client = redis.NewClient(config[0].Options)
36+
return r
2237
}
2338

2439
type Redis struct {
@@ -33,13 +48,12 @@ type Redis struct {
3348
once sync.Once
3449
}
3550

36-
func (r *Redis) Init(conf *Config, prefix string) error {
51+
func (r *Redis) Init(conf *Config) error {
3752
var err error
3853
r.once.Do(func() {
3954
r.ttl = conf.TTL
4055
r.logger = conf.Logger
4156
r.logger.SetIsDebug(conf.Debug)
42-
r.keyPrefix = prefix
4357
err = r.initScripts()
4458
})
4559
return err

0 commit comments

Comments
 (0)