1
0
mirror of https://github.com/astaxie/beego.git synced 2025-08-02 17:45:31 +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

@@ -1,6 +1,7 @@
package ssdb
import (
"context"
"encoding/json"
"errors"
"strconv"
@@ -24,29 +25,26 @@ func NewSsdbCache() cache.Cache {
}
// Get gets a key's 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 nil
return nil, nil
}
}
value, err := rc.conn.Get(key)
if err == nil {
return value
return value, nil
}
return nil
return nil, nil
}
// GetMulti gets one or keys values from memcache.
func (rc *Cache) GetMulti(keys []string) []interface{} {
// GetMulti gets one or keys values from ssdb.
func (rc *Cache) GetMulti(ctx context.Context, keys []string) ([]interface{}, error) {
size := len(keys)
var values []interface{}
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
for i := 0; i < size; i++ {
values = append(values, err)
}
return values
return values, err
}
}
res, err := rc.conn.Do("multi_get", keys)
@@ -55,12 +53,12 @@ func (rc *Cache) GetMulti(keys []string) []interface{} {
for i := 1; i < resSize; i += 2 {
values = append(values, res[i+1])
}
return values
return values, nil
}
for i := 0; i < size; i++ {
values = append(values, err)
}
return values
return values, nil
}
// DelMulti deletes one or more keys from memcache
@@ -76,13 +74,13 @@ func (rc *Cache) DelMulti(keys []string) error {
// Put puts value into memcache.
// value: must be of type string
func (rc *Cache) Put(key string, value 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
}
}
v, ok := value.(string)
v, ok := val.(string)
if !ok {
return errors.New("value must string")
}
@@ -104,7 +102,7 @@ func (rc *Cache) Put(key string, value 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
@@ -115,7 +113,7 @@ func (rc *Cache) Delete(key string) error {
}
// Incr increases a key's 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
@@ -126,7 +124,7 @@ func (rc *Cache) Incr(key string) error {
}
// Decr decrements a key's 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
@@ -137,25 +135,25 @@ func (rc *Cache) Decr(key string) error {
}
// IsExist checks if a key 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
}
}
resp, err := rc.conn.Do("exists", key)
if err != nil {
return false
return false, err
}
if len(resp) == 2 && resp[1] == "1" {
return true
return true, nil
}
return false
return false, nil
}
// ClearAll clears all cached items 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

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