1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-23 06:20:50 +00:00
Beego/cache/cache_test.go

118 lines
2.5 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")
}
if err = bm.Put("astaxie", 1, 10); err != nil {
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")
}
if err = bm.Put("astaxie", 1, 10); err != nil {
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")
}
}
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")
}
if err = bm.Put("astaxie", 1, 10); err != nil {
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")
}
//test string
if err = bm.Put("astaxie", "author", 10); 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")
}
2014-12-19 06:40:16 +00:00
os.RemoveAll("cache")
2014-01-01 12:50:06 +00:00
}