This commit is contained in:
bryanqiu 2023-04-10 11:47:31 +08:00
parent 06adbc3e49
commit 701a93b10b

221
cache.go
View File

@ -11,51 +11,113 @@ import (
) )
var ( var (
ErrNotFound = errors.New("not found") ErrNotFound = errors.New("not found")
OptWithoutGetter = optWithoutGetter{} // for get only
OptWithCreateTime = optWithCreateTime{} // for set & get
) )
type Cacher[T any] interface { type Cacher[T any] interface {
GetFromCache(id string) (dat *T, err error) GetFromCache(id string, options ...Option) (dat *T, err error)
SetIntoCache(id string, dat *T) (err error) SetIntoCache(id string, dat *T, options ...Option) (err error)
} }
type Getter[T any] interface { type Getter[T any] interface {
GetById(id string) (dat *T, err error) GetById(id string) (dat *T, err error)
} }
type (
Option any
optWithoutGetter struct{} // for get only
optWithCreateTime struct{ createtime *time.Time } // for set & get
Options struct {
withoutGetter *optWithoutGetter
withCreateTime *optWithCreateTime
}
)
func optionParser(options ...Option) (opts Options, err error) {
var l = len(options)
if l == 0 {
return opts, nil
}
//TODO: add i out of range & options[i] type mismatch panic recover
for i := 0; i < l; i++ {
o := options[i]
switch o.(type) {
case optWithoutGetter:
opts.withoutGetter = new(optWithoutGetter)
case optWithCreateTime:
opts.withCreateTime = new(optWithCreateTime)
i++
opts.withCreateTime.createtime = options[i].(*time.Time)
default:
panic("unreachable code, maybe miss some code modification")
}
}
return opts, nil
}
func isOptWithCreateTimeForSetter(opts Options) bool {
if opts.withCreateTime == nil {
return false
}
var createtime = opts.withCreateTime.createtime
if createtime == nil {
return false
}
return !createtime.IsZero()
}
func isOptWithCreateTimeForGetter(opts Options) bool {
if opts.withCreateTime == nil {
return false
}
var createtime = opts.withCreateTime.createtime
if createtime == nil {
return false
}
return createtime.IsZero()
}
type Config struct { type Config struct {
UseLocalCache bool UseLocalCache bool // use localcache or not
LocalCacheLifetimeSecond int64 LocalCacheLifetimeSecond int64 // local cache lifetime(t<0: forever; t=0: default; t>0: seconds)
UseRedisCache bool UseRedisCache bool // use redis or not
RedisCacheConn redis.Conn RedisCacheConn redis.Conn // redis.Conn
RedisCacheKeyPrefix string RedisCacheKeyPrefix string // redis key prefix
RedisCacheLifetimeSecond int64 RedisCacheLifetimeSecond int64 // redis cache lifetime(t<0: forever; t=0: default; t>0: seconds)
UseGetter bool // not used
GetterNoWarning bool // no warning if no getter
} }
func NewCache[T any](getter Getter[T], cfg Config) Cacher[T] { func NewCache[T any](getter Getter[T], cfg Config) Cacher[T] {
var c = new(cacher[T]) var c = new(cacher[T])
// Getter // Getter
c.cfg = cfg
c.getter = getter c.getter = getter
// Local cache // Local cache
if cfg.UseLocalCache { if cfg.UseLocalCache {
c.localCache = new(localCacher[T]) c.localCache = new(localCacher[T])
c.localCache.cacheItems = map[string]*cacheItem[T]{} c.localCache.cacheItems = map[string]*cacheItem[T]{}
c.localCache.cacheSecond = time.Duration(cfg.LocalCacheLifetimeSecond) * time.Second c.localCache.cacheDuration = time.Duration(cfg.LocalCacheLifetimeSecond) * time.Second
if c.localCache.cacheSecond.Nanoseconds() <= 0 { if c.localCache.cacheDuration.Nanoseconds() == 0 {
c.localCache.cacheSecond = 60 * time.Second c.localCache.cacheDuration = 60 * time.Second
} }
} }
// Redis cache // Redis cache
if cfg.UseRedisCache && cfg.RedisCacheConn != nil { if cfg.UseRedisCache && cfg.RedisCacheConn != nil {
c.redisCache = new(redisCacher[T])
c.redisCache.rds = cfg.RedisCacheConn
c.redisCache.cacheSecond = time.Duration(cfg.RedisCacheLifetimeSecond) * time.Second
if cfg.RedisCacheKeyPrefix == "" { if cfg.RedisCacheKeyPrefix == "" {
panic("redis cache's key prefix must not be null") panic("redis cache's key prefix must not be null")
} }
c.redisCache = new(redisCacher[T])
c.redisCache.rds = cfg.RedisCacheConn
c.redisCache.cacheDuration = time.Duration(cfg.RedisCacheLifetimeSecond) * time.Second
c.redisCache.cachePrefix = fmt.Sprintf("%s-cache-id:", cfg.RedisCacheKeyPrefix) c.redisCache.cachePrefix = fmt.Sprintf("%s-cache-id:", cfg.RedisCacheKeyPrefix)
if c.redisCache.cacheDuration.Nanoseconds() == 0 {
c.redisCache.cacheDuration = 180 * time.Second
}
} else if cfg.UseRedisCache { } else if cfg.UseRedisCache {
panic("want to user redis cache, but redis.Conn is nil") panic("want to user redis cache, but redis.Conn is nil")
} }
@ -69,44 +131,60 @@ type cacheItem[T any] struct {
} }
type localCacher[T any] struct { type localCacher[T any] struct {
cacheItems map[string]*cacheItem[T] cacheItems map[string]*cacheItem[T]
cacheSecond time.Duration cacheDuration time.Duration
} }
type redisCacher[T any] struct { type redisCacher[T any] struct {
rds redis.Conn rds redis.Conn
cachePrefix string cachePrefix string
cacheSecond time.Duration cacheDuration time.Duration
} }
type cacher[T any] struct { type cacher[T any] struct {
cfg Config
getter Getter[T] getter Getter[T]
localCache *localCacher[T] localCache *localCacher[T]
redisCache *redisCacher[T] redisCache *redisCacher[T]
} }
func (c *localCacher[T]) GetFromCache(id string) (t *T, err error) { func (c *localCacher[T]) GetFromCache(id string, options ...Option) (t *T, err error) {
var earliestCreateTime = time.Now().Add(-c.cacheSecond) var a, ok = c.cacheItems[id]
if a, ok := c.cacheItems[id]; !ok { if !ok {
return nil, ErrNotFound return nil, ErrNotFound
} else if earliestCreateTime.Before(a.CreateTime) {
return a.Data, nil
} }
//TODO: cocurrent
delete(c.cacheItems, id) if c.cacheDuration.Nanoseconds() > 0 {
return nil, ErrNotFound var earliestCreateTime = time.Now().Add(-c.cacheDuration)
if a.CreateTime.Before(earliestCreateTime) {
log.Infof("app(%s) is in redis cache but expired", id)
//TODO: cocurrent
delete(c.cacheItems, id)
return nil, ErrNotFound
}
}
opt, err := optionParser(options...)
if err == nil && isOptWithCreateTimeForGetter(opt) {
*opt.withCreateTime.createtime = a.CreateTime
}
return a.Data, nil
} }
func (c *localCacher[T]) SetIntoCache(id string, t *T) (err error) { func (c *localCacher[T]) SetIntoCache(id string, t *T, options ...Option) (err error) {
c.cacheItems[id] = &cacheItem[T]{ var newitem = &cacheItem[T]{Data: t}
Data: t, opt, err := optionParser(options...)
CreateTime: time.Now(), if err == nil && isOptWithCreateTimeForSetter(opt) {
newitem.CreateTime = *opt.withCreateTime.createtime
} else {
newitem.CreateTime = time.Now()
} }
fmt.Println("cacheItems", c.cacheItems) c.cacheItems[id] = newitem
return nil return nil
} }
func (c *redisCacher[T]) GetFromCache(id string) (*T, error) { func (c *redisCacher[T]) GetFromCache(id string, options ...Option) (*T, error) {
var ( var (
rds = c.rds rds = c.rds
redisKey = c.cachePrefix + id redisKey = c.cachePrefix + id
@ -128,34 +206,45 @@ func (c *redisCacher[T]) GetFromCache(id string) (*T, error) {
} }
// Step 3. check expire time // Step 3. check expire time
var d = c.cacheSecond if c.cacheDuration.Nanoseconds() > 0 {
if d.Nanoseconds() > 0 { var earliestCreateTime = time.Now().Add(-c.cacheDuration)
var earliestCreateTime = time.Now().Add(-c.cacheSecond)
if a.CreateTime.Before(earliestCreateTime) { if a.CreateTime.Before(earliestCreateTime) {
log.Infof("app(%s) is in redis cache but expired", id) log.Infof("app(%s) is in redis cache but expired", id)
return nil, ErrNotFound return nil, ErrNotFound
} }
} }
// Step 4. get cache creattime if need
opt, err := optionParser(options...)
if err == nil && isOptWithCreateTimeForGetter(opt) {
*opt.withCreateTime.createtime = a.CreateTime
}
return a.Data, nil return a.Data, nil
} }
func (c *redisCacher[T]) SetIntoCache(id string, t *T) error { func (c *redisCacher[T]) SetIntoCache(id string, t *T, options ...Option) error {
var ( var (
opt, _ = optionParser(options...)
rds = c.rds rds = c.rds
redisKey = c.cachePrefix + id redisKey = c.cachePrefix + id
redisValue = []byte{} redisValue = []byte{}
err error = nil err error = nil
) )
// Step 1. decode from string // Step 1. encode to string
var a = cacheItem[T]{Data: t, CreateTime: time.Now()} var a = cacheItem[T]{Data: t}
if err == nil && isOptWithCreateTimeForSetter(opt) {
a.CreateTime = *opt.withCreateTime.createtime
} else {
a.CreateTime = time.Now()
}
if redisValue, err = json.Marshal(a); err != nil { if redisValue, err = json.Marshal(a); err != nil {
log.Errorf("set cache failed: 'json marshal failed <%s>'", err.Error()) log.Errorf("set cache failed: 'json marshal failed <%s>'", err.Error())
return errors.New("set cache failed: 'marshal failed'") return errors.New("set cache failed: 'marshal failed'")
} }
// Step 2. read from redis // Step 2. write into redis
if _, err = rds.Do("SET", redisKey, string(redisValue)); err != nil { if _, err = rds.Do("SET", redisKey, string(redisValue)); err != nil {
log.Errorf("set cache failed: 'redis failed <%s>'", err.Error()) log.Errorf("set cache failed: 'redis failed <%s>'", err.Error())
return errors.New("set cache failed: 'redis failed'") return errors.New("set cache failed: 'redis failed'")
@ -164,45 +253,53 @@ func (c *redisCacher[T]) SetIntoCache(id string, t *T) error {
return nil return nil
} }
func (c *cacher[T]) GetFromCache(id string) (dat *T, err error) { func (c *cacher[T]) GetFromCache(id string, options ...Option) (dat *T, err error) {
// Step 1. check id // Step 1. check id
if len(id) < 8 { if len(id) < 1 {
return nil, ErrNotFound return nil, ErrNotFound
} }
var opt, _ = optionParser(options...)
// Step 2. get from [local] // Step 2. get from [local]
if c.localCache != nil { if c.localCache != nil {
if dat, err := c.localCache.GetFromCache(id); err == nil { if dat, err := c.localCache.GetFromCache(id, options...); err == nil {
log.Infof("get cache(id:%s) from localCacher success", id) log.Infof("get cache(id:%s) from localCacher success", id)
return dat, nil return dat, nil
} else {
log.Infof("get cache(id:%s) from localCacher failed, try next", id)
} }
log.Infof("get cache(id:%s) from localCacher failed, try next", id)
} }
// Step 3. get from [redis] // Step 3. get from [redis]
if c.redisCache != nil { if c.redisCache != nil {
if dat, err := c.redisCache.GetFromCache(id); err == nil { if opt.withCreateTime == nil { //get create time from redis
options = append(options, OptWithCreateTime, new(time.Time))
}
if dat, err := c.redisCache.GetFromCache(id, options...); err == nil {
if c.localCache != nil { if c.localCache != nil {
log.Infof("set cache(id:%s) to localCache by redisCacher") c.localCache.SetIntoCache(id, dat, options...) //set create time from redis
c.localCache.SetIntoCache(id, dat) log.Infof("set cache(id:%s) to localCache by redisCacher done", id)
} }
log.Infof("get cache(id:%s) from redisCacher success", id) log.Infof("get cache(id:%s) from redisCacher success", id)
return dat, nil return dat, nil
} else if c.getter == nil {
log.Warningf("get cache(id:%s) from all cache failed, and storager is nil, "+
"trade as not found. may be you forgot set in db ???", id)
return nil, ErrNotFound
} }
log.Infof("get cache(id:%s) from redisCacher failed, try next", id)
} }
if c.getter == nil {
log.Warningf("get cache(id:%s) from all cache failed, and storager is nil, "+ var getter = c.getter
"trade as not found. may be you forgot set in db ???", id) if opt.withoutGetter != nil {
log.Infof("all cache failed, and option 'withnogetter' is set, return ErrNotFound")
return nil, ErrNotFound
}
if getter == nil {
log.Infof("all cache failed, and getter is nil, return ErrNotFound")
if c.cfg.GetterNoWarning == false {
log.Warningf("cache 'getter' is nil, did you save right config in database?")
}
return nil, ErrNotFound return nil, ErrNotFound
} }
// Step 4. get from [storager(database or somewhere)] // Step 4. get from [storager(database or somewhere)]
dat, err = c.getter.GetById(id) dat, err = getter.GetById(id)
if err != nil { if err != nil {
log.Errorf("cache(id:%s) is not in database, something error: %s", id, err) log.Errorf("cache(id:%s) is not in database, something error: %s", id, err)
return nil, ErrNotFound return nil, ErrNotFound
@ -211,10 +308,10 @@ func (c *cacher[T]) GetFromCache(id string) (dat *T, err error) {
// Step 5. set to cacha // Step 5. set to cacha
if c.localCache != nil { if c.localCache != nil {
c.localCache.SetIntoCache(id, dat) c.localCache.SetIntoCache(id, dat, options...)
} }
if c.redisCache != nil { if c.redisCache != nil {
c.redisCache.SetIntoCache(id, dat) c.redisCache.SetIntoCache(id, dat, options...)
} }
// Step 6. return // Step 6. return
@ -222,10 +319,10 @@ func (c *cacher[T]) GetFromCache(id string) (dat *T, err error) {
return dat, nil return dat, nil
} }
func (c *cacher[T]) SetIntoCache(id string, dat *T) (err error) { func (c *cacher[T]) SetIntoCache(id string, dat *T, options ...Option) (err error) {
// localCache // localCache
if c.localCache != nil { if c.localCache != nil {
if err = c.localCache.SetIntoCache(id, dat); err != nil { if err = c.localCache.SetIntoCache(id, dat, options...); err != nil {
log.Infof("set data into localCache failed, err:%s", err) log.Infof("set data into localCache failed, err:%s", err)
} else { } else {
log.Debugf("success set data into localCache, id: %s", id) log.Debugf("success set data into localCache, id: %s", id)
@ -234,7 +331,7 @@ func (c *cacher[T]) SetIntoCache(id string, dat *T) (err error) {
// redisCache // redisCache
if c.redisCache != nil { if c.redisCache != nil {
if err = c.redisCache.SetIntoCache(id, dat); err != nil { if err = c.redisCache.SetIntoCache(id, dat, options...); err != nil {
log.Infof("set data into redisCache failed, err:%s", err) log.Infof("set data into redisCache failed, err:%s", err)
} else { } else {
log.Debugf("success set data into redisCache, id: %s", id) log.Debugf("success set data into redisCache, id: %s", id)