Add context to cache API

This commit is contained in:
Ming Deng
2020-10-04 22:11:28 +08:00
parent b89d9511ab
commit 3364c609de
11 changed files with 259 additions and 230 deletions

View File

@@ -1,6 +1,7 @@
package ssdb
import (
"context"
"fmt"
"os"
"strconv"
@@ -23,75 +24,78 @@ func TestSsdbcacheCache(t *testing.T) {
}
// test put and exist
if ssdb.IsExist("ssdb") {
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); res {
t.Error("check err")
}
timeoutDuration := 10 * time.Second
//timeoutDuration := -10*time.Second if timeoutDuration is negtive,it means permanent
if err = ssdb.Put("ssdb", "ssdb", timeoutDuration); err != nil {
// timeoutDuration := -10*time.Second if timeoutDuration is negtive,it means permanent
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if !ssdb.IsExist("ssdb") {
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); !res {
t.Error("check err")
}
// Get test done
if err = ssdb.Put("ssdb", "ssdb", timeoutDuration); err != nil {
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if v := ssdb.Get("ssdb"); v != "ssdb" {
if v, _ := ssdb.Get(context.Background(), "ssdb"); v != "ssdb" {
t.Error("get Error")
}
//inc/dec test done
if err = ssdb.Put("ssdb", "2", timeoutDuration); err != nil {
// inc/dec test done
if err = ssdb.Put(context.Background(), "ssdb", "2", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if err = ssdb.Incr("ssdb"); err != nil {
if err = ssdb.Incr(context.Background(), "ssdb"); err != nil {
t.Error("incr Error", err)
}
if v, err := strconv.Atoi(ssdb.Get("ssdb").(string)); err != nil || v != 3 {
val, _ := ssdb.Get(context.Background(), "ssdb")
if v, err := strconv.Atoi(val.(string)); err != nil || v != 3 {
t.Error("get err")
}
if err = ssdb.Decr("ssdb"); err != nil {
if err = ssdb.Decr(context.Background(), "ssdb"); err != nil {
t.Error("decr error")
}
// test del
if err = ssdb.Put("ssdb", "3", timeoutDuration); err != nil {
if err = ssdb.Put(context.Background(), "ssdb", "3", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if v, err := strconv.Atoi(ssdb.Get("ssdb").(string)); err != nil || v != 3 {
val, _ = ssdb.Get(context.Background(), "ssdb")
if v, err := strconv.Atoi(val.(string)); err != nil || v != 3 {
t.Error("get err")
}
if err := ssdb.Delete("ssdb"); err == nil {
if ssdb.IsExist("ssdb") {
if err := ssdb.Delete(context.Background(), "ssdb"); err == nil {
if e, _ := ssdb.IsExist(context.Background(), "ssdb"); e {
t.Error("delete err")
}
}
//test string
if err = ssdb.Put("ssdb", "ssdb", -10*time.Second); err != nil {
// test string
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", -10*time.Second); err != nil {
t.Error("set Error", err)
}
if !ssdb.IsExist("ssdb") {
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); !res {
t.Error("check err")
}
if v := ssdb.Get("ssdb").(string); v != "ssdb" {
if v, _ := ssdb.Get(context.Background(), "ssdb"); v.(string) != "ssdb" {
t.Error("get err")
}
//test GetMulti done
if err = ssdb.Put("ssdb1", "ssdb1", -10*time.Second); err != nil {
// test GetMulti done
if err = ssdb.Put(context.Background(), "ssdb1", "ssdb1", -10*time.Second); err != nil {
t.Error("set Error", err)
}
if !ssdb.IsExist("ssdb1") {
if res, _ := ssdb.IsExist(context.Background(), "ssdb1"); !res {
t.Error("check err")
}
vv := ssdb.GetMulti([]string{"ssdb", "ssdb1"})
vv, _ := ssdb.GetMulti(context.Background(), []string{"ssdb", "ssdb1"})
if len(vv) != 2 {
t.Error("getmulti error")
}
@@ -103,10 +107,12 @@ func TestSsdbcacheCache(t *testing.T) {
}
// test clear all done
if err = ssdb.ClearAll(); err != nil {
if err = ssdb.ClearAll(context.Background()); err != nil {
t.Error("clear all err")
}
if ssdb.IsExist("ssdb") || ssdb.IsExist("ssdb1") {
e1, _ := ssdb.IsExist(context.Background(), "ssdb")
e2, _ := ssdb.IsExist(context.Background(), "ssdb1")
if e1 || e2 {
t.Error("check err")
}
}