cocurrent fix

This commit is contained in:
bryanqiu 2023-09-07 11:52:20 +08:00
parent 89b9506902
commit 5298a2cabf

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"reflect" "reflect"
"sync"
"time" "time"
"github.com/gomodule/redigo/redis" "github.com/gomodule/redigo/redis"
@ -69,7 +70,7 @@ func NewCache[T any](getter Getter[T], cfg Config) Cacher[T] {
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
@ -159,9 +160,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 +175,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 +192,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 +208,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 +221,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
} }