mirror of
https://github.com/astaxie/beego.git
synced 2024-11-18 11:40:54 +00:00
118 lines
2.5 KiB
Go
118 lines
2.5 KiB
Go
// 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.
|
|
|
|
package cache
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestCache(t *testing.T) {
|
|
bm, err := NewCache("memory", `{"interval":20}`)
|
|
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)
|
|
|
|
if bm.IsExist("astaxie") {
|
|
t.Error("check err")
|
|
}
|
|
|
|
if err = bm.Put("astaxie", 1, 10); err != nil {
|
|
t.Error("set Error", 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 {
|
|
t.Error("Decr Error", err)
|
|
}
|
|
|
|
if v := bm.Get("astaxie"); v.(int) != 1 {
|
|
t.Error("get err")
|
|
}
|
|
bm.Delete("astaxie")
|
|
if bm.IsExist("astaxie") {
|
|
t.Error("delete err")
|
|
}
|
|
}
|
|
|
|
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 {
|
|
t.Error("Decr Error", err)
|
|
}
|
|
|
|
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")
|
|
}
|
|
os.RemoveAll("cache")
|
|
}
|