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

@@ -30,6 +30,7 @@
package redis
import (
"context"
"encoding/json"
"errors"
"fmt"
@@ -83,63 +84,60 @@ func (rc *Cache) associate(originKey interface{}) string {
}
// Get cache from redis.
func (rc *Cache) Get(key string) interface{} {
func (rc *Cache) Get(ctx context.Context, key string) (interface{}, error) {
if v, err := rc.do("GET", key); err == nil {
return v
return v, nil
} else {
return nil, err
}
return nil
}
// GetMulti gets cache from redis.
func (rc *Cache) GetMulti(keys []string) []interface{} {
func (rc *Cache) GetMulti(ctx context.Context, keys []string) ([]interface{}, error) {
c := rc.p.Get()
defer c.Close()
var args []interface{}
for _, key := range keys {
args = append(args, rc.associate(key))
}
values, err := redis.Values(c.Do("MGET", args...))
if err != nil {
return nil
}
return values
return redis.Values(c.Do("MGET", args...))
}
// Put puts cache into redis.
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 {
_, err := rc.do("SETEX", key, int64(timeout/time.Second), val)
return err
}
// Delete deletes a key's cache in redis.
func (rc *Cache) Delete(key string) error {
func (rc *Cache) Delete(ctx context.Context, key string) error {
_, err := rc.do("DEL", key)
return err
}
// IsExist checks cache's existence in redis.
func (rc *Cache) IsExist(key string) bool {
func (rc *Cache) IsExist(ctx context.Context, key string) (bool, error) {
v, err := redis.Bool(rc.do("EXISTS", key))
if err != nil {
return false
return false, err
}
return v
return v, nil
}
// Incr increases a key's counter in redis.
func (rc *Cache) Incr(key string) error {
func (rc *Cache) Incr(ctx context.Context, key string) error {
_, err := redis.Bool(rc.do("INCRBY", key, 1))
return err
}
// Decr decreases a key's counter in redis.
func (rc *Cache) Decr(key string) error {
func (rc *Cache) Decr(ctx context.Context, key string) error {
_, err := redis.Bool(rc.do("INCRBY", key, -1))
return err
}
// ClearAll deletes all cache in the redis collection
func (rc *Cache) ClearAll() error {
func (rc *Cache) ClearAll(context.Context) error {
cachedKeys, err := rc.Scan(rc.key + ":*")
if err != nil {
return err

View File

@@ -15,6 +15,7 @@
package redis
import (
"context"
"fmt"
"os"
"testing"
@@ -38,67 +39,70 @@ func TestRedisCache(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, _ := redis.Int(bm.Get("astaxie"), err); v != 1 {
val, _ := bm.Get(context.Background(), "astaxie")
if v, _ := redis.Int(val, err); 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, _ := redis.Int(bm.Get("astaxie"), err); v != 2 {
val, _ = bm.Get(context.Background(), "astaxie")
if v, _ := redis.Int(val, err); 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, _ := redis.Int(bm.Get("astaxie"), err); v != 1 {
val, _ = bm.Get(context.Background(), "astaxie")
if v, _ := redis.Int(val, err); 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, _ := redis.String(bm.Get("astaxie"), err); v != "author" {
val, _ = bm.Get(context.Background(), "astaxie")
if v, _ := redis.String(val, err); 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")
}
@@ -110,7 +114,7 @@ func TestRedisCache(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")
}
}
@@ -130,7 +134,7 @@ func TestCache_Scan(t *testing.T) {
}
// insert all
for i := 0; i < 100; i++ {
if err = bm.Put(fmt.Sprintf("astaxie%d", i), fmt.Sprintf("author%d", i), timeoutDuration); err != nil {
if err = bm.Put(context.Background(), fmt.Sprintf("astaxie%d", i), fmt.Sprintf("author%d", i), timeoutDuration); err != nil {
t.Error("set Error", err)
}
}
@@ -144,7 +148,7 @@ func TestCache_Scan(t *testing.T) {
assert.Equal(t, 100, len(keys), "scan all error")
// clear all
if err = bm.ClearAll(); err != nil {
if err = bm.ClearAll(context.Background()); err != nil {
t.Error("clear all err")
}