Skip to content

Commit

Permalink
feat: breaking change! support custom redis cache key prefix
Browse files Browse the repository at this point in the history
#11 Done
  • Loading branch information
asjdf committed Mar 8, 2023
1 parent 0858efe commit c430b94
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {

cache, _ := cache.NewGorm2Cache(&config.CacheConfig{
CacheLevel: config.CacheLevelAll,
CacheStorage: storage.NewRedisWithClient(redisClient),
CacheStorage: storage.NewRedis(&storage.RedisStoreConfig{Client: redisClient}),
InvalidateWhenUpdate: true, // when you create/update/delete objects, invalidate cache
CacheTTL: 5000, // 5000 ms
CacheMaxItemCnt: 50, // if length of objects retrieved one single time
Expand Down
4 changes: 1 addition & 3 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ func (c *Gorm2Cache) AttachToDB(db *gorm.DB) {
func (c *Gorm2Cache) Init() error {
c.InstanceId = util.GenInstanceId()

prefix := util.GormCachePrefix + ":" + c.InstanceId

if c.cache != nil {
c.cache = c.Config.CacheStorage
} else {
Expand All @@ -94,7 +92,7 @@ func (c *Gorm2Cache) Init() error {
TTL: c.Config.CacheTTL,
Debug: c.Config.DebugMode,
Logger: c.Logger,
}, prefix)
})
if err != nil {
c.Logger.CtxError(context.Background(), "[Init] cache init error: %v", err)
return err
Expand Down
2 changes: 1 addition & 1 deletion storage/gcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Gcache struct {
once sync.Once
}

func (g *Gcache) Init(config *Config, prefix string) error {
func (g *Gcache) Init(config *Config) error {
g.once.Do(func() {
if config.TTL != 0 {
g.builder.Expiration(time.Duration(config.TTL) * time.Microsecond)
Expand Down
2 changes: 1 addition & 1 deletion storage/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Config struct {
}

type DataStorage interface {
Init(config *Config, prefix string) error
Init(config *Config) error
CleanCache(ctx context.Context) error

// read
Expand Down
11 changes: 8 additions & 3 deletions storage/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/asjdf/gorm-cache/util"
)

var _ DataStorage = &Memory{}

type MemStoreConfig struct {
MaxSize int64 // maximal items in primary cache
}
Expand All @@ -18,8 +20,11 @@ var DefaultMemStoreConfig = &MemStoreConfig{
MaxSize: 1000,
}

func NewMem(config *MemStoreConfig) *Memory {
return &Memory{config: config}
func NewMem(config ...*MemStoreConfig) *Memory {
if len(config) == 0 {
config = append(config, DefaultMemStoreConfig)
}
return &Memory{config: config[0]}
}

type Memory struct {
Expand All @@ -31,7 +36,7 @@ type Memory struct {
once sync.Once
}

func (m *Memory) Init(conf *Config, prefix string) error {
func (m *Memory) Init(conf *Config) error {
m.once.Do(func() {
c := ccache.New(ccache.Configure[string]().MaxSize(m.config.MaxSize))
m.cache = c
Expand Down
30 changes: 22 additions & 8 deletions storage/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,29 @@ import (

var _ DataStorage = &Redis{}

func NewRedisWithClient(client *redis.Client) *Redis {
return &Redis{
client: client,
}
type RedisStoreConfig struct {
KeyPrefix string // key prefix will be random if not set

Client *redis.Client // if Client is not nil, Options will be ignored
Options *redis.Options
}

func NewRedisWithOptions(options *redis.Options) *Redis {
return NewRedisWithClient(redis.NewClient(options))
func NewRedis(config ...*RedisStoreConfig) *Redis {
if len(config) == 0 {
panic("redis config is required")
}
if config[0].KeyPrefix == "" {
config[0].KeyPrefix = util.GormCachePrefix + ":" + util.GenInstanceId()
}
r := &Redis{
keyPrefix: config[0].KeyPrefix,
}
if config[0].Client != nil {
r.client = config[0].Client
return r
}
r.client = redis.NewClient(config[0].Options)
return r
}

type Redis struct {
Expand All @@ -33,13 +48,12 @@ type Redis struct {
once sync.Once
}

func (r *Redis) Init(conf *Config, prefix string) error {
func (r *Redis) Init(conf *Config) error {
var err error
r.once.Do(func() {
r.ttl = conf.TTL
r.logger = conf.Logger
r.logger.SetIsDebug(conf.Debug)
r.keyPrefix = prefix
err = r.initScripts()
})
return err
Expand Down

0 comments on commit c430b94

Please sign in to comment.