From c550ce47c2ab8ce8f9d7738d69a44c6dece762c5 Mon Sep 17 00:00:00 2001 From: jaron Date: Mon, 9 Dec 2024 11:23:04 +0800 Subject: [PATCH] feat(cache): update cache feat(cache): update cache feat(cache): update cache feat(cache): update cache feat(cache): update cache --- cache/redis.go | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/cache/redis.go b/cache/redis.go index 9eb90ce..1339909 100644 --- a/cache/redis.go +++ b/cache/redis.go @@ -2,20 +2,20 @@ package cache import ( "context" + "github.com/zeromicro/go-zero/core/jsonx" "time" - "github.com/zeromicro/go-zero/core/jsonx" zerocache "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/redis" "github.com/zeromicro/go-zero/core/syncx" ) -type cacheNode struct { +type redisNode struct { rds *redis.Redis node zerocache.Cache } -func (c cacheNode) SetNoExpireCtx(ctx context.Context, key string, val any) error { +func (c redisNode) SetNoExpireCtx(ctx context.Context, key string, val any) error { data, err := jsonx.Marshal(val) if err != nil { return err @@ -23,55 +23,55 @@ func (c cacheNode) SetNoExpireCtx(ctx context.Context, key string, val any) erro return c.rds.SetCtx(ctx, key, string(data)) } -func (c cacheNode) Del(keys ...string) error { +func (c redisNode) Del(keys ...string) error { return c.node.Del(keys...) } -func (c cacheNode) DelCtx(ctx context.Context, keys ...string) error { +func (c redisNode) DelCtx(ctx context.Context, keys ...string) error { return c.node.DelCtx(ctx, keys...) } -func (c cacheNode) Get(key string, val any) error { +func (c redisNode) Get(key string, val any) error { return c.node.Get(key, val) } -func (c cacheNode) GetCtx(ctx context.Context, key string, val any) error { +func (c redisNode) GetCtx(ctx context.Context, key string, val any) error { return c.node.GetCtx(ctx, key, val) } -func (c cacheNode) IsNotFound(err error) bool { +func (c redisNode) IsNotFound(err error) bool { return c.node.IsNotFound(err) } -func (c cacheNode) Set(key string, val any) error { +func (c redisNode) Set(key string, val any) error { return c.node.SetCtx(context.Background(), key, val) } -func (c cacheNode) SetCtx(ctx context.Context, key string, val any) error { +func (c redisNode) SetCtx(ctx context.Context, key string, val any) error { return c.node.SetCtx(ctx, key, val) } -func (c cacheNode) SetWithExpire(key string, val any, expire time.Duration) error { +func (c redisNode) SetWithExpire(key string, val any, expire time.Duration) error { return c.node.SetWithExpireCtx(context.Background(), key, val, expire) } -func (c cacheNode) SetWithExpireCtx(ctx context.Context, key string, val any, expire time.Duration) error { +func (c redisNode) SetWithExpireCtx(ctx context.Context, key string, val any, expire time.Duration) error { return c.node.SetWithExpireCtx(ctx, key, val, expire) } -func (c cacheNode) Take(val any, key string, query func(val any) error) error { +func (c redisNode) Take(val any, key string, query func(val any) error) error { return c.node.Take(val, key, query) } -func (c cacheNode) TakeCtx(ctx context.Context, val any, key string, query func(val any) error) error { +func (c redisNode) TakeCtx(ctx context.Context, val any, key string, query func(val any) error) error { return c.node.TakeCtx(ctx, val, key, query) } -func (c cacheNode) TakeWithExpire(val any, key string, query func(val any, expire time.Duration) error) error { +func (c redisNode) TakeWithExpire(val any, key string, query func(val any, expire time.Duration) error) error { return c.node.TakeWithExpire(val, key, query) } -func (c cacheNode) TakeWithExpireCtx(ctx context.Context, val any, key string, query func(val any, expire time.Duration) error) error { +func (c redisNode) TakeWithExpireCtx(ctx context.Context, val any, key string, query func(val any, expire time.Duration) error) error { return c.node.TakeWithExpireCtx(ctx, val, key, query) } @@ -79,7 +79,8 @@ func NewRedisNode(rds *redis.Redis, errNotFound error, opts ...zerocache.Option) singleFlights := syncx.NewSingleFlight() stats := zerocache.NewStat("redis-cache") node := zerocache.NewNode(rds, singleFlights, stats, errNotFound, opts...) - return &cacheNode{ + return &redisNode{ + rds: rds, node: node, } }