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

@@ -15,15 +15,15 @@
package memcache
import (
"context"
"fmt"
"os"
_ "github.com/bradfitz/gomemcache/memcache"
"strconv"
"testing"
"time"
_ "github.com/bradfitz/gomemcache/memcache"
"github.com/astaxie/beego/pkg/client/cache"
)
@@ -39,67 +39,71 @@ func TestMemcacheCache(t *testing.T) {
t.Error("init err")
}
timeoutDuration := 10 * time.Second
if err = bm.Put("astaxie", "1", timeoutDuration); err != nil {
if err = bm.Put(context.Background(), "astaxie", "1", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if !bm.IsExist("astaxie") {
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
t.Error("check err")
}
time.Sleep(11 * time.Second)
if bm.IsExist("astaxie") {
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
t.Error("check err")
}
if err = bm.Put("astaxie", "1", timeoutDuration); err != nil {
if err = bm.Put(context.Background(), "astaxie", "1", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if v, err := strconv.Atoi(string(bm.Get("astaxie").([]byte))); err != nil || v != 1 {
val, _ := bm.Get(context.Background(), "astaxie")
if v, err := strconv.Atoi(string(val.([]byte))); err != nil || v != 1 {
t.Error("get err")
}
if err = bm.Incr("astaxie"); err != nil {
if err = bm.Incr(context.Background(), "astaxie"); err != nil {
t.Error("Incr Error", err)
}
if v, err := strconv.Atoi(string(bm.Get("astaxie").([]byte))); err != nil || v != 2 {
val, _ = bm.Get(context.Background(), "astaxie")
if v, err := strconv.Atoi(string(val.([]byte))); err != nil || v != 2 {
t.Error("get err")
}
if err = bm.Decr("astaxie"); err != nil {
if err = bm.Decr(context.Background(), "astaxie"); err != nil {
t.Error("Decr Error", err)
}
if v, err := strconv.Atoi(string(bm.Get("astaxie").([]byte))); err != nil || v != 1 {
val, _ = bm.Get(context.Background(), "astaxie")
if v, err := strconv.Atoi(string(val.([]byte))); err != nil || v != 1 {
t.Error("get err")
}
bm.Delete("astaxie")
if bm.IsExist("astaxie") {
bm.Delete(context.Background(), "astaxie")
if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
t.Error("delete err")
}
// test string
if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
if err = bm.Put(context.Background(), "astaxie", "author", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if !bm.IsExist("astaxie") {
if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
t.Error("check err")
}
if v := bm.Get("astaxie").([]byte); string(v) != "author" {
val, _ = bm.Get(context.Background(), "astaxie")
if v := val.([]byte); string(v) != "author" {
t.Error("get err")
}
// test GetMulti
if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
if err = bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if !bm.IsExist("astaxie1") {
if res, _ := bm.IsExist(context.Background(), "astaxie1"); !res {
t.Error("check err")
}
vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
vv, _ := bm.GetMulti(context.Background(), []string{"astaxie", "astaxie1"})
if len(vv) != 2 {
t.Error("GetMulti ERROR")
}
@@ -111,7 +115,7 @@ func TestMemcacheCache(t *testing.T) {
}
// test clear all
if err = bm.ClearAll(); err != nil {
if err = bm.ClearAll(context.Background()); err != nil {
t.Error("clear all err")
}
}