Files
jx-callback/business/jxutils/cache/redis/redis_test.go
2019-03-20 22:28:51 +08:00

76 lines
1.4 KiB
Go

package redis
import (
"testing"
"git.rosy.net.cn/jx-callback/business/jxutils/cache"
"git.rosy.net.cn/jx-callback/globals/testinit0"
)
var (
cacher cache.ICacher
)
func init() {
testinit0.Init()
cacher = New("localhost", 6379, "")
}
type FuckYou struct {
Key string `json:"key"`
}
func TestIt(t *testing.T) {
cacher.FlushKeys("")
if cacher.Get("key") != nil {
t.Fatal("should get nothing")
}
if err := cacher.Set("key", map[string]interface{}{
"key": "value",
}, 0); err != nil {
t.Fatal(err)
}
if cacher.Get("key") == nil {
t.Fatal("should not get nothing")
}
result := new(FuckYou)
cacher.GetAs("key", result)
if result.Key != "value" {
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")
}
}