feat(lru): del by key

This commit is contained in:
HuangJiaLuo 2021-10-06 19:56:32 +08:00
parent 63bd44da44
commit 90b021bee2
2 changed files with 27 additions and 0 deletions

View File

@ -128,3 +128,15 @@ func (lru *SingleCache) Del() error {
lru.li.Remove(data)
return nil
}
//DelByKey 根据key删除
func (lru *SingleCache)DelByKey(key string) error {
if lru.lruMap == nil {
return errorx.New("lru is nil")
}
if _, ok := lru.lruMap[key]; ok {
delete(lru.lruMap, key)
return nil
}
return errorx.New("lru no this key")
}

View File

@ -21,4 +21,19 @@ func TestNewLRUCache(t *testing.T) {
fmt.Println(cache.nowSize)
_, isTrue := cache.Get("1")
require.Equal(t, isTrue, true)
}
func TestNewLRUCache2(t *testing.T) {
//根据key删除
cache := NewLRUCache()
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")
require.Equal(t, ok, false)
fmt.Println(cache.DelByKey("1"))
}