- move Cacher from globals to api
- vendorPromotionID for CreatePromotion - LockPromotionSkus
This commit is contained in:
19
business/jxutils/cache/redis/redis.go
vendored
19
business/jxutils/cache/redis/redis.go
vendored
@@ -25,8 +25,19 @@ func New(host string, port int, password string) *Cacher {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cacher) FlushAll() error {
|
||||
return c.client.FlushAll().Err()
|
||||
func (c *Cacher) FlushKeys(prefix string) (err error) {
|
||||
if prefix == "" {
|
||||
return c.client.FlushAll().Err()
|
||||
}
|
||||
keys, err := c.Keys(prefix)
|
||||
if err == nil {
|
||||
for _, key := range keys {
|
||||
if err = c.Del(key); err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Cacher) Set(key string, value interface{}, expiration time.Duration) error {
|
||||
@@ -58,3 +69,7 @@ func (c *Cacher) GetAs(key string, ptr interface{}) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Cacher) Keys(prefix string) ([]string, error) {
|
||||
return c.client.Keys(prefix + "*").Result()
|
||||
}
|
||||
|
||||
45
business/jxutils/cache/redis/redis_test.go
vendored
45
business/jxutils/cache/redis/redis_test.go
vendored
@@ -3,12 +3,16 @@ package redis
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/cache"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
"git.rosy.net.cn/jx-callback/globals/beegodb"
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
var (
|
||||
cacher cache.ICacher
|
||||
)
|
||||
|
||||
func init() {
|
||||
//E:/goprojects/src/git.rosy.net.cn/jx-callback/conf/app.conf
|
||||
///Users/xujianhua/go/src/git.rosy.net.cn/jx-callback/conf/app.conf
|
||||
@@ -17,7 +21,9 @@ func init() {
|
||||
|
||||
globals.Init()
|
||||
beegodb.Init()
|
||||
api.Init()
|
||||
// api.Init()
|
||||
|
||||
cacher = New("localhost", 6379, "")
|
||||
}
|
||||
|
||||
type FuckYou struct {
|
||||
@@ -25,8 +31,7 @@ type FuckYou struct {
|
||||
}
|
||||
|
||||
func TestIt(t *testing.T) {
|
||||
cacher := New("localhost", 6379, "")
|
||||
cacher.FlushAll()
|
||||
cacher.FlushKeys("")
|
||||
if cacher.Get("key") != nil {
|
||||
t.Fatal("should get nothing")
|
||||
}
|
||||
@@ -46,3 +51,35 @@ func TestIt(t *testing.T) {
|
||||
t.Fatal("should get value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeys(t *testing.T) {
|
||||
cacher.FlushKeys("")
|
||||
keys, err := cacher.Keys("")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(keys) != 0 {
|
||||
t.Fatal("keys should be empty here")
|
||||
}
|
||||
cacher.Set("key1", 1, 0)
|
||||
cacher.Set("key2", 2, 0)
|
||||
cacher.Set("key22", 3, 0)
|
||||
cacher.Set("key23", 4, 0)
|
||||
cacher.Set("key24", 5, 0)
|
||||
keys, err = cacher.Keys("")
|
||||
if len(keys) != 5 {
|
||||
t.Fatal("keys should be 5 here")
|
||||
}
|
||||
keys, err = cacher.Keys("key")
|
||||
if len(keys) != 5 {
|
||||
t.Fatal("keys should be 5 here")
|
||||
}
|
||||
keys, err = cacher.Keys("key2")
|
||||
if len(keys) != 4 {
|
||||
t.Fatal("keys should be 4 here")
|
||||
}
|
||||
keys, err = cacher.Keys("key22")
|
||||
if len(keys) != 1 {
|
||||
t.Fatal("keys should be 1 here")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user