update cache protocol

This commit is contained in:
fancl 2023-09-20 11:05:08 +08:00
parent 183f940f65
commit b91e28a512
4 changed files with 89 additions and 22 deletions

10
pkg/cache/cache.go vendored
View File

@ -6,13 +6,13 @@ import (
) )
type ( type (
LoadFunc func(ctx context.Context) (any, error) LoadFunc func(ctx context.Context) ([]byte, error)
) )
type Cache interface { type Cache interface {
Set(ctx context.Context, key string, value any) Set(ctx context.Context, key string, buf []byte)
SetEx(ctx context.Context, key string, value any, expire time.Duration) SetEx(ctx context.Context, key string, buf []byte, expire time.Duration)
Get(ctx context.Context, key string) (value any, ok bool) Get(ctx context.Context, key string) (buf []byte, ok bool)
Try(ctx context.Context, key string, cb LoadFunc) (value any, err error) Try(ctx context.Context, key string, cb LoadFunc) (buf []byte, err error)
Del(ctx context.Context, key string) Del(ctx context.Context, key string)
} }

49
pkg/cache/instance.go vendored
View File

@ -2,6 +2,8 @@ package cache
import ( import (
"context" "context"
"encoding/json"
"os"
"time" "time"
) )
@ -21,22 +23,57 @@ func GetCache() Cache {
return std return std
} }
func Set(ctx context.Context, key string, value any) { // Set 设置缓存数据
std.Set(ctx, key, value) func Set(ctx context.Context, key string, buf []byte) {
std.Set(ctx, key, buf)
} }
func SetEx(ctx context.Context, key string, value any, expire time.Duration) { // SetEx 设置一个有效时间的缓存数据
std.SetEx(ctx, key, value, expire) func SetEx(ctx context.Context, key string, buf []byte, expire time.Duration) {
std.SetEx(ctx, key, buf, expire)
} }
func Try(ctx context.Context, key string, cb LoadFunc) (value any, err error) { // Try 尝试获取缓存数据,获取不到就设置
func Try(ctx context.Context, key string, cb LoadFunc) (buf []byte, err error) {
return std.Try(ctx, key, cb) return std.Try(ctx, key, cb)
} }
func Get(ctx context.Context, key string) (value any, ok bool) { // Get 获取缓存数据
func Get(ctx context.Context, key string) (buf []byte, ok bool) {
return std.Get(ctx, key) return std.Get(ctx, key)
} }
// Del 删除缓存数据
func Del(ctx context.Context, key string) { func Del(ctx context.Context, key string) {
std.Del(ctx, key) std.Del(ctx, key)
} }
// Store 存储缓存数据
func Store(ctx context.Context, key string, val any) (err error) {
return StoreEx(ctx, key, val, 0)
}
// StoreEx 存储缓存数据
func StoreEx(ctx context.Context, key string, val any, expire time.Duration) (err error) {
var (
buf []byte
)
if buf, err = json.Marshal(val); err != nil {
return
}
SetEx(ctx, key, buf, expire)
return
}
// Load 加载指定的缓存数据
func Load(ctx context.Context, key string, val any) (err error) {
var (
ok bool
buf []byte
)
if buf, ok = Get(ctx, key); !ok {
return os.ErrNotExist
}
err = json.Unmarshal(buf, val)
return
}

24
pkg/cache/instance_test.go vendored 100644
View File

@ -0,0 +1,24 @@
package cache
import (
"context"
"testing"
)
func TestStore(t *testing.T) {
ctx := context.Background()
var (
n int
name string
)
Store(ctx, "age", 1)
Load(ctx, "age", &n)
if n != 1 {
t.Errorf("not equal")
}
Store(ctx, "name", "zhansan")
Load(ctx, "name", &name)
if name != "zhansan" {
t.Errorf("not equal")
}
}

28
pkg/cache/memcache.go vendored
View File

@ -30,32 +30,38 @@ type MemCache struct {
engine *cache.Cache engine *cache.Cache
} }
func (cache *MemCache) Try(ctx context.Context, key string, cb LoadFunc) (value any, err error) { func (cache *MemCache) Try(ctx context.Context, key string, cb LoadFunc) (buf []byte, err error) {
var ( var (
ok bool ok bool
) )
if value, ok = cache.engine.Get(key); ok { if buf, ok = cache.Get(ctx, key); ok {
return value, nil return buf, nil
} }
if cb == nil { if cb == nil {
return nil, os.ErrNotExist return nil, os.ErrNotExist
} }
if value, err = cb(ctx); err == nil { if buf, err = cb(ctx); err == nil {
cache.engine.Set(key, value, 0) cache.Set(ctx, key, buf)
} }
return return
} }
func (cache *MemCache) Set(ctx context.Context, key string, value any) { func (cache *MemCache) Set(ctx context.Context, key string, buf []byte) {
cache.engine.Set(key, value, 0) cache.engine.Set(key, buf, 0)
} }
func (cache *MemCache) SetEx(ctx context.Context, key string, value any, expire time.Duration) { func (cache *MemCache) SetEx(ctx context.Context, key string, buf []byte, expire time.Duration) {
cache.engine.Set(key, value, expire) cache.engine.Set(key, buf, expire)
} }
func (cache *MemCache) Get(ctx context.Context, key string) (value any, ok bool) { func (cache *MemCache) Get(ctx context.Context, key string) (buf []byte, ok bool) {
return cache.engine.Get(key) var (
val any
)
if val, ok = cache.engine.Get(key); ok {
buf, ok = val.([]byte)
}
return
} }
func (cache *MemCache) Del(ctx context.Context, key string) { func (cache *MemCache) Del(ctx context.Context, key string) {