Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support golanglru cache #973

Merged
merged 4 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.2
github.com/hashicorp/golang-lru/v2 v2.0.5
github.com/hnlq715/struct2interface v0.1.5
github.com/iancoleman/strcase v0.3.0
github.com/json-iterator/go v1.1.12
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru/v2 v2.0.5 h1:wW7h1TG88eUIJ2i69gaE3uNVtEPIagzhGvHgwfx2Vm4=
github.com/hashicorp/golang-lru/v2 v2.0.5/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
Expand Down
59 changes: 59 additions & 0 deletions pkg/cache/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cache

// Storage 接入本地缓存库需要实现的接口
type Storage[K comparable, V any] interface {
GetCacheMapOrigin(key string, ids []K) (v map[K]V, idsNone []K)
SetCacheMapOrigin(key string, idsNone []K, fn func([]K) (map[K]V, error), v map[K]V) (err error)
}

type Cache[K comparable, V any] struct {
Storage[K, V]
}

// GetAndSetCacheData 获取缓存后数据
func (c *Cache[K, V]) GetAndSetCacheData(key string, id K, fn func() (V, error)) (value V, err error) {
resMap, err := c.GetAndSetCacheMap(key, []K{id}, func([]K) (map[K]V, error) {
innerVal, innerErr := fn()
return map[K]V{id: innerVal}, innerErr
})
value = resMap[id]
return
}

// GetCacheValue 获取缓存数据
func (c *Cache[K, V]) GetCacheValue(key string, id K) (value V) {
resMap, _ := c.GetCacheMapOrigin(key, []K{id})
value = resMap[id]
return
}

// SetCacheValue 设置缓存数据
func (c *Cache[K, V]) SetCacheValue(key string, id K, fn func() (V, error)) (err error) {
err = c.SetCacheMapOrigin(key, []K{id}, func([]K) (map[K]V, error) {
innerVal, innerErr := fn()
return map[K]V{id: innerVal}, innerErr
}, nil)
return
}

// GetAndSetCacheMap 获取缓存后数据 map形式
func (c *Cache[K, V]) GetAndSetCacheMap(key string, ids []K, fn func([]K) (map[K]V, error)) (v map[K]V, err error) {
// 获取缓存数据
v, idsNone := c.GetCacheMapOrigin(key, ids)

// 设置缓存数据
err = c.SetCacheMapOrigin(key, idsNone, fn, v)
return
}

// GetCacheMap 获取缓存数据 map形式
func (c *Cache[K, V]) GetCacheMap(key string, ids []K) (v map[K]V) {
v, _ = c.GetCacheMapOrigin(key, ids)
return
}

// SetCacheMap 设置缓存数据 map形式
func (c *Cache[K, V]) SetCacheMap(key string, ids []K, fn func([]K) (map[K]V, error)) (err error) {
err = c.SetCacheMapOrigin(key, ids, fn, nil)
return
}
73 changes: 7 additions & 66 deletions pkg/cache/xfreecache/v2/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,16 @@ import (
"reflect"
)

type storage interface {
// SetCacheData 设置缓存数据 key:缓存key data:缓存数据
SetCacheData(key string, data []byte) (err error)
// GetCacheData 存储缓存数据 key:缓存key data:缓存数据
GetCacheData(key string) (data []byte, err error)
}

type cache[K comparable, V any] struct {
storage
}

// GetAndSetCacheData 获取缓存后数据
func (c *cache[K, V]) GetAndSetCacheData(key string, id K, fn func() (V, error)) (value V, err error) {
resMap, err := c.GetAndSetCacheMap(key, []K{id}, func([]K) (map[K]V, error) {
innerVal, innerErr := fn()
return map[K]V{id: innerVal}, innerErr
})
value = resMap[id]
return
}

// GetCacheValue 获取缓存数据
func (c *cache[K, V]) GetCacheValue(key string, id K) (value V) {
resMap, _ := c.getCacheMap(key, []K{id})
value = resMap[id]
return
}

// SetCacheValue 设置缓存数据
func (c *cache[K, V]) SetCacheValue(key string, id K, fn func() (V, error)) (err error) {
err = c.setCacheMap(key, []K{id}, func([]K) (map[K]V, error) {
innerVal, innerErr := fn()
return map[K]V{id: innerVal}, innerErr
}, nil)
return
}

// GetAndSetCacheMap 获取缓存后数据 map形式
func (c *cache[K, V]) GetAndSetCacheMap(key string, ids []K, fn func([]K) (map[K]V, error)) (v map[K]V, err error) {
// 获取缓存数据
v, idsNone := c.getCacheMap(key, ids)

// 设置缓存数据
err = c.setCacheMap(key, idsNone, fn, v)
return
}

// GetCacheMap 获取缓存数据 map形式
func (c *cache[K, V]) GetCacheMap(key string, ids []K) (v map[K]V) {
v, _ = c.getCacheMap(key, ids)
return
}

// SetCacheMap 设置缓存数据 map形式
func (c *cache[K, V]) SetCacheMap(key string, ids []K, fn func([]K) (map[K]V, error)) (err error) {
err = c.setCacheMap(key, ids, fn, nil)
return
}

func (c *cache[K, V]) getCacheMap(key string, ids []K) (v map[K]V, idsNone []K) {
func (l *localStorage[K, V]) GetCacheMapOrigin(key string, ids []K) (v map[K]V, idsNone []K) {
var zero V
v = make(map[K]V)
idsNone = make([]K, 0, len(ids))

// id去重
ids = lo.Uniq(ids)
for _, id := range ids {
cacheKey := c.getKey(key, id)
resT, innerErr := c.GetCacheData(cacheKey)
cacheKey := l.getKey(key, id)
resT, innerErr := l.getCacheData(cacheKey)
if innerErr == nil && resT != nil {
var value V
// 反序列化
Expand All @@ -96,7 +37,7 @@ func (c *cache[K, V]) getCacheMap(key string, ids []K) (v map[K]V, idsNone []K)
return
}

func (c *cache[K, V]) setCacheMap(key string, idsNone []K, fn func([]K) (map[K]V, error), v map[K]V) (err error) {
func (l *localStorage[K, V]) SetCacheMapOrigin(key string, idsNone []K, fn func([]K) (map[K]V, error), v map[K]V) (err error) {
args := []zap.Field{zap.Any("key", key), zap.Any("ids", idsNone)}

if len(idsNone) == 0 {
Expand Down Expand Up @@ -135,8 +76,8 @@ func (c *cache[K, V]) setCacheMap(key string, idsNone []K, fn func([]K) (map[K]V
return
}

cacheKey := c.getKey(key, id)
err = c.SetCacheData(cacheKey, data)
cacheKey := l.getKey(key, id)
err = l.setCacheData(cacheKey, data)
if err != nil {
xlog.Jupiter().Error("GetAndSetCacheMap setCacheData", append(args, zap.Error(err))...)
return
Expand All @@ -145,6 +86,6 @@ func (c *cache[K, V]) setCacheMap(key string, idsNone []K, fn func([]K) (map[K]V
return
}

func (c *cache[K, V]) getKey(key string, id K) string {
func (l *localStorage[K, V]) getKey(key string, id K) string {
return fmt.Sprintf("%s:%v", key, id)
}
Loading
Loading