2014-04-12 05:18:18 +00:00
|
|
|
// Beego (http://beego.me/)
|
|
|
|
// @description beego is an open-source, high-performance web framework for the Go programming language.
|
|
|
|
// @link http://github.com/astaxie/beego for the canonical source repository
|
|
|
|
// @license http://github.com/astaxie/beego/blob/master/LICENSE
|
|
|
|
// @authors astaxie
|
|
|
|
|
2013-04-22 10:56:30 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-01-01 12:50:06 +00:00
|
|
|
func TestCache(t *testing.T) {
|
2013-07-04 05:02:11 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2013-07-04 05:02:11 +00:00
|
|
|
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) {
|
|
|
|
bm, err := NewCache("file", `{"CachePath":"/cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}`)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|