1 Star 1 Fork 0

maemual/go-cache

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
cache_test.go 3.83 KB
一键复制 编辑 原始数据 按行查看 历史
maemual 提交于 2014-12-11 16:08 +08:00 . 完善文档和README
package cache
import (
"fmt"
"sync"
"testing"
"time"
)
func TestCache(t *testing.T) {
c := New(2*time.Second, 1*time.Second)
_, found := c.Get("xx")
if found {
t.Error("You should not get")
}
c.Set("key", "lalala", -1)
val, found := c.Get("key")
if !found {
t.Error("You must get this value")
}
if val != "lalala" {
t.Error("You get a wrong value")
}
c.Delete("key")
_, found = c.Get("key")
if found {
t.Error("The key is delete, you should not get")
}
c.Set("key", "bababa", -1)
cnt := c.ItemCount()
if cnt != 1 {
t.Error("The number of cache must be 1")
}
c.Flush()
_, found = c.Get("key")
if found {
t.Error("All keys are flush, you should not get")
}
cnt = c.ItemCount()
if cnt != 0 {
t.Error("The number of cache must be 0")
}
c.Set("key", "val", 2*time.Second)
_, found = c.Get("key")
if !found {
t.Error("must have this")
}
time.Sleep(3 * time.Second)
cnt = c.ItemCount()
if cnt != 0 {
t.Error("The number of cache must be 0")
}
_, found = c.Get("key")
if found {
t.Error("The key is time out, you should not get")
}
}
func TestIncDec(t *testing.T) {
c := New(1*time.Second, -1)
c.Set("key", 100, -1)
c.Increment("key", 1)
val, _ := c.Get("key")
if val != 101 {
t.Error("Increment error")
}
c.Decrement("key", 1)
val, _ = c.Get("key")
if val != 100 {
t.Error("Decrement error")
}
}
func BenchmarkCacheGet(b *testing.B) {
b.StopTimer()
tc := New(0, 0)
tc.Set("key", "values", -1)
b.StartTimer()
for i := 0; i < b.N; i++ {
tc.Get("key")
}
}
func BenchmarkRWMutexGet(b *testing.B) {
b.StopTimer()
m := map[string]string{
"key": "values",
}
mu := sync.RWMutex{}
b.StartTimer()
for i := 0; i < b.N; i++ {
mu.RLock()
_, _ = m["key"]
mu.RUnlock()
}
}
func BenchmarkCacheSet(b *testing.B) {
b.StopTimer()
tc := New(0, 0)
b.StartTimer()
for i := 0; i < b.N; i++ {
tc.Set("key", "values", -1)
}
}
func BenchmarkRWMutexSet(b *testing.B) {
b.StopTimer()
m := map[string]string{}
mu := sync.RWMutex{}
b.StartTimer()
for i := 0; i < b.N; i++ {
mu.Lock()
m["key"] = "values"
mu.Unlock()
}
}
func BenchmarkCacheIncrement(b *testing.B) {
b.StopTimer()
tc := New(0, 0)
tc.Set("key", 0, -1)
b.StartTimer()
for i := 0; i < b.N; i++ {
tc.Increment("key", 1)
}
}
func BenchmarkCacheDecrement(b *testing.B) {
b.StopTimer()
tc := New(0, 0)
tc.Set("key", 0, -1)
b.StartTimer()
for i := 0; i < b.N; i++ {
tc.Decrement("key", 1)
}
}
func TestLRUCache(t *testing.T) {
_, err := NewLRU(-1)
if err == nil {
t.Error("Impossiable!")
}
lru, err := NewLRU(1)
if err != nil {
t.Error(err)
}
lru.Add("1", 111)
if lru.Len() != 1 {
t.Error("Now, there is one value in cache")
}
val, hit := lru.Get("1")
if !hit {
t.Error("I should get the key")
}
if val != 111 {
t.Error("Get the wrong value")
}
lru.Add("2", 222)
if lru.Len() != 1 {
t.Error("Now, there is only one value in cache")
}
_, hit = lru.Get("1")
if hit {
t.Error("The old value must be removed")
}
lru.Remove("2")
_, hit = lru.Get("2")
if hit {
t.Error("The value must be removed")
}
if lru.Len() != 0 {
t.Error("Now, there is no value in cache")
}
lru.Clear()
if lru.Len() != 0 {
t.Error("Now, the lru cache is cleared")
}
lru.SetMaxEntries(2)
lru.Add("1", 2222)
lru.Add("2", 34444)
if lru.Len() != 2 {
t.Error("Now, the len of lru cache must be 2")
}
}
func ExampleCache() {
c := New(0, 0)
c.Set("1", 1111, 0)
val, found := c.Get("1")
if found {
fmt.Println(val)
}
c.Increment("1", 1)
val, found = c.Get("1")
if found {
fmt.Println(val)
}
// Output:
// 1111
// 1112
}
func ExampleLRUCache() {
lru, err := NewLRU(3)
if err != nil {
fmt.Println(err)
}
lru.Add("1", 1111)
lru.Add("2", 2222)
lru.Add("3", 3333)
lru.Add("4", 4444)
_, hit := lru.Get("1")
if hit {
fmt.Println("Hit key 1")
} else {
fmt.Println("Not hit key 1")
}
// Output:
// Not hit key 1
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/maemual/go-cache.git
git@gitee.com:maemual/go-cache.git
maemual
go-cache
go-cache
master

搜索帮助