2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-07-05 14:27:31 +00:00
|
|
|
|
2014-07-12 07:51:47 +00:00
|
|
|
package redis
|
2014-07-05 14:27:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/astaxie/beego/cache"
|
2018-03-26 08:59:01 +00:00
|
|
|
"github.com/gomodule/redigo/redis"
|
2014-07-05 14:27:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRedisCache(t *testing.T) {
|
2014-07-12 07:51:47 +00:00
|
|
|
bm, err := cache.NewCache("redis", `{"conn": "127.0.0.1:6379"}`)
|
2014-07-05 14:27:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("init err")
|
|
|
|
}
|
2016-03-25 05:25:29 +00:00
|
|
|
timeoutDuration := 10 * time.Second
|
|
|
|
if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
|
2014-07-05 14:27:31 +00:00
|
|
|
t.Error("set Error", err)
|
|
|
|
}
|
|
|
|
if !bm.IsExist("astaxie") {
|
|
|
|
t.Error("check err")
|
|
|
|
}
|
|
|
|
|
2016-01-08 15:30:19 +00:00
|
|
|
time.Sleep(11 * time.Second)
|
2014-07-05 14:27:31 +00:00
|
|
|
|
|
|
|
if bm.IsExist("astaxie") {
|
|
|
|
t.Error("check err")
|
|
|
|
}
|
2016-03-25 05:25:29 +00:00
|
|
|
if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
|
2014-07-05 14:27:31 +00:00
|
|
|
t.Error("set Error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 {
|
|
|
|
t.Error("get err")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = bm.Incr("astaxie"); err != nil {
|
|
|
|
t.Error("Incr Error", err)
|
|
|
|
}
|
|
|
|
|
2014-07-12 07:51:47 +00:00
|
|
|
if v, _ := redis.Int(bm.Get("astaxie"), err); v != 2 {
|
2014-07-05 14:27:31 +00:00
|
|
|
t.Error("get err")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = bm.Decr("astaxie"); err != nil {
|
|
|
|
t.Error("Decr Error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 {
|
|
|
|
t.Error("get err")
|
|
|
|
}
|
|
|
|
bm.Delete("astaxie")
|
|
|
|
if bm.IsExist("astaxie") {
|
|
|
|
t.Error("delete err")
|
|
|
|
}
|
2015-06-07 13:33:01 +00:00
|
|
|
|
2014-07-05 14:27:31 +00:00
|
|
|
//test string
|
2016-03-25 05:25:29 +00:00
|
|
|
if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
|
2014-07-05 14:27:31 +00:00
|
|
|
t.Error("set Error", err)
|
|
|
|
}
|
|
|
|
if !bm.IsExist("astaxie") {
|
|
|
|
t.Error("check err")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, _ := redis.String(bm.Get("astaxie"), err); v != "author" {
|
|
|
|
t.Error("get err")
|
|
|
|
}
|
2015-06-07 13:33:01 +00:00
|
|
|
|
|
|
|
//test GetMulti
|
2016-03-25 05:25:29 +00:00
|
|
|
if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
|
2015-06-07 13:33:01 +00:00
|
|
|
t.Error("set Error", err)
|
|
|
|
}
|
|
|
|
if !bm.IsExist("astaxie1") {
|
|
|
|
t.Error("check err")
|
|
|
|
}
|
|
|
|
|
|
|
|
vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
|
|
|
|
if len(vv) != 2 {
|
|
|
|
t.Error("GetMulti ERROR")
|
|
|
|
}
|
|
|
|
if v, _ := redis.String(vv[0], nil); v != "author" {
|
|
|
|
t.Error("GetMulti ERROR")
|
|
|
|
}
|
|
|
|
if v, _ := redis.String(vv[1], nil); v != "author1" {
|
|
|
|
t.Error("GetMulti ERROR")
|
|
|
|
}
|
|
|
|
|
2014-07-12 07:51:47 +00:00
|
|
|
// test clear all
|
|
|
|
if err = bm.ClearAll(); err != nil {
|
|
|
|
t.Error("clear all err")
|
|
|
|
}
|
2014-07-05 14:27:31 +00:00
|
|
|
}
|