chore: refactor cache (#1532)
This commit is contained in:
120
core/stores/cache/cache_test.go
vendored
120
core/stores/cache/cache_test.go
vendored
@@ -26,6 +26,10 @@ type mockedNode struct {
|
||||
}
|
||||
|
||||
func (mc *mockedNode) Del(keys ...string) error {
|
||||
return mc.DelCtx(context.Background(), keys...)
|
||||
}
|
||||
|
||||
func (mc *mockedNode) DelCtx(_ context.Context, keys ...string) error {
|
||||
var be errorx.BatchError
|
||||
|
||||
for _, key := range keys {
|
||||
@@ -39,10 +43,14 @@ func (mc *mockedNode) Del(keys ...string) error {
|
||||
return be.Err()
|
||||
}
|
||||
|
||||
func (mc *mockedNode) Get(key string, v interface{}) error {
|
||||
func (mc *mockedNode) Get(key string, val interface{}) error {
|
||||
return mc.GetCtx(context.Background(), key, val)
|
||||
}
|
||||
|
||||
func (mc *mockedNode) GetCtx(ctx context.Context, key string, val interface{}) error {
|
||||
bs, ok := mc.vals[key]
|
||||
if ok {
|
||||
return json.Unmarshal(bs, v)
|
||||
return json.Unmarshal(bs, val)
|
||||
}
|
||||
|
||||
return mc.errNotFound
|
||||
@@ -52,8 +60,12 @@ func (mc *mockedNode) IsNotFound(err error) bool {
|
||||
return errors.Is(err, mc.errNotFound)
|
||||
}
|
||||
|
||||
func (mc *mockedNode) Set(key string, v interface{}) error {
|
||||
data, err := json.Marshal(v)
|
||||
func (mc *mockedNode) Set(key string, val interface{}) error {
|
||||
return mc.SetCtx(context.Background(), key, val)
|
||||
}
|
||||
|
||||
func (mc *mockedNode) SetCtx(ctx context.Context, key string, val interface{}) error {
|
||||
data, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -62,52 +74,40 @@ func (mc *mockedNode) Set(key string, v interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mc *mockedNode) SetWithExpire(key string, v interface{}, _ time.Duration) error {
|
||||
return mc.Set(key, v)
|
||||
func (mc *mockedNode) SetWithExpire(key string, val interface{}, expire time.Duration) error {
|
||||
return mc.SetWithExpireCtx(context.Background(), key, val, expire)
|
||||
}
|
||||
|
||||
func (mc *mockedNode) Take(v interface{}, key string, query func(v interface{}) error) error {
|
||||
func (mc *mockedNode) SetWithExpireCtx(ctx context.Context, key string, val interface{}, expire time.Duration) error {
|
||||
return mc.Set(key, val)
|
||||
}
|
||||
|
||||
func (mc *mockedNode) Take(val interface{}, key string, query func(val interface{}) error) error {
|
||||
return mc.TakeCtx(context.Background(), val, key, query)
|
||||
}
|
||||
|
||||
func (mc *mockedNode) TakeCtx(ctx context.Context, val interface{}, key string, query func(val interface{}) error) error {
|
||||
if _, ok := mc.vals[key]; ok {
|
||||
return mc.Get(key, v)
|
||||
return mc.GetCtx(ctx, key, val)
|
||||
}
|
||||
|
||||
if err := query(v); err != nil {
|
||||
if err := query(val); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return mc.Set(key, v)
|
||||
return mc.SetCtx(ctx, key, val)
|
||||
}
|
||||
|
||||
func (mc *mockedNode) TakeWithExpire(v interface{}, key string, query func(v interface{}, expire time.Duration) error) error {
|
||||
return mc.Take(v, key, func(v interface{}) error {
|
||||
return query(v, 0)
|
||||
func (mc *mockedNode) TakeWithExpire(val interface{}, key string, query func(val interface{}, expire time.Duration) error) error {
|
||||
return mc.TakeWithExpireCtx(context.Background(), val, key, query)
|
||||
}
|
||||
|
||||
func (mc *mockedNode) TakeWithExpireCtx(ctx context.Context, val interface{}, key string, query func(val interface{}, expire time.Duration) error) error {
|
||||
return mc.Take(val, key, func(val interface{}) error {
|
||||
return query(val, 0)
|
||||
})
|
||||
}
|
||||
|
||||
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()
|
||||
@@ -141,18 +141,18 @@ func TestCache_SetDel(t *testing.T) {
|
||||
}
|
||||
}
|
||||
for i := 0; i < total; i++ {
|
||||
var v int
|
||||
assert.Nil(t, c.Get(fmt.Sprintf("key/%d", i), &v))
|
||||
assert.Equal(t, i, v)
|
||||
var val int
|
||||
assert.Nil(t, c.Get(fmt.Sprintf("key/%d", i), &val))
|
||||
assert.Equal(t, i, val)
|
||||
}
|
||||
assert.Nil(t, c.Del())
|
||||
for i := 0; i < total; i++ {
|
||||
assert.Nil(t, c.Del(fmt.Sprintf("key/%d", i)))
|
||||
}
|
||||
for i := 0; i < total; i++ {
|
||||
var v int
|
||||
assert.True(t, c.IsNotFound(c.Get(fmt.Sprintf("key/%d", i), &v)))
|
||||
assert.Equal(t, 0, v)
|
||||
var val int
|
||||
assert.True(t, c.IsNotFound(c.Get(fmt.Sprintf("key/%d", i), &val)))
|
||||
assert.Equal(t, 0, val)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,18 +179,18 @@ func TestCache_OneNode(t *testing.T) {
|
||||
}
|
||||
}
|
||||
for i := 0; i < total; i++ {
|
||||
var v int
|
||||
assert.Nil(t, c.Get(fmt.Sprintf("key/%d", i), &v))
|
||||
assert.Equal(t, i, v)
|
||||
var val int
|
||||
assert.Nil(t, c.Get(fmt.Sprintf("key/%d", i), &val))
|
||||
assert.Equal(t, i, val)
|
||||
}
|
||||
assert.Nil(t, c.Del())
|
||||
for i := 0; i < total; i++ {
|
||||
assert.Nil(t, c.Del(fmt.Sprintf("key/%d", i)))
|
||||
}
|
||||
for i := 0; i < total; i++ {
|
||||
var v int
|
||||
assert.True(t, c.IsNotFound(c.Get(fmt.Sprintf("key/%d", i), &v)))
|
||||
assert.Equal(t, 0, v)
|
||||
var val int
|
||||
assert.True(t, c.IsNotFound(c.Get(fmt.Sprintf("key/%d", i), &val)))
|
||||
assert.Equal(t, 0, val)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,9 +230,9 @@ func TestCache_Balance(t *testing.T) {
|
||||
assert.True(t, entropy > .95, fmt.Sprintf("entropy should be greater than 0.95, but got %.2f", entropy))
|
||||
|
||||
for i := 0; i < total; i++ {
|
||||
var v int
|
||||
assert.Nil(t, c.Get(strconv.Itoa(i), &v))
|
||||
assert.Equal(t, i, v)
|
||||
var val int
|
||||
assert.Nil(t, c.Get(strconv.Itoa(i), &val))
|
||||
assert.Equal(t, i, val)
|
||||
}
|
||||
|
||||
for i := 0; i < total/10; i++ {
|
||||
@@ -244,14 +244,14 @@ func TestCache_Balance(t *testing.T) {
|
||||
for i := 0; i < total/10; i++ {
|
||||
var val int
|
||||
if i%2 == 0 {
|
||||
assert.Nil(t, c.Take(&val, strconv.Itoa(i*10), func(v interface{}) error {
|
||||
*v.(*int) = i
|
||||
assert.Nil(t, c.Take(&val, strconv.Itoa(i*10), func(val interface{}) error {
|
||||
*val.(*int) = i
|
||||
count++
|
||||
return nil
|
||||
}))
|
||||
} else {
|
||||
assert.Nil(t, c.TakeWithExpire(&val, strconv.Itoa(i*10), func(v interface{}, expire time.Duration) error {
|
||||
*v.(*int) = i
|
||||
assert.Nil(t, c.TakeWithExpire(&val, strconv.Itoa(i*10), func(val interface{}, expire time.Duration) error {
|
||||
*val.(*int) = i
|
||||
count++
|
||||
return nil
|
||||
}))
|
||||
@@ -272,10 +272,10 @@ func TestCacheNoNode(t *testing.T) {
|
||||
assert.NotNil(t, c.Get("foo", nil))
|
||||
assert.NotNil(t, c.Set("foo", nil))
|
||||
assert.NotNil(t, c.SetWithExpire("foo", nil, time.Second))
|
||||
assert.NotNil(t, c.Take(nil, "foo", func(v interface{}) error {
|
||||
assert.NotNil(t, c.Take(nil, "foo", func(val interface{}) error {
|
||||
return nil
|
||||
}))
|
||||
assert.NotNil(t, c.TakeWithExpire(nil, "foo", func(v interface{}, duration time.Duration) error {
|
||||
assert.NotNil(t, c.TakeWithExpire(nil, "foo", func(val interface{}, duration time.Duration) error {
|
||||
return nil
|
||||
}))
|
||||
}
|
||||
@@ -283,8 +283,8 @@ func TestCacheNoNode(t *testing.T) {
|
||||
func calcEntropy(m map[int]int, total int) float64 {
|
||||
var entropy float64
|
||||
|
||||
for _, v := range m {
|
||||
proba := float64(v) / float64(total)
|
||||
for _, val := range m {
|
||||
proba := float64(val) / float64(total)
|
||||
entropy -= proba * math.Log2(proba)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user