forked from p93542168/wheat-cache
feat(lru): change string to proto.KeyBase
This commit is contained in:
parent
90b021bee2
commit
d08e50f4ce
|
@ -5,6 +5,7 @@ import (
|
||||||
_ "gitee.com/timedb/wheatCache/conf"
|
_ "gitee.com/timedb/wheatCache/conf"
|
||||||
"gitee.com/timedb/wheatCache/pkg/errorx"
|
"gitee.com/timedb/wheatCache/pkg/errorx"
|
||||||
"gitee.com/timedb/wheatCache/pkg/event"
|
"gitee.com/timedb/wheatCache/pkg/event"
|
||||||
|
"gitee.com/timedb/wheatCache/pkg/proto"
|
||||||
"gitee.com/timedb/wheatCache/pkg/structure"
|
"gitee.com/timedb/wheatCache/pkg/structure"
|
||||||
"gitee.com/timedb/wheatCache/pkg/util"
|
"gitee.com/timedb/wheatCache/pkg/util"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
@ -86,30 +87,30 @@ func (lru *SingleCache) GetDriver() event.DriverInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
//Add 增加
|
//Add 增加
|
||||||
func (lru *SingleCache) Add(key string, val structure.KeyBaseInterface) {
|
func (lru *SingleCache) Add(key *proto.BaseKey, val structure.KeyBaseInterface) {
|
||||||
|
|
||||||
keyBaseVal := &keyBaseValue{
|
keyBaseVal := &keyBaseValue{
|
||||||
key: key,
|
key: key.Key,
|
||||||
val: val,
|
val: val,
|
||||||
}
|
}
|
||||||
if elVal, ok := lru.lruMap[key]; ok {
|
if elVal, ok := lru.lruMap[key.Key]; ok {
|
||||||
lru.li.MoveToFront(elVal)
|
lru.li.MoveToFront(elVal)
|
||||||
elVal.Value = keyBaseVal
|
elVal.Value = keyBaseVal
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
valEl := lru.li.PushFront(keyBaseVal)
|
valEl := lru.li.PushFront(keyBaseVal)
|
||||||
lru.lruMap[key] = valEl
|
lru.lruMap[key.Key] = valEl
|
||||||
//增加大小
|
//增加大小
|
||||||
lru.UpdateLruSize(structure.UpdateLength(valEl.Value.(*keyBaseValue).val.SizeByte()))
|
lru.UpdateLruSize(structure.UpdateLength(valEl.Value.(*keyBaseValue).val.SizeByte()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get 查找key对应的value
|
// Get 查找key对应的value
|
||||||
func (lru *SingleCache) Get(key string) (structure.KeyBaseInterface, bool) {
|
func (lru *SingleCache) Get(key *proto.BaseKey) (structure.KeyBaseInterface, bool) {
|
||||||
|
|
||||||
if lru.lruMap == nil {
|
if lru.lruMap == nil {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
if elVal, ok := lru.lruMap[key]; ok {
|
if elVal, ok := lru.lruMap[key.Key]; ok {
|
||||||
lru.li.MoveToFront(elVal)
|
lru.li.MoveToFront(elVal)
|
||||||
return elVal.Value.(*keyBaseValue).val, true
|
return elVal.Value.(*keyBaseValue).val, true
|
||||||
}
|
}
|
||||||
|
@ -130,12 +131,12 @@ func (lru *SingleCache) Del() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
//DelByKey 根据key删除
|
//DelByKey 根据key删除
|
||||||
func (lru *SingleCache)DelByKey(key string) error {
|
func (lru *SingleCache)DelByKey(key *proto.BaseKey) error {
|
||||||
if lru.lruMap == nil {
|
if lru.lruMap == nil {
|
||||||
return errorx.New("lru is nil")
|
return errorx.New("lru is nil")
|
||||||
}
|
}
|
||||||
if _, ok := lru.lruMap[key]; ok {
|
if _, ok := lru.lruMap[key.Key]; ok {
|
||||||
delete(lru.lruMap, key)
|
delete(lru.lruMap, key.Key)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return errorx.New("lru no this key")
|
return errorx.New("lru no this key")
|
||||||
|
|
|
@ -2,6 +2,7 @@ package lru
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gitee.com/timedb/wheatCache/pkg/proto"
|
||||||
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
|
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -12,14 +13,23 @@ func TestNewLRUCache(t *testing.T) {
|
||||||
v1 := stringx.NewStringSingle()
|
v1 := stringx.NewStringSingle()
|
||||||
v2 := stringx.NewStringSingle()
|
v2 := stringx.NewStringSingle()
|
||||||
v3 := stringx.NewStringSingle()
|
v3 := stringx.NewStringSingle()
|
||||||
cache.Add("1", v1)
|
key1 := proto.BaseKey{
|
||||||
cache.Add("2", v2)
|
Key: "1",
|
||||||
cache.Add("3", v3)
|
}
|
||||||
cache.Add("1", v1)
|
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)
|
||||||
fmt.Println(cache.nowSize)
|
fmt.Println(cache.nowSize)
|
||||||
cache.Del()
|
cache.Del()
|
||||||
fmt.Println(cache.nowSize)
|
fmt.Println(cache.nowSize)
|
||||||
_, isTrue := cache.Get("1")
|
_, isTrue := cache.Get(&key1)
|
||||||
require.Equal(t, isTrue, true)
|
require.Equal(t, isTrue, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,11 +39,20 @@ func TestNewLRUCache2(t *testing.T) {
|
||||||
v1 := stringx.NewStringSingle()
|
v1 := stringx.NewStringSingle()
|
||||||
v2 := stringx.NewStringSingle()
|
v2 := stringx.NewStringSingle()
|
||||||
v3 := stringx.NewStringSingle()
|
v3 := stringx.NewStringSingle()
|
||||||
cache.Add("1", v1)
|
key1 := proto.BaseKey{
|
||||||
cache.Add("2", v2)
|
Key: "1",
|
||||||
cache.Add("3", v3)
|
}
|
||||||
cache.DelByKey("1")
|
key2 := proto.BaseKey{
|
||||||
_, ok := cache.Get("1")
|
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)
|
||||||
require.Equal(t, ok, false)
|
require.Equal(t, ok, false)
|
||||||
fmt.Println(cache.DelByKey("1"))
|
require.Error(t, cache.DelByKey(&key1))
|
||||||
}
|
}
|
Loading…
Reference in New Issue