feature extend readthrough for cache module (#5116)
* feature 增加readthrough
This commit is contained in:
118
client/cache/memcache/memcache_test.go
vendored
118
client/cache/memcache/memcache_test.go
vendored
@@ -16,6 +16,7 @@ package memcache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -23,6 +24,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/beego/beego/v2/core/berror"
|
||||
|
||||
_ "github.com/bradfitz/gomemcache/memcache"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
@@ -69,7 +72,7 @@ func TestMemcacheCache(t *testing.T) {
|
||||
v, err = strconv.Atoi(string(val.([]byte)))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1, v)
|
||||
bm.Delete(context.Background(), "astaxie")
|
||||
assert.Nil(t, bm.Delete(context.Background(), "astaxie"))
|
||||
|
||||
res, _ = bm.IsExist(context.Background(), "astaxie")
|
||||
assert.False(t, res)
|
||||
@@ -111,3 +114,116 @@ func TestMemcacheCache(t *testing.T) {
|
||||
assert.Nil(t, bm.ClearAll(context.Background()))
|
||||
// test clear all
|
||||
}
|
||||
|
||||
func TestReadThroughCache_Memcache_Get(t *testing.T) {
|
||||
bm, err := cache.NewCache("memcache", fmt.Sprintf(`{"conn": "%s"}`, "127.0.0.1:11211"))
|
||||
assert.Nil(t, err)
|
||||
|
||||
testReadThroughCacheGet(t, bm)
|
||||
}
|
||||
|
||||
func testReadThroughCacheGet(t *testing.T, bm cache.Cache) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
key string
|
||||
value string
|
||||
cache cache.Cache
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "Get load err",
|
||||
key: "key0",
|
||||
cache: func() cache.Cache {
|
||||
kvs := map[string]any{"key0": "value0"}
|
||||
db := &MockOrm{kvs: kvs}
|
||||
loadfunc := func(ctx context.Context, key string) (any, error) {
|
||||
v, er := db.Load(key)
|
||||
if er != nil {
|
||||
return nil, er
|
||||
}
|
||||
val := []byte(v.(string))
|
||||
return val, nil
|
||||
}
|
||||
c, err := cache.NewReadThroughCache(bm, 3*time.Second, loadfunc)
|
||||
assert.Nil(t, err)
|
||||
return c
|
||||
}(),
|
||||
wantErr: func() error {
|
||||
err := errors.New("the key not exist")
|
||||
return berror.Wrap(
|
||||
err, cache.LoadFuncFailed, "cache unable to load data")
|
||||
}(),
|
||||
},
|
||||
{
|
||||
name: "Get cache exist",
|
||||
key: "key1",
|
||||
value: "value1",
|
||||
cache: func() cache.Cache {
|
||||
keysMap := map[string]int{"key1": 1}
|
||||
kvs := map[string]any{"key1": "value1"}
|
||||
db := &MockOrm{keysMap: keysMap, kvs: kvs}
|
||||
loadfunc := func(ctx context.Context, key string) (any, error) {
|
||||
v, er := db.Load(key)
|
||||
if er != nil {
|
||||
return nil, er
|
||||
}
|
||||
val := []byte(v.(string))
|
||||
return val, nil
|
||||
}
|
||||
c, err := cache.NewReadThroughCache(bm, 3*time.Second, loadfunc)
|
||||
assert.Nil(t, err)
|
||||
err = c.Put(context.Background(), "key1", "value1", 3*time.Second)
|
||||
assert.Nil(t, err)
|
||||
return c
|
||||
}(),
|
||||
},
|
||||
{
|
||||
name: "Get loadFunc exist",
|
||||
key: "key2",
|
||||
value: "value2",
|
||||
cache: func() cache.Cache {
|
||||
keysMap := map[string]int{"key2": 1}
|
||||
kvs := map[string]any{"key2": "value2"}
|
||||
db := &MockOrm{keysMap: keysMap, kvs: kvs}
|
||||
loadfunc := func(ctx context.Context, key string) (any, error) {
|
||||
v, er := db.Load(key)
|
||||
if er != nil {
|
||||
return nil, er
|
||||
}
|
||||
val := []byte(v.(string))
|
||||
return val, nil
|
||||
}
|
||||
c, err := cache.NewReadThroughCache(bm, 3*time.Second, loadfunc)
|
||||
assert.Nil(t, err)
|
||||
return c
|
||||
}(),
|
||||
},
|
||||
}
|
||||
_, err := cache.NewReadThroughCache(bm, 3*time.Second, nil)
|
||||
assert.Equal(t, berror.Error(cache.InvalidLoadFunc, "loadFunc cannot be nil"), err)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
bs := []byte(tc.value)
|
||||
c := tc.cache
|
||||
val, err := c.Get(context.Background(), tc.key)
|
||||
if err != nil {
|
||||
assert.EqualError(t, tc.wantErr, err.Error())
|
||||
return
|
||||
}
|
||||
assert.Equal(t, bs, val)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type MockOrm struct {
|
||||
keysMap map[string]int
|
||||
kvs map[string]any
|
||||
}
|
||||
|
||||
func (m *MockOrm) Load(key string) (any, error) {
|
||||
_, ok := m.keysMap[key]
|
||||
if !ok {
|
||||
return nil, errors.New("the key not exist")
|
||||
}
|
||||
return m.kvs[key], nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user