Skip to content

Commit

Permalink
feat(cache): update cache
Browse files Browse the repository at this point in the history
feat(cache): update cache

feat(cache): update cache

feat(cache): update cache

feat(cache): update cache

feat(cache): update cache
  • Loading branch information
jaronnie committed Dec 9, 2024
1 parent 4926d92 commit e027bd3
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,76 +10,77 @@ import (
"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
}
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)
}

func NewRedisNode(rds *redis.Redis, errNotFound error, opts ...zerocache.Option) Cache {
singleFlights := syncx.NewSingleFlight()
stats := zerocache.NewStat("redis-cache")
node := zerocache.NewNode(rds, singleFlights, stats, errNotFound, opts...)
return &cacheNode{
return &redisNode{
rds: rds,
node: node,
}
}

0 comments on commit e027bd3

Please sign in to comment.