1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-23 00:30:56 +00:00
Beego/cache/cache_test.go

169 lines
3.6 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// 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
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// 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.
2013-04-22 10:56:30 +00:00
package cache
import (
2014-12-19 06:40:16 +00:00
"os"
2013-04-22 10:56:30 +00:00
"testing"
"time"
)
2014-01-01 12:50:06 +00:00
func TestCache(t *testing.T) {
bm, err := NewCache("memory", `{"interval":20}`)
2013-04-22 10:56:30 +00:00
if err != nil {
t.Error("init err")
}
timeoutDuration := 10 * time.Second
2016-01-08 05:47:14 +00:00
if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
2013-04-22 10:56:30 +00:00
t.Error("set Error", err)
}
if !bm.IsExist("astaxie") {
t.Error("check err")
}
if v := bm.Get("astaxie"); v.(int) != 1 {
t.Error("get err")
}
time.Sleep(30 * time.Second)
2013-04-22 10:56:30 +00:00
if bm.IsExist("astaxie") {
t.Error("check err")
}
2016-01-08 05:47:14 +00:00
if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
2013-04-22 10:56:30 +00:00
t.Error("set Error", err)
}
2013-07-16 11:05:44 +00:00
if err = bm.Incr("astaxie"); err != nil {
t.Error("Incr Error", err)
}
if v := bm.Get("astaxie"); v.(int) != 2 {
t.Error("get err")
}
if err = bm.Decr("astaxie"); err != nil {
2014-04-09 04:39:12 +00:00
t.Error("Decr Error", err)
2013-07-16 11:05:44 +00:00
}
if v := bm.Get("astaxie"); v.(int) != 1 {
t.Error("get err")
}
2013-04-22 10:56:30 +00:00
bm.Delete("astaxie")
if bm.IsExist("astaxie") {
t.Error("delete err")
}
//test GetMulti
2016-01-08 05:47:14 +00:00
if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if !bm.IsExist("astaxie") {
t.Error("check err")
}
if v := bm.Get("astaxie"); v.(string) != "author" {
t.Error("get err")
}
2016-01-08 05:47:14 +00:00
if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
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 vv[0].(string) != "author" {
t.Error("GetMulti ERROR")
}
if vv[1].(string) != "author1" {
t.Error("GetMulti ERROR")
}
2013-04-22 10:56:30 +00:00
}
2014-01-01 12:50:06 +00:00
func TestFileCache(t *testing.T) {
2014-12-19 06:40:16 +00:00
bm, err := NewCache("file", `{"CachePath":"cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}`)
2014-01-01 12:50:06 +00:00
if err != nil {
t.Error("init err")
}
timeoutDuration := 10 * time.Second
2016-01-08 05:47:14 +00:00
if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
2014-01-01 12:50:06 +00:00
t.Error("set Error", err)
}
if !bm.IsExist("astaxie") {
t.Error("check err")
}
if v := bm.Get("astaxie"); v.(int) != 1 {
t.Error("get err")
}
if err = bm.Incr("astaxie"); err != nil {
t.Error("Incr Error", err)
}
if v := bm.Get("astaxie"); v.(int) != 2 {
t.Error("get err")
}
if err = bm.Decr("astaxie"); err != nil {
2014-04-09 04:39:12 +00:00
t.Error("Decr Error", err)
2014-01-01 12:50:06 +00:00
}
if v := bm.Get("astaxie"); v.(int) != 1 {
t.Error("get err")
}
bm.Delete("astaxie")
if bm.IsExist("astaxie") {
t.Error("delete err")
}
2014-01-01 12:50:06 +00:00
//test string
2016-01-08 05:47:14 +00:00
if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
2014-01-01 12:50:06 +00:00
t.Error("set Error", err)
}
if !bm.IsExist("astaxie") {
t.Error("check err")
}
if v := bm.Get("astaxie"); v.(string) != "author" {
t.Error("get err")
}
//test GetMulti
2016-01-08 05:47:14 +00:00
if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
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 vv[0].(string) != "author" {
t.Error("GetMulti ERROR")
}
if vv[1].(string) != "author1" {
t.Error("GetMulti ERROR")
}
2014-12-19 06:40:16 +00:00
os.RemoveAll("cache")
2014-01-01 12:50:06 +00:00
}