Merge develop

This commit is contained in:
Ming Deng
2021-02-12 22:02:22 +08:00
39 changed files with 800 additions and 1923 deletions

View File

@@ -207,7 +207,7 @@ func (rc *Cache) StartAndGC(config string) error {
cf["key"] = DefaultKey
}
if _, ok := cf["conn"]; !ok {
return berror.Wrapf(err, cache.InvalidRedisCacheCfg, "config missing conn field. ", config)
return berror.Wrapf(err, cache.InvalidRedisCacheCfg, "config missing conn field: %s", config)
}
// Format redis://<password>@<host>:<port>

View File

@@ -35,96 +35,74 @@ func TestRedisCache(t *testing.T) {
}
bm, err := cache.NewCache("redis", fmt.Sprintf(`{"conn": "%s"}`, redisAddr))
if err != nil {
t.Error("init err")
}
timeoutDuration := 10 * time.Second
if err = bm.Put(context.Background(), "astaxie", 1, timeoutDuration); err != nil {
t.Error("set Error", err)
}
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
t.Error("check err")
}
assert.Nil(t, err)
timeoutDuration := 3 * time.Second
time.Sleep(11 * time.Second)
assert.Nil(t, bm.Put(context.Background(), "astaxie", 1, timeoutDuration))
res, _ := bm.IsExist(context.Background(), "astaxie")
assert.True(t, res)
time.Sleep(5 * time.Second)
res, _ = bm.IsExist(context.Background(), "astaxie")
assert.False(t, res)
assert.Nil(t, bm.Put(context.Background(), "astaxie", 1, timeoutDuration))
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
t.Error("check err")
}
if err = bm.Put(context.Background(), "astaxie", 1, timeoutDuration); err != nil {
t.Error("set Error", err)
}
val, _ := bm.Get(context.Background(), "astaxie")
if v, _ := redis.Int(val, err); v != 1 {
t.Error("get err")
}
v, _ := redis.Int(val, err)
assert.Equal(t, 1, v)
if err = bm.Incr(context.Background(), "astaxie"); err != nil {
t.Error("Incr Error", err)
}
assert.Nil(t, bm.Incr(context.Background(), "astaxie"))
val, _ = bm.Get(context.Background(), "astaxie")
if v, _ := redis.Int(val, err); v != 2 {
t.Error("get err")
}
v, _ = redis.Int(val, err)
assert.Equal(t, 2, v)
if err = bm.Decr(context.Background(), "astaxie"); err != nil {
t.Error("Decr Error", err)
}
assert.Nil(t, bm.Decr(context.Background(), "astaxie"))
val, _ = bm.Get(context.Background(), "astaxie")
if v, _ := redis.Int(val, err); v != 1 {
t.Error("get err")
}
v, _ = redis.Int(val, err)
assert.Equal(t, 1, v)
bm.Delete(context.Background(), "astaxie")
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
t.Error("delete err")
}
res, _ = bm.IsExist(context.Background(), "astaxie")
assert.False(t, res)
assert.Nil(t, bm.Put(context.Background(), "astaxie", "author", timeoutDuration))
// test string
if err = bm.Put(context.Background(), "astaxie", "author", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
t.Error("check err")
}
res, _ = bm.IsExist(context.Background(), "astaxie")
assert.True(t, res)
val, _ = bm.Get(context.Background(), "astaxie")
if v, _ := redis.String(val, err); v != "author" {
t.Error("get err")
}
vs, _ := redis.String(val, err)
assert.Equal(t, "author", vs)
// test GetMulti
if err = bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if res, _ := bm.IsExist(context.Background(), "astaxie1"); !res {
t.Error("check err")
}
assert.Nil(t, bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration))
res, _ = bm.IsExist(context.Background(), "astaxie1")
assert.True(t, res)
vv, _ := bm.GetMulti(context.Background(), []string{"astaxie", "astaxie1"})
if len(vv) != 2 {
t.Error("GetMulti ERROR")
}
if v, _ := redis.String(vv[0], nil); v != "author" {
t.Error("GetMulti ERROR")
}
if v, _ := redis.String(vv[1], nil); v != "author1" {
t.Error("GetMulti ERROR")
}
assert.Equal(t, 2, len(vv))
vs, _ = redis.String(vv[0], nil)
assert.Equal(t, "author", vs)
vs, _ = redis.String(vv[1], nil)
assert.Equal(t, "author1", vs)
vv, _ = bm.GetMulti(context.Background(), []string{"astaxie0", "astaxie1"})
if vv[0] != nil {
t.Error("GetMulti ERROR")
}
if v, _ := redis.String(vv[1], nil); v != "author1" {
t.Error("GetMulti ERROR")
}
assert.Nil(t, vv[0])
vs, _ = redis.String(vv[1], nil)
assert.Equal(t, "author1", vs)
// test clear all
if err = bm.ClearAll(context.Background()); err != nil {
t.Error("clear all err")
}
assert.Nil(t, bm.ClearAll(context.Background()))
}
func TestCache_Scan(t *testing.T) {
@@ -137,35 +115,24 @@ func TestCache_Scan(t *testing.T) {
// init
bm, err := cache.NewCache("redis", fmt.Sprintf(`{"conn": "%s"}`, addr))
if err != nil {
t.Error("init err")
}
assert.Nil(t, err)
// insert all
for i := 0; i < 100; i++ {
if err = bm.Put(context.Background(), fmt.Sprintf("astaxie%d", i), fmt.Sprintf("author%d", i), timeoutDuration); err != nil {
t.Error("set Error", err)
}
assert.Nil(t, bm.Put(context.Background(), fmt.Sprintf("astaxie%d", i), fmt.Sprintf("author%d", i), timeoutDuration))
}
time.Sleep(time.Second)
// scan all for the first time
keys, err := bm.(*Cache).Scan(DefaultKey + ":*")
if err != nil {
t.Error("scan Error", err)
}
assert.Nil(t, err)
assert.Equal(t, 100, len(keys), "scan all error")
// clear all
if err = bm.ClearAll(context.Background()); err != nil {
t.Error("clear all err")
}
assert.Nil(t, bm.ClearAll(context.Background()))
// scan all for the second time
keys, err = bm.(*Cache).Scan(DefaultKey + ":*")
if err != nil {
t.Error("scan Error", err)
}
if len(keys) != 0 {
t.Error("scan all err")
}
assert.Nil(t, err)
assert.Equal(t, 0, len(keys))
}