1
0
mirror of https://github.com/astaxie/beego.git synced 2025-08-02 19:05:30 +00:00

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

@@ -30,6 +30,7 @@
package memcache
import (
"context"
"encoding/json"
"errors"
"strings"
@@ -52,28 +53,25 @@ func NewMemCache() cache.Cache {
}
// Get get value from memcache.
func (rc *Cache) Get(key string) interface{} {
func (rc *Cache) Get(ctx context.Context, key string) (interface{}, error) {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
return nil, err
}
}
if item, err := rc.conn.Get(key); err == nil {
return item.Value
return item.Value, nil
} else {
return nil, err
}
return nil
}
// GetMulti gets a value from a key in memcache.
func (rc *Cache) GetMulti(keys []string) []interface{} {
size := len(keys)
func (rc *Cache) GetMulti(ctx context.Context, keys []string) ([]interface{}, error) {
var rv []interface{}
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
for i := 0; i < size; i++ {
rv = append(rv, err)
}
return rv
return rv, err
}
}
mv, err := rc.conn.GetMulti(keys)
@@ -81,16 +79,12 @@ func (rc *Cache) GetMulti(keys []string) []interface{} {
for _, v := range mv {
rv = append(rv, v.Value)
}
return rv
}
for i := 0; i < size; i++ {
rv = append(rv, err)
}
return rv
return rv, err
}
// Put puts a value into memcache.
func (rc *Cache) Put(key string, val interface{}, timeout time.Duration) error {
func (rc *Cache) Put(ctx context.Context, key string, val interface{}, timeout time.Duration) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
@@ -108,7 +102,7 @@ func (rc *Cache) Put(key string, val interface{}, timeout time.Duration) error {
}
// Delete deletes a value in memcache.
func (rc *Cache) Delete(key string) error {
func (rc *Cache) Delete(ctx context.Context, key string) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
@@ -118,7 +112,7 @@ func (rc *Cache) Delete(key string) error {
}
// Incr increases counter.
func (rc *Cache) Incr(key string) error {
func (rc *Cache) Incr(ctx context.Context, key string) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
@@ -129,7 +123,7 @@ func (rc *Cache) Incr(key string) error {
}
// Decr decreases counter.
func (rc *Cache) Decr(key string) error {
func (rc *Cache) Decr(ctx context.Context, key string) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
@@ -140,18 +134,18 @@ func (rc *Cache) Decr(key string) error {
}
// IsExist checks if a value exists in memcache.
func (rc *Cache) IsExist(key string) bool {
func (rc *Cache) IsExist(ctx context.Context, key string) (bool, error) {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return false
return false, err
}
}
_, err := rc.conn.Get(key)
return err == nil
return err == nil, err
}
// ClearAll clears all cache in memcache.
func (rc *Cache) ClearAll() error {
func (rc *Cache) ClearAll(context.Context) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err

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")
}
}