wheat-cache/pkg/lru/lru_test.go

58 lines
1.1 KiB
Go
Raw Normal View History

2021-09-27 11:29:47 +08:00
package lru
import (
2021-10-04 20:32:20 +08:00
"fmt"
"gitee.com/timedb/wheatCache/pkg/proto"
2021-09-27 11:29:47 +08:00
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
2021-10-04 11:21:55 +08:00
"github.com/stretchr/testify/require"
2021-09-27 11:29:47 +08:00
"testing"
)
func TestNewLRUCache(t *testing.T) {
cache := NewLRUCache()
v1 := stringx.NewStringSingle()
v2 := stringx.NewStringSingle()
v3 := stringx.NewStringSingle()
key1 := proto.BaseKey{
Key: "1",
}
key2 := proto.BaseKey{
Key: "2",
}
key3 := proto.BaseKey{
Key: "3",
}
cache.Add(&key1, v1)
cache.Add(&key2, v2)
cache.Add(&key3, v3)
cache.Add(&key1, v1)
2021-10-04 20:32:20 +08:00
fmt.Println(cache.nowSize)
2021-09-27 11:29:47 +08:00
cache.Del()
2021-10-04 20:32:20 +08:00
fmt.Println(cache.nowSize)
_, isTrue := cache.Get(&key1)
2021-10-04 11:21:55 +08:00
require.Equal(t, isTrue, true)
2021-10-06 19:56:32 +08:00
}
func TestNewLRUCache2(t *testing.T) {
//根据key删除
cache := NewLRUCache()
v1 := stringx.NewStringSingle()
v2 := stringx.NewStringSingle()
v3 := stringx.NewStringSingle()
key1 := proto.BaseKey{
Key: "1",
}
key2 := proto.BaseKey{
Key: "2",
}
key3 := proto.BaseKey{
Key: "3",
}
cache.Add(&key1, v1)
cache.Add(&key2, v2)
cache.Add(&key3, v3)
cache.DelByKey(&key1)
_, ok := cache.Get(&key1)
2021-10-06 19:56:32 +08:00
require.Equal(t, ok, false)
require.Error(t, cache.DelByKey(&key1))
2021-09-27 11:29:47 +08:00
}