Compare commits

..

8 Commits

Author SHA1 Message Date
bryanqiu
499d8b2f00 v1.4.1 add pool get timeout 2024-05-16 17:54:09 +08:00
bryanqiu
5298a2cabf cocurrent fix 2023-09-07 11:52:20 +08:00
bryanqiu
89b9506902 v1.3.2 fix cfg bug 2023-04-28 09:00:12 +08:00
bryanqiu
4ce4df224c v1.3.1 add UseExpiredCache feature 2023-04-27 10:46:02 +08:00
bryanqiu
0db6e87b75 add OptWithRedisConn 2023-04-13 12:34:16 +08:00
bryanqiu
bea6d37069 fix bug 2023-04-13 11:39:26 +08:00
bryanqiu
3888bcb8cd add redisPool 2023-04-13 09:53:30 +08:00
bryanqiu
3ca8e82637 typo fix 2023-04-10 14:18:19 +08:00

292
cache.go
View File

@ -1,9 +1,12 @@
package cache package cache
import ( import (
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"reflect"
"sync"
"time" "time"
"github.com/gomodule/redigo/redis" "github.com/gomodule/redigo/redis"
@ -11,9 +14,12 @@ import (
) )
var ( var (
ErrExpired = errors.New("expired")
ErrNotFound = errors.New("not found") ErrNotFound = errors.New("not found")
ErrTimeout = errors.New("not found(timeout)")
OptWithoutGetter = optWithoutGetter{} // for get only OptWithoutGetter = optWithoutGetter{} // for get only
OptWithCreateTime = optWithCreateTime{} // for set & get OptWithCreateTime = optWithCreateTime{} // for set & get
OptWithRedisConn = optWithRedisConn{} // for set & get
) )
type Cacher[T any] interface { type Cacher[T any] interface {
@ -25,20 +31,87 @@ type Getter[T any] interface {
GetById(id string) (dat *T, err error) GetById(id string) (dat *T, err error)
} }
type cacher[T any] struct {
cfg Config
getter Getter[T]
localCache *localCacher[T]
redisCache *redisCacher[T]
}
type ( type (
Option any Option any
optWithoutGetter struct{} // for get only optWithoutGetter struct{} // for get only
optWithRedisConn struct{ redisconn redis.Conn } // for set & get
optWithCreateTime struct{ createtime *time.Time } // for set & get optWithCreateTime struct{ createtime *time.Time } // for set & get
Options struct { Options struct {
withoutGetter *optWithoutGetter withoutGetter *optWithoutGetter
withCreateTime *optWithCreateTime withCreateTime *optWithCreateTime
withRedisConn *optWithRedisConn
} }
) )
func optionParser(options ...Option) (opts Options, err error) { type Config struct {
UseLocalCache bool // use localcache or not
LocalCacheLifetimeSecond int64 // local cache lifetime(t<0: forever; t=0: default; t>0: seconds)
UseRedisCache bool // use redis or not
RedisCacheConn redis.Conn `json:"-"` // redis.Conn
RedisCacheConnPool *redis.Pool `json:"-"` // redis.Pool
RedisCacheKeyPrefix string // redis key prefix
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
UseExpiredCache *bool // use expired cache or not (true: &[]bool{true}[0], false: &[]bool{true}[0])
}
func NewCache[T any](getter Getter[T], cfg Config) Cacher[T] {
var c = new(cacher[T])
// Getter
c.getter = getter
// Local cache
if cfg.UseLocalCache {
c.localCache = new(localCacher[T])
c.localCache.cacher = c
c.localCache.cacheItems = sync.Map{} //map[string]*cacheItem[T]{}
c.localCache.cacheDuration = time.Duration(cfg.LocalCacheLifetimeSecond) * time.Second
if c.localCache.cacheDuration.Nanoseconds() == 0 {
c.localCache.cacheDuration = 60 * time.Second
}
}
// Redis cache
if cfg.UseRedisCache && (cfg.RedisCacheConn != nil || cfg.RedisCacheConnPool != nil) {
if cfg.RedisCacheKeyPrefix == "" {
panic("redis cache's key prefix must not be null")
}
c.redisCache = new(redisCacher[T])
c.redisCache.cacher = c
c.redisCache.rds = cfg.RedisCacheConn
c.redisCache.rdsPool = cfg.RedisCacheConnPool
c.redisCache.cacheDuration = time.Duration(cfg.RedisCacheLifetimeSecond) * time.Second
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 {
panic("want to user redis cache, but redis.Conn is nil")
}
//UseExpiredCache
if cfg.UseExpiredCache == nil {
cfg.UseExpiredCache = &[]bool{true}[0]
}
c.cfg = cfg
name := reflect.TypeOf(*new(T)).String()
log.PrintPretty("new cache '"+name+"' by config:", cfg)
return c
}
func optionParser(options ...Option) (opts Options) {
var l = len(options) var l = len(options)
if l == 0 { if l == 0 {
return opts, nil return opts
} }
//TODO: add i out of range & options[i] type mismatch panic recover //TODO: add i out of range & options[i] type mismatch panic recover
for i := 0; i < l; i++ { for i := 0; i < l; i++ {
@ -50,11 +123,15 @@ func optionParser(options ...Option) (opts Options, err error) {
opts.withCreateTime = new(optWithCreateTime) opts.withCreateTime = new(optWithCreateTime)
i++ i++
opts.withCreateTime.createtime = options[i].(*time.Time) opts.withCreateTime.createtime = options[i].(*time.Time)
case optWithRedisConn:
opts.withRedisConn = new(optWithRedisConn)
i++
opts.withRedisConn.redisconn = options[i].(redis.Conn)
default: default:
panic("unreachable code, maybe miss some code modification") panic("unreachable code, maybe miss some code modification")
} }
} }
return opts, nil return opts
} }
func isOptWithCreateTimeForSetter(opts Options) bool { func isOptWithCreateTimeForSetter(opts Options) bool {
@ -79,118 +156,100 @@ func isOptWithCreateTimeForGetter(opts Options) bool {
return createtime.IsZero() return createtime.IsZero()
} }
type Config struct {
UseLocalCache bool // use localcache or not
LocalCacheLifetimeSecond int64 // local cache lifetime(t<0: forever; t=0: default; t>0: seconds)
UseRedisCache bool // use redis or not
RedisCacheConn redis.Conn // redis.Conn
RedisCacheKeyPrefix string // redis key prefix
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] {
var c = new(cacher[T])
// Getter
c.cfg = cfg
c.getter = getter
// Local cache
if cfg.UseLocalCache {
c.localCache = new(localCacher[T])
c.localCache.cacheItems = map[string]*cacheItem[T]{}
c.localCache.cacheDuration = time.Duration(cfg.LocalCacheLifetimeSecond) * time.Second
if c.localCache.cacheDuration.Nanoseconds() == 0 {
c.localCache.cacheDuration = 60 * time.Second
}
}
// Redis cache
if cfg.UseRedisCache && cfg.RedisCacheConn != nil {
if cfg.RedisCacheKeyPrefix == "" {
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)
if c.redisCache.cacheDuration.Nanoseconds() == 0 {
c.redisCache.cacheDuration = 180 * time.Second
}
} else if cfg.UseRedisCache {
panic("want to user redis cache, but redis.Conn is nil")
}
return c
}
type cacheItem[T any] struct { type cacheItem[T any] struct {
Data *T `json:"d"` //cache data Data *T `json:"d"` //cache data
CreateTime time.Time `json:"t"` //cache create time CreateTime time.Time `json:"t"` //cache create time
} }
type localCacher[T any] struct { type localCacher[T any] struct {
cacheItems map[string]*cacheItem[T] cacher *cacher[T] // parents cacher pointer
cacheDuration time.Duration cacheItems sync.Map // map[string]*cacheItem[T]
cacheDuration time.Duration // cache alive duration
} }
type redisCacher[T any] struct { type redisCacher[T any] struct {
rds redis.Conn cacher *cacher[T]
cachePrefix string cachePrefix string
cacheDuration time.Duration cacheDuration time.Duration
} rds redis.Conn
rdsPool *redis.Pool
type cacher[T any] struct {
cfg Config
getter Getter[T]
localCache *localCacher[T]
redisCache *redisCacher[T]
} }
func (c *localCacher[T]) GetFromCache(id string, options ...Option) (t *T, err error) { func (c *localCacher[T]) GetFromCache(id string, options ...Option) (t *T, err error) {
var a, ok = c.cacheItems[id] // Step 1. get from map
if !ok { var a *cacheItem[T] = nil
if aa, ok := c.cacheItems.Load(id); !ok {
return nil, ErrNotFound return nil, ErrNotFound
} else if a, ok = aa.(*cacheItem[T]); !ok {
panic("unreachable code, inner item type must be '*cacheItem[T]'")
} }
// Step 2. check is expired or not
if c.cacheDuration.Nanoseconds() > 0 { if c.cacheDuration.Nanoseconds() > 0 {
var earliestCreateTime = time.Now().Add(-c.cacheDuration) var earliestCreateTime = time.Now().Add(-c.cacheDuration)
if a.CreateTime.Before(earliestCreateTime) { if a.CreateTime.Before(earliestCreateTime) {
log.Infof("app(%s) is in redis cache but expired", id) if *c.cacher.cfg.UseExpiredCache {
//TODO: cocurrent log.Infof("cache(%s) in local is expired, "+
delete(c.cacheItems, id) "we will use it when all other caches are missing", id)
return nil, ErrNotFound err = ErrExpired
} else {
log.Infof("cache(%s) is in local cache but expired, we delete it", id)
c.cacheItems.Delete(id)
return nil, ErrNotFound
}
} }
} }
opt, err := optionParser(options...) // Step 3. get cache creattime if need
if err == nil && isOptWithCreateTimeForGetter(opt) { var opt = optionParser(options...)
if isOptWithCreateTimeForGetter(opt) {
*opt.withCreateTime.createtime = a.CreateTime *opt.withCreateTime.createtime = a.CreateTime
} }
// Step 4. return
if err == ErrExpired {
return a.Data, ErrExpired
} else if err != nil {
panic("unreachable code: only ErrExpired error valid when data exist")
}
return a.Data, nil return a.Data, nil
} }
func (c *localCacher[T]) SetIntoCache(id string, t *T, options ...Option) (err error) { func (c *localCacher[T]) SetIntoCache(id string, t *T, options ...Option) (err error) {
var opt = optionParser(options...)
var newitem = &cacheItem[T]{Data: t} var newitem = &cacheItem[T]{Data: t}
opt, err := optionParser(options...) if isOptWithCreateTimeForSetter(opt) {
if err == nil && isOptWithCreateTimeForSetter(opt) {
newitem.CreateTime = *opt.withCreateTime.createtime newitem.CreateTime = *opt.withCreateTime.createtime
} else { } else {
newitem.CreateTime = time.Now() newitem.CreateTime = time.Now()
} }
c.cacheItems[id] = newitem c.cacheItems.Store(id, newitem)
return nil return nil
} }
func (c *redisCacher[T]) GetFromCache(id string, options ...Option) (*T, error) { func (c *redisCacher[T]) GetFromCache(id string, options ...Option) (*T, error) {
var ( var (
rds = c.rds err error = nil
redisKey = c.cachePrefix + id rds redis.Conn = nil
redisValue = "" opt = optionParser(options...)
err error = nil redisKey = c.cachePrefix + id
redisValue = ""
) )
if opt.withRedisConn != nil && opt.withRedisConn.redisconn != nil {
rds = opt.withRedisConn.redisconn
} else if c.rdsPool != nil {
// timeout context
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
// redis conn from pool
rds, err = c.rdsPool.GetContext(ctx)
if err != nil {
return nil, ErrTimeout
}
defer rds.Close()
} else if c.rds != nil {
rds = c.rds
}
// Step 1. read from redis // Step 1. read from redis
if redisValue, err = redis.String(rds.Do("GET", redisKey)); err != nil { if redisValue, err = redis.String(rds.Do("GET", redisKey)); err != nil {
@ -209,32 +268,59 @@ func (c *redisCacher[T]) GetFromCache(id string, options ...Option) (*T, error)
if c.cacheDuration.Nanoseconds() > 0 { if c.cacheDuration.Nanoseconds() > 0 {
var earliestCreateTime = time.Now().Add(-c.cacheDuration) var earliestCreateTime = time.Now().Add(-c.cacheDuration)
if a.CreateTime.Before(earliestCreateTime) { if a.CreateTime.Before(earliestCreateTime) {
log.Infof("app(%s) is in redis cache but expired", id) if *c.cacher.cfg.UseExpiredCache {
return nil, ErrNotFound log.Infof("cache(%s) in redis is expired, "+
"we will use it when all other caches are missing", id)
err = ErrExpired
} else {
log.Infof("app(%s) is in redis cache but expired", id)
return nil, ErrNotFound
}
} }
} }
// Step 4. get cache creattime if need // Step 4. get cache creattime if need
opt, err := optionParser(options...) if isOptWithCreateTimeForGetter(opt) {
if err == nil && isOptWithCreateTimeForGetter(opt) {
*opt.withCreateTime.createtime = a.CreateTime *opt.withCreateTime.createtime = a.CreateTime
} }
// Step 5. return
if err == ErrExpired {
return a.Data, ErrExpired
} else if err != nil {
panic("unreachable code")
}
return a.Data, nil return a.Data, nil
} }
func (c *redisCacher[T]) SetIntoCache(id string, t *T, options ...Option) error { func (c *redisCacher[T]) SetIntoCache(id string, t *T, options ...Option) error {
var ( var (
opt, _ = optionParser(options...) opt = optionParser(options...)
rds = c.rds rds redis.Conn = nil
redisKey = c.cachePrefix + id redisKey = c.cachePrefix + id
redisValue = []byte{} redisValue = []byte{}
err error = nil err error = nil
) )
// Step 0. redis.Conn
if opt.withRedisConn != nil && opt.withRedisConn.redisconn != nil {
rds = opt.withRedisConn.redisconn
} else if c.rdsPool != nil {
// timeout context
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
// redis conn from pool
rds, err = c.rdsPool.GetContext(ctx)
if err != nil {
return errors.New("set cache failed: 'get redis conn timeout'")
}
defer rds.Close()
} else if c.rds != nil {
rds = c.rds
}
// Step 1. encode to string // Step 1. encode to string
var a = cacheItem[T]{Data: t} var a = cacheItem[T]{Data: t}
if err == nil && isOptWithCreateTimeForSetter(opt) { if isOptWithCreateTimeForSetter(opt) {
a.CreateTime = *opt.withCreateTime.createtime a.CreateTime = *opt.withCreateTime.createtime
} else { } else {
a.CreateTime = time.Now() a.CreateTime = time.Now()
@ -259,21 +345,26 @@ func (c *cacher[T]) GetFromCache(id string, options ...Option) (dat *T, err erro
return nil, ErrNotFound return nil, ErrNotFound
} }
var opt, _ = optionParser(options...) var opt = optionParser(options...)
var lastExpiredDat *T = nil
// 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, options...); 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 if err == ErrExpired {
log.Infof("get cache(id:%s) from localCacher success(but expired), need try next", id)
lastExpiredDat = dat
} else {
log.Infof("get cache(id:%s) from localCacher failed, need 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 opt.withCreateTime == nil { //get create time from redis //if opt.withCreateTime == nil { //get create time from redis
options = append(options, OptWithCreateTime, new(time.Time)) // options = append(options, OptWithCreateTime, new(time.Time))
} //}
if dat, err := c.redisCache.GetFromCache(id, options...); err == nil { if dat, err := c.redisCache.GetFromCache(id, options...); err == nil {
if c.localCache != nil { if c.localCache != nil {
c.localCache.SetIntoCache(id, dat, options...) //set create time from redis c.localCache.SetIntoCache(id, dat, options...) //set create time from redis
@ -281,16 +372,28 @@ func (c *cacher[T]) GetFromCache(id string, options ...Option) (dat *T, err erro
} }
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 err == ErrExpired {
if c.localCache != nil {
c.localCache.SetIntoCache(id, dat, options...) //set create time from redis
log.Infof("set cache(id:%s) to localCache by redisCacher done", id)
}
log.Infof("get cache(id:%s) from redisCacher success(but expired), need try next", id)
lastExpiredDat = dat
} else {
log.Infof("get cache(id:%s) from redisCacher failed, need try from next cacher", id)
} }
log.Infof("get cache(id:%s) from redisCacher failed, try next", id)
} }
var getter = c.getter var getter = c.getter
if opt.withoutGetter != nil { if opt.withoutGetter != nil {
log.Infof("all cache failed, and option 'withnogetter' is set, return ErrNotFound") log.Infof("all cache failed, and option 'withoutgetter' is set, return ErrNotFound")
return nil, ErrNotFound return nil, ErrNotFound
} }
if getter == nil {
if getter == nil && lastExpiredDat != nil {
log.Infof("all cache failed(and getter is nil) but expired cache is avaliable, we use it")
return lastExpiredDat, nil
} else if getter == nil {
log.Infof("all cache failed, and getter is nil, return ErrNotFound") log.Infof("all cache failed, and getter is nil, return ErrNotFound")
if c.cfg.GetterNoWarning == false { if c.cfg.GetterNoWarning == false {
log.Warningf("cache 'getter' is nil, did you save right config in database?") log.Warningf("cache 'getter' is nil, did you save right config in database?")
@ -300,7 +403,10 @@ func (c *cacher[T]) GetFromCache(id string, options ...Option) (dat *T, err erro
// Step 4. get from [storager(database or somewhere)] // Step 4. get from [storager(database or somewhere)]
dat, err = getter.GetById(id) dat, err = getter.GetById(id)
if err != nil { if err != nil && lastExpiredDat != nil {
log.Infof("all cache failed and getter failed, expired cache is avaliable, we will use it")
return lastExpiredDat, nil
} else 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
} }