feat: support ctx in Cache (#1518)

* feature: support ctx in `Cache`

Signed-off-by: chenquan <chenquan.dev@foxmail.com>

* fix: `errors.Is` instead of `=`

Signed-off-by: chenquan <chenquan.dev@foxmail.com>
This commit is contained in:
chenquan
2022-02-13 03:28:14 -06:00
committed by GitHub
parent 84ddc660c4
commit e8c307e4dc
3 changed files with 169 additions and 36 deletions

View File

@@ -1,7 +1,9 @@
package cache
import (
"context"
"encoding/json"
"errors"
"fmt"
"math"
"strconv"
@@ -16,6 +18,8 @@ import (
"github.com/zeromicro/go-zero/core/syncx"
)
var _ Cache = (*mockedNode)(nil)
type mockedNode struct {
vals map[string][]byte
errNotFound error
@@ -45,7 +49,7 @@ func (mc *mockedNode) Get(key string, v interface{}) error {
}
func (mc *mockedNode) IsNotFound(err error) bool {
return err == mc.errNotFound
return errors.Is(err, mc.errNotFound)
}
func (mc *mockedNode) Set(key string, v interface{}) error {
@@ -58,7 +62,7 @@ func (mc *mockedNode) Set(key string, v interface{}) error {
return nil
}
func (mc *mockedNode) SetWithExpire(key string, v interface{}, expire time.Duration) error {
func (mc *mockedNode) SetWithExpire(key string, v interface{}, _ time.Duration) error {
return mc.Set(key, v)
}
@@ -80,6 +84,30 @@ func (mc *mockedNode) TakeWithExpire(v interface{}, key string, query func(v int
})
}
func (mc *mockedNode) DelCtx(_ context.Context, keys ...string) error {
return mc.Del(keys...)
}
func (mc *mockedNode) GetCtx(_ context.Context, key string, v interface{}) error {
return mc.Get(key, v)
}
func (mc *mockedNode) SetCtx(_ context.Context, key string, v interface{}) error {
return mc.Set(key, v)
}
func (mc *mockedNode) SetWithExpireCtx(_ context.Context, key string, v interface{}, expire time.Duration) error {
return mc.SetWithExpire(key, v, expire)
}
func (mc *mockedNode) TakeCtx(_ context.Context, v interface{}, key string, query func(v interface{}) error) error {
return mc.Take(v, key, query)
}
func (mc *mockedNode) TakeWithExpireCtx(_ context.Context, v interface{}, key string, query func(v interface{}, expire time.Duration) error) error {
return mc.TakeWithExpire(v, key, query)
}
func TestCache_SetDel(t *testing.T) {
const total = 1000
r1, clean1, err := redistest.CreateRedis()