diff --git a/pkg/lru/lru.go b/pkg/lru/lru.go index 9100b6d..7ad8f41 100644 --- a/pkg/lru/lru.go +++ b/pkg/lru/lru.go @@ -5,6 +5,7 @@ import ( _ "gitee.com/timedb/wheatCache/conf" "gitee.com/timedb/wheatCache/pkg/errorx" "gitee.com/timedb/wheatCache/pkg/event" + "gitee.com/timedb/wheatCache/pkg/proto" "gitee.com/timedb/wheatCache/pkg/structure" "gitee.com/timedb/wheatCache/pkg/util" "github.com/spf13/viper" @@ -86,30 +87,30 @@ func (lru *SingleCache) GetDriver() event.DriverInterface { } //Add 增加 -func (lru *SingleCache) Add(key string, val structure.KeyBaseInterface) { +func (lru *SingleCache) Add(key *proto.BaseKey, val structure.KeyBaseInterface) { keyBaseVal := &keyBaseValue{ - key: key, + key: key.Key, val: val, } - if elVal, ok := lru.lruMap[key]; ok { + if elVal, ok := lru.lruMap[key.Key]; ok { lru.li.MoveToFront(elVal) elVal.Value = keyBaseVal return } valEl := lru.li.PushFront(keyBaseVal) - lru.lruMap[key] = valEl + lru.lruMap[key.Key] = valEl //增加大小 lru.UpdateLruSize(structure.UpdateLength(valEl.Value.(*keyBaseValue).val.SizeByte())) } // 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 { return nil, false } - if elVal, ok := lru.lruMap[key]; ok { + if elVal, ok := lru.lruMap[key.Key]; ok { lru.li.MoveToFront(elVal) return elVal.Value.(*keyBaseValue).val, true } @@ -130,12 +131,12 @@ func (lru *SingleCache) Del() error { } //DelByKey 根据key删除 -func (lru *SingleCache)DelByKey(key string) error { +func (lru *SingleCache)DelByKey(key *proto.BaseKey) error { if lru.lruMap == nil { return errorx.New("lru is nil") } - if _, ok := lru.lruMap[key]; ok { - delete(lru.lruMap, key) + if _, ok := lru.lruMap[key.Key]; ok { + delete(lru.lruMap, key.Key) return nil } return errorx.New("lru no this key") diff --git a/pkg/lru/lru_test.go b/pkg/lru/lru_test.go index 302f97e..2c925d1 100644 --- a/pkg/lru/lru_test.go +++ b/pkg/lru/lru_test.go @@ -2,6 +2,7 @@ package lru import ( "fmt" + "gitee.com/timedb/wheatCache/pkg/proto" "gitee.com/timedb/wheatCache/pkg/structure/stringx" "github.com/stretchr/testify/require" "testing" @@ -12,14 +13,23 @@ func TestNewLRUCache(t *testing.T) { v1 := stringx.NewStringSingle() v2 := stringx.NewStringSingle() v3 := stringx.NewStringSingle() - cache.Add("1", v1) - cache.Add("2", v2) - cache.Add("3", v3) - cache.Add("1", v1) + 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) fmt.Println(cache.nowSize) cache.Del() fmt.Println(cache.nowSize) - _, isTrue := cache.Get("1") + _, isTrue := cache.Get(&key1) require.Equal(t, isTrue, true) } @@ -29,11 +39,20 @@ func TestNewLRUCache2(t *testing.T) { v1 := stringx.NewStringSingle() v2 := stringx.NewStringSingle() v3 := stringx.NewStringSingle() - cache.Add("1", v1) - cache.Add("2", v2) - cache.Add("3", v3) - cache.DelByKey("1") - _, ok := cache.Get("1") + 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) require.Equal(t, ok, false) - fmt.Println(cache.DelByKey("1")) + require.Error(t, cache.DelByKey(&key1)) } \ No newline at end of file