From b91e28a512dd325aec87b3d6dfb3f586233e621e Mon Sep 17 00:00:00 2001 From: fancl Date: Wed, 20 Sep 2023 11:05:08 +0800 Subject: [PATCH] update cache protocol --- pkg/cache/cache.go | 10 ++++---- pkg/cache/instance.go | 49 +++++++++++++++++++++++++++++++++----- pkg/cache/instance_test.go | 24 +++++++++++++++++++ pkg/cache/memcache.go | 28 +++++++++++++--------- 4 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 pkg/cache/instance_test.go diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 3e15b6b..2a18616 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -6,13 +6,13 @@ import ( ) type ( - LoadFunc func(ctx context.Context) (any, error) + LoadFunc func(ctx context.Context) ([]byte, error) ) type Cache interface { - Set(ctx context.Context, key string, value any) - SetEx(ctx context.Context, key string, value any, expire time.Duration) - Get(ctx context.Context, key string) (value any, ok bool) - Try(ctx context.Context, key string, cb LoadFunc) (value any, err error) + Set(ctx context.Context, key string, buf []byte) + SetEx(ctx context.Context, key string, buf []byte, expire time.Duration) + Get(ctx context.Context, key string) (buf []byte, ok bool) + Try(ctx context.Context, key string, cb LoadFunc) (buf []byte, err error) Del(ctx context.Context, key string) } diff --git a/pkg/cache/instance.go b/pkg/cache/instance.go index 3d0a236..bab5f76 100644 --- a/pkg/cache/instance.go +++ b/pkg/cache/instance.go @@ -2,6 +2,8 @@ package cache import ( "context" + "encoding/json" + "os" "time" ) @@ -21,22 +23,57 @@ func GetCache() Cache { return std } -func Set(ctx context.Context, key string, value any) { - std.Set(ctx, key, value) +// Set 设置缓存数据 +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) { - std.SetEx(ctx, key, value, expire) +// SetEx 设置一个有效时间的缓存数据 +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) } -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) } +// Del 删除缓存数据 func Del(ctx context.Context, key string) { 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 +} diff --git a/pkg/cache/instance_test.go b/pkg/cache/instance_test.go new file mode 100644 index 0000000..dbabda0 --- /dev/null +++ b/pkg/cache/instance_test.go @@ -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") + } +} diff --git a/pkg/cache/memcache.go b/pkg/cache/memcache.go index d8061ae..fadf412 100644 --- a/pkg/cache/memcache.go +++ b/pkg/cache/memcache.go @@ -30,32 +30,38 @@ type MemCache struct { 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 ( ok bool ) - if value, ok = cache.engine.Get(key); ok { - return value, nil + if buf, ok = cache.Get(ctx, key); ok { + return buf, nil } if cb == nil { return nil, os.ErrNotExist } - if value, err = cb(ctx); err == nil { - cache.engine.Set(key, value, 0) + if buf, err = cb(ctx); err == nil { + cache.Set(ctx, key, buf) } return } -func (cache *MemCache) Set(ctx context.Context, key string, value any) { - cache.engine.Set(key, value, 0) +func (cache *MemCache) Set(ctx context.Context, key string, buf []byte) { + cache.engine.Set(key, buf, 0) } -func (cache *MemCache) SetEx(ctx context.Context, key string, value any, expire time.Duration) { - cache.engine.Set(key, value, expire) +func (cache *MemCache) SetEx(ctx context.Context, key string, buf []byte, expire time.Duration) { + cache.engine.Set(key, buf, expire) } -func (cache *MemCache) Get(ctx context.Context, key string) (value any, ok bool) { - return cache.engine.Get(key) +func (cache *MemCache) Get(ctx context.Context, key string) (buf []byte, ok bool) { + 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) {