Kaynağa Gözat

update cache protocol

fancl 1 yıl önce
ebeveyn
işleme
b91e28a512
4 değiştirilmiş dosya ile 89 ekleme ve 22 silme
  1. 5 5
      pkg/cache/cache.go
  2. 43 6
      pkg/cache/instance.go
  3. 24 0
      pkg/cache/instance_test.go
  4. 17 11
      pkg/cache/memcache.go

+ 5 - 5
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)
 }

+ 43 - 6
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
+}

+ 24 - 0
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")
+	}
+}

+ 17 - 11
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) {