Skip to content

Commit

Permalink
Merge pull request #28 from mgtv-tech/feat_redisv9
Browse files Browse the repository at this point in the history
feat: Support go redis v9 adapter. #26
  • Loading branch information
daoshenzzg authored Sep 11, 2024
2 parents cb2df10 + db5f81b commit 9642909
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 34 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func Example_basicUsage() {
})

mycache := cache.New(cache.WithName("any"),
cache.WithRemote(remote.NewGoRedisV9Adaptor(ring)),
cache.WithRemote(remote.NewGoRedisV9Adapter(ring)),
cache.WithLocal(local.NewFreeCache(256*local.MB, time.Minute)),
cache.WithErrNotFound(errRecordNotFound))

Expand Down Expand Up @@ -111,7 +111,7 @@ func Example_advancedUsage() {
})

mycache := cache.New(cache.WithName("any"),
cache.WithRemote(remote.NewGoRedisV9Adaptor(ring)),
cache.WithRemote(remote.NewGoRedisV9Adapter(ring)),
cache.WithLocal(local.NewFreeCache(256*local.MB, time.Minute)),
cache.WithErrNotFound(errRecordNotFound),
cache.WithRefreshDuration(time.Minute))
Expand Down Expand Up @@ -139,7 +139,7 @@ func Example_mGetUsage() {
})

mycache := cache.New(cache.WithName("any"),
cache.WithRemote(remote.NewGoRedisV9Adaptor(ring)),
cache.WithRemote(remote.NewGoRedisV9Adapter(ring)),
cache.WithLocal(local.NewFreeCache(256*local.MB, time.Minute)),
cache.WithErrNotFound(errRecordNotFound),
cache.WithRemoteExpiry(time.Minute),
Expand Down Expand Up @@ -176,7 +176,7 @@ func Example_syncLocalUsage() {
pubSub := ring.Subscribe(context.Background(), channelName)

mycache := cache.New(cache.WithName("any"),
cache.WithRemote(remote.NewGoRedisV9Adaptor(ring)),
cache.WithRemote(remote.NewGoRedisV9Adapter(ring)),
cache.WithLocal(local.NewFreeCache(256*local.MB, time.Minute)),
cache.WithErrNotFound(errRecordNotFound),
cache.WithRemoteExpiry(time.Minute),
Expand Down
8 changes: 4 additions & 4 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func Example_basicUsage() {
})

mycache := cache.New(cache.WithName("any"),
cache.WithRemote(remote.NewGoRedisV9Adaptor(ring)),
cache.WithRemote(remote.NewGoRedisV9Adapter(ring)),
cache.WithLocal(local.NewFreeCache(256*local.MB, time.Minute)),
cache.WithErrNotFound(errRecordNotFound))

Expand Down Expand Up @@ -109,7 +109,7 @@ func Example_advancedUsage() {
})

mycache := cache.New(cache.WithName("any"),
cache.WithRemote(remote.NewGoRedisV9Adaptor(ring)),
cache.WithRemote(remote.NewGoRedisV9Adapter(ring)),
cache.WithLocal(local.NewFreeCache(256*local.MB, time.Minute)),
cache.WithErrNotFound(errRecordNotFound),
cache.WithRefreshDuration(time.Minute))
Expand Down Expand Up @@ -137,7 +137,7 @@ func Example_mGetUsage() {
})

mycache := cache.New(cache.WithName("any"),
cache.WithRemote(remote.NewGoRedisV9Adaptor(ring)),
cache.WithRemote(remote.NewGoRedisV9Adapter(ring)),
cache.WithLocal(local.NewFreeCache(256*local.MB, time.Minute)),
cache.WithErrNotFound(errRecordNotFound),
cache.WithRemoteExpiry(time.Minute),
Expand Down Expand Up @@ -174,7 +174,7 @@ func Example_syncLocalUsage() {
pubSub := ring.Subscribe(context.Background(), channelName)

mycache := cache.New(cache.WithName("any"),
cache.WithRemote(remote.NewGoRedisV9Adaptor(ring)),
cache.WithRemote(remote.NewGoRedisV9Adapter(ring)),
cache.WithLocal(local.NewFreeCache(256*local.MB, time.Minute)),
cache.WithErrNotFound(errRecordNotFound),
cache.WithRemoteExpiry(time.Minute),
Expand Down
2 changes: 1 addition & 1 deletion bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func newRefreshBoth() Cache {
newOnce.Do(func() {
name := "bench"
asyncCache = New(WithName(name),
WithRemote(remote.NewGoRedisV9Adaptor(rdb)),
WithRemote(remote.NewGoRedisV9Adapter(rdb)),
WithLocal(local.NewFreeCache(256*local.MB, 3*time.Second)),
WithErrNotFound(errTestNotFound),
WithRefreshDuration(2*time.Second),
Expand Down
8 changes: 4 additions & 4 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ var _ = Describe("Cache", func() {
It("with skip elements that unmarshal error", func() {
if cache.CacheType() == TypeRemote {
codecErrCache := New(WithName("codecErr"),
WithRemote(remote.NewGoRedisV9Adaptor(rdb)),
WithRemote(remote.NewGoRedisV9Adapter(rdb)),
WithCodec(mockUnmarshalErr))
cacheT := NewT[int, *object](codecErrCache)

Expand Down Expand Up @@ -301,7 +301,7 @@ var _ = Describe("Cache", func() {
It("with skip elements that marshal error", func() {
if cache.CacheType() == TypeRemote {
codecErrCache := New(WithName("codecErr"),
WithRemote(remote.NewGoRedisV9Adaptor(rdb)),
WithRemote(remote.NewGoRedisV9Adapter(rdb)),
WithCodec(mockMarshalErr))
cacheT := NewT[int, *object](codecErrCache)
ids := []int{1, 2}
Expand Down Expand Up @@ -875,15 +875,15 @@ func newLocal(localType localType) Cache {

func newRemote(rds *redis.Client) Cache {
return New(WithName("remote"),
WithRemote(remote.NewGoRedisV9Adaptor(rds)),
WithRemote(remote.NewGoRedisV9Adapter(rds)),
WithErrNotFound(errTestNotFound),
WithRefreshDuration(refreshDuration),
WithStopRefreshAfterLastAccess(stopRefreshAfterLastAccess))
}

func newBoth(rds *redis.Client, localType localType, syncLocal ...bool) Cache {
return New(WithName("both"),
WithRemote(remote.NewGoRedisV9Adaptor(rds)),
WithRemote(remote.NewGoRedisV9Adapter(rds)),
WithLocal(localNew(localType)),
WithErrNotFound(errTestNotFound),
WithRefreshDuration(refreshDuration),
Expand Down
8 changes: 4 additions & 4 deletions example_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Example_basicUsage() {
})

mycache := cache.New(cache.WithName("any"),
cache.WithRemote(remote.NewGoRedisV9Adaptor(ring)),
cache.WithRemote(remote.NewGoRedisV9Adapter(ring)),
cache.WithLocal(local.NewFreeCache(256*local.MB, time.Minute)),
cache.WithErrNotFound(errRecordNotFound))

Expand Down Expand Up @@ -75,7 +75,7 @@ func Example_advancedUsage() {
})

mycache := cache.New(cache.WithName("any"),
cache.WithRemote(remote.NewGoRedisV9Adaptor(ring)),
cache.WithRemote(remote.NewGoRedisV9Adapter(ring)),
cache.WithLocal(local.NewFreeCache(256*local.MB, time.Minute)),
cache.WithErrNotFound(errRecordNotFound),
cache.WithRefreshDuration(time.Minute))
Expand Down Expand Up @@ -103,7 +103,7 @@ func Example_mGetUsage() {
})

mycache := cache.New(cache.WithName("any"),
cache.WithRemote(remote.NewGoRedisV9Adaptor(ring)),
cache.WithRemote(remote.NewGoRedisV9Adapter(ring)),
cache.WithLocal(local.NewFreeCache(256*local.MB, time.Minute)),
cache.WithErrNotFound(errRecordNotFound),
cache.WithRemoteExpiry(time.Minute),
Expand Down Expand Up @@ -140,7 +140,7 @@ func Example_syncLocalUsage() {
pubSub := ring.Subscribe(context.Background(), channelName)

mycache := cache.New(cache.WithName("any"),
cache.WithRemote(remote.NewGoRedisV9Adaptor(ring)),
cache.WithRemote(remote.NewGoRedisV9Adapter(ring)),
cache.WithLocal(local.NewFreeCache(256*local.MB, time.Minute)),
cache.WithErrNotFound(errRecordNotFound),
cache.WithRemoteExpiry(time.Minute),
Expand Down
26 changes: 13 additions & 13 deletions remote/goredisv9adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,40 @@ import (
"github.com/redis/go-redis/v9"
)

var _ Remote = (*GoRedisV9Adaptor)(nil)
var _ Remote = (*GoRedisV9Adapter)(nil)

type GoRedisV9Adaptor struct {
type GoRedisV9Adapter struct {
client redis.Cmdable
}

// NewGoRedisV9Adaptor is
func NewGoRedisV9Adaptor(client redis.Cmdable) Remote {
return &GoRedisV9Adaptor{
// NewGoRedisV9Adapter is
func NewGoRedisV9Adapter(client redis.Cmdable) Remote {
return &GoRedisV9Adapter{
client: client,
}
}

func (r *GoRedisV9Adaptor) SetEX(ctx context.Context, key string, value any, expire time.Duration) error {
func (r *GoRedisV9Adapter) SetEX(ctx context.Context, key string, value any, expire time.Duration) error {
return r.client.SetEx(ctx, key, value, expire).Err()
}

func (r *GoRedisV9Adaptor) SetNX(ctx context.Context, key string, value any, expire time.Duration) (val bool, err error) {
func (r *GoRedisV9Adapter) SetNX(ctx context.Context, key string, value any, expire time.Duration) (val bool, err error) {
return r.client.SetNX(ctx, key, value, expire).Result()
}

func (r *GoRedisV9Adaptor) SetXX(ctx context.Context, key string, value any, expire time.Duration) (val bool, err error) {
func (r *GoRedisV9Adapter) SetXX(ctx context.Context, key string, value any, expire time.Duration) (val bool, err error) {
return r.client.SetXX(ctx, key, value, expire).Result()
}

func (r *GoRedisV9Adaptor) Get(ctx context.Context, key string) (val string, err error) {
func (r *GoRedisV9Adapter) Get(ctx context.Context, key string) (val string, err error) {
return r.client.Get(ctx, key).Result()
}

func (r *GoRedisV9Adaptor) Del(ctx context.Context, key string) (val int64, err error) {
func (r *GoRedisV9Adapter) Del(ctx context.Context, key string) (val int64, err error) {
return r.client.Del(ctx, key).Result()
}

func (r *GoRedisV9Adaptor) MGet(ctx context.Context, keys ...string) (map[string]any, error) {
func (r *GoRedisV9Adapter) MGet(ctx context.Context, keys ...string) (map[string]any, error) {
pipeline := r.client.Pipeline()
keyIdxMap := make(map[int]string, len(keys))
ret := make(map[string]any, len(keys))
Expand All @@ -68,7 +68,7 @@ func (r *GoRedisV9Adaptor) MGet(ctx context.Context, keys ...string) (map[string
return ret, nil
}

func (r *GoRedisV9Adaptor) MSet(ctx context.Context, value map[string]any, expire time.Duration) error {
func (r *GoRedisV9Adapter) MSet(ctx context.Context, value map[string]any, expire time.Duration) error {
pipeline := r.client.Pipeline()

for key, val := range value {
Expand All @@ -79,6 +79,6 @@ func (r *GoRedisV9Adaptor) MSet(ctx context.Context, value map[string]any, expir
return err
}

func (r *GoRedisV9Adaptor) Nil() error {
func (r *GoRedisV9Adapter) Nil() error {
return redis.Nil
}
8 changes: 4 additions & 4 deletions remote/goredisv9adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestGoRedisV9Adaptor_MGet(t *testing.T) {
client := NewGoRedisV9Adaptor(newRdb())
client := NewGoRedisV9Adapter(newRdb())

if err := client.SetEX(context.Background(), "key1", "value1", time.Minute); err != nil {
t.Fatal(err)
Expand All @@ -29,7 +29,7 @@ func TestGoRedisV9Adaptor_MGet(t *testing.T) {
}

func TestGoRedisV9Adaptor_MSet(t *testing.T) {
client := NewGoRedisV9Adaptor(newRdb())
client := NewGoRedisV9Adapter(newRdb())

err := client.MSet(context.Background(), map[string]any{"key1": "value1", "key2": 2}, time.Minute)
assert.Nil(t, err)
Expand All @@ -48,7 +48,7 @@ func TestGoRedisV9Adaptor_MSet(t *testing.T) {
}

func TestGoRedisV9Adaptor_Del(t *testing.T) {
client := NewGoRedisV9Adaptor(newRdb())
client := NewGoRedisV9Adapter(newRdb())

err := client.SetEX(context.Background(), "key1", "value1", time.Minute)
assert.Nil(t, err)
Expand All @@ -63,7 +63,7 @@ func TestGoRedisV9Adaptor_Del(t *testing.T) {
}

func TestGoRedisV9Adaptor_SetXxNx(t *testing.T) {
client := NewGoRedisV9Adaptor(newRdb())
client := NewGoRedisV9Adapter(newRdb())

_, err := client.SetXX(context.Background(), "key1", "value1", time.Minute)
assert.Nil(t, err)
Expand Down

0 comments on commit 9642909

Please sign in to comment.