Compare commits

..

3 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

View File

@ -1,10 +1,12 @@
package cache package cache
import ( import (
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"reflect" "reflect"
"sync"
"time" "time"
"github.com/gomodule/redigo/redis" "github.com/gomodule/redigo/redis"
@ -14,6 +16,7 @@ import (
var ( var (
ErrExpired = errors.New("expired") 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 OptWithRedisConn = optWithRedisConn{} // for set & get
@ -63,14 +66,13 @@ type Config struct {
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.cacher = c c.localCache.cacher = c
c.localCache.cacheItems = map[string]*cacheItem[T]{} c.localCache.cacheItems = sync.Map{} //map[string]*cacheItem[T]{}
c.localCache.cacheDuration = time.Duration(cfg.LocalCacheLifetimeSecond) * time.Second c.localCache.cacheDuration = time.Duration(cfg.LocalCacheLifetimeSecond) * time.Second
if c.localCache.cacheDuration.Nanoseconds() == 0 { if c.localCache.cacheDuration.Nanoseconds() == 0 {
c.localCache.cacheDuration = 60 * time.Second c.localCache.cacheDuration = 60 * time.Second
@ -100,6 +102,7 @@ func NewCache[T any](getter Getter[T], cfg Config) Cacher[T] {
cfg.UseExpiredCache = &[]bool{true}[0] cfg.UseExpiredCache = &[]bool{true}[0]
} }
c.cfg = cfg
name := reflect.TypeOf(*new(T)).String() name := reflect.TypeOf(*new(T)).String()
log.PrintPretty("new cache '"+name+"' by config:", cfg) log.PrintPretty("new cache '"+name+"' by config:", cfg)
return c return c
@ -159,9 +162,9 @@ type cacheItem[T any] struct {
} }
type localCacher[T any] struct { type localCacher[T any] struct {
cacher *cacher[T] cacher *cacher[T] // parents cacher pointer
cacheItems map[string]*cacheItem[T] cacheItems sync.Map // map[string]*cacheItem[T]
cacheDuration time.Duration cacheDuration time.Duration // cache alive duration
} }
type redisCacher[T any] struct { type redisCacher[T any] struct {
@ -174,9 +177,11 @@ type redisCacher[T any] struct {
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) {
// Step 1. get from map // Step 1. get from map
var a, ok = c.cacheItems[id] var a *cacheItem[T] = nil
if !ok { 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 // Step 2. check is expired or not
@ -189,8 +194,7 @@ func (c *localCacher[T]) GetFromCache(id string, options ...Option) (t *T, err e
err = ErrExpired err = ErrExpired
} else { } else {
log.Infof("cache(%s) is in local cache but expired, we delete it", id) log.Infof("cache(%s) is in local cache but expired, we delete it", id)
//TODO: cocurrent c.cacheItems.Delete(id)
delete(c.cacheItems, id)
return nil, ErrNotFound return nil, ErrNotFound
} }
} }
@ -206,7 +210,7 @@ func (c *localCacher[T]) GetFromCache(id string, options ...Option) (t *T, err e
if err == ErrExpired { if err == ErrExpired {
return a.Data, ErrExpired return a.Data, ErrExpired
} else if err != nil { } else if err != nil {
panic("unreachable code") panic("unreachable code: only ErrExpired error valid when data exist")
} }
return a.Data, nil return a.Data, nil
} }
@ -219,7 +223,7 @@ func (c *localCacher[T]) SetIntoCache(id string, t *T, options ...Option) (err e
} else { } else {
newitem.CreateTime = time.Now() newitem.CreateTime = time.Now()
} }
c.cacheItems[id] = newitem c.cacheItems.Store(id, newitem)
return nil return nil
} }
@ -234,7 +238,14 @@ func (c *redisCacher[T]) GetFromCache(id string, options ...Option) (*T, error)
if opt.withRedisConn != nil && opt.withRedisConn.redisconn != nil { if opt.withRedisConn != nil && opt.withRedisConn.redisconn != nil {
rds = opt.withRedisConn.redisconn rds = opt.withRedisConn.redisconn
} else if c.rdsPool != nil { } else if c.rdsPool != nil {
rds = c.rdsPool.Get() // 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() defer rds.Close()
} else if c.rds != nil { } else if c.rds != nil {
rds = c.rds rds = c.rds
@ -294,7 +305,14 @@ func (c *redisCacher[T]) SetIntoCache(id string, t *T, options ...Option) error
if opt.withRedisConn != nil && opt.withRedisConn.redisconn != nil { if opt.withRedisConn != nil && opt.withRedisConn.redisconn != nil {
rds = opt.withRedisConn.redisconn rds = opt.withRedisConn.redisconn
} else if c.rdsPool != nil { } else if c.rdsPool != nil {
rds = c.rdsPool.Get() // 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() defer rds.Close()
} else if c.rds != nil { } else if c.rds != nil {
rds = c.rds rds = c.rds
@ -362,7 +380,7 @@ func (c *cacher[T]) GetFromCache(id string, options ...Option) (dat *T, err erro
log.Infof("get cache(id:%s) from redisCacher success(but expired), need try next", id) log.Infof("get cache(id:%s) from redisCacher success(but expired), need try next", id)
lastExpiredDat = dat lastExpiredDat = dat
} else { } else {
log.Infof("get cache(id:%s) from redisCacher failed, need try next", id) log.Infof("get cache(id:%s) from redisCacher failed, need try from next cacher", id)
} }
} }