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
  • Loading branch information
jaronnie committed Dec 7, 2024
1 parent 96758b9 commit 4926d92
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 6 deletions.
9 changes: 8 additions & 1 deletion cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package cache

import "github.com/zeromicro/go-zero/core/stores/cache"
import (
"context"

"github.com/zeromicro/go-zero/core/stores/cache"
)

type Cache interface {
cache.Cache

// SetNoExpireCtx Because zero cache set ctx has default expire, so jzero add this method
SetNoExpireCtx(ctx context.Context, key string, val any) error
}
85 changes: 85 additions & 0 deletions cache/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package cache

import (
"context"
"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 {
rds *redis.Redis
node zerocache.Cache
}

func (c cacheNode) 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 {
return c.node.Del(keys...)
}

func (c cacheNode) DelCtx(ctx context.Context, keys ...string) error {
return c.node.DelCtx(ctx, keys...)
}

func (c cacheNode) Get(key string, val any) error {
return c.node.Get(key, val)
}

func (c cacheNode) GetCtx(ctx context.Context, key string, val any) error {
return c.node.GetCtx(ctx, key, val)
}

func (c cacheNode) IsNotFound(err error) bool {
return c.node.IsNotFound(err)
}

func (c cacheNode) 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 {
return c.node.SetCtx(ctx, key, val)
}

func (c cacheNode) 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 {
return c.node.SetWithExpireCtx(ctx, key, val, expire)
}

func (c cacheNode) 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 {
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 {
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 {
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{
node: node,
}
}
12 changes: 7 additions & 5 deletions cache/sync/map.go → cache/sync_map.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sync
package cache

import (
"context"
Expand All @@ -8,8 +8,6 @@ import (
"time"

"github.com/zeromicro/go-zero/core/errorx"

"github.com/jzero-io/jzero-contrib/cache"
)

type (
Expand All @@ -24,8 +22,12 @@ type (
}
)

// New creates an instance of SyncMap cache driver
func New(errNotFound error) cache.Cache {
func (sm *syncMap) SetNoExpireCtx(ctx context.Context, key string, val any) error {
return sm.SetCtx(ctx, key, val)
}

// NewSyncMap creates an instance of SyncMap cache driver
func NewSyncMap(errNotFound error) Cache {
return &syncMap{
storage: &sync.Map{},
errNotFound: errNotFound,
Expand Down

0 comments on commit 4926d92

Please sign in to comment.