update(pkg): commit lru

This commit is contained in:
HuangJiaLuo 2021-09-25 12:33:29 +08:00 committed by bandl
parent 98ef750a67
commit 2ad86909d1
1 changed files with 89 additions and 0 deletions

89
pkg/lru/Lru.go Normal file
View File

@ -0,0 +1,89 @@
package lru
import (
"container/list"
"errors"
"gitee.com/timedb/wheatCache/pkg/structure"
)
type Node struct {
Key interface{}
value structure.KeyBaseInterface
}
func (*Node) NewCacheNode(k interface{}, v structure.KeyBaseInterface) Node {
return Node{Key: k, value: v}
}
type LRUCache struct {
maxsize int64 //最大的长度
clearsize int64 // 清理长度
nowsize int64 // 现在的长度
li *list.List
LruMap map[interface{}]*list.Element
}
// NewLRUCache lru初始化
func NewLRUCache(msize, csize int64) *LRUCache {
return &LRUCache{
maxsize: msize,
clearsize: csize,
nowsize: 0,
li: list.New(),
LruMap: make(map[interface{}]*list.Element)}
}
// Add 增
func (lru *LRUCache) Add(k interface{}, v structure.KeyBaseInterface) error {
if lru.li == nil {
return nil
}
if PreEle, ok := lru.LruMap[k]; ok {
lru.li.MoveToFront(PreEle)
PreEle.Value.(*Node).value = v
lru.nowsize += PreEle.Value.(*Node).value.SizeByte()
return nil
}
if lru.nowsize >= lru.maxsize*1/3 {
lru.nowsize -= lru.maxsize
if PreEle, ok := lru.LruMap[k]; ok {
cacheNode := PreEle.Value.(*Node).value
delete(lru.LruMap, cacheNode.SizeByte())
lru.li.Remove(PreEle)
}
}
newEle := lru.li.PushFront(&Node{k, v})
lru.LruMap[k] = newEle
return nil
}
// Get 查
func (lru *LRUCache) Get(k interface{}) (v structure.KeyBaseInterface, ret bool) {
if lru.LruMap == nil {
return v, false
}
if PreEle, ok := lru.LruMap[k]; ok {
// 移到最前面
lru.li.MoveToFront(PreEle)
return PreEle.Value.(*Node).value, true
}
return v, false
}
// Clear 删除
func (lru *LRUCache) Clear(k interface{}) error {
if lru.LruMap == nil {
return errors.New("cache 为空")
}
for lru.nowsize >= lru.clearsize*1/3 {
if PreEle, ok := lru.LruMap[k]; ok {
cacheNode := PreEle.Value.(*Node).value
delete(lru.LruMap, cacheNode.SizeByte())
lru.li.Remove(PreEle)
return nil
}
}
return nil
}