request update @hjl

This commit is contained in:
bandl 2021-09-25 13:37:41 +08:00
parent 2ad86909d1
commit fbc6edc60d
3 changed files with 27 additions and 11 deletions

View File

@ -5,4 +5,8 @@ env: 'dev'
storage: storage:
host: '127.0.0.1' host: '127.0.0.1'
port: 5890 port: 5890
lru-cache:
mode: "single" # thread

View File

@ -6,13 +6,20 @@ import (
"gitee.com/timedb/wheatCache/pkg/structure" "gitee.com/timedb/wheatCache/pkg/structure"
) )
// feat
/*
1. cleanProduce
2. 定义 LRUSingle Work 函数
*/
type Node struct { type Node struct {
Key interface{} Key interface{}
value structure.KeyBaseInterface value structure.KeyBaseInterface
} }
func (*Node) NewCacheNode(k interface{}, v structure.KeyBaseInterface) Node { func (*Node) NewCacheNode(k interface{}, v structure.KeyBaseInterface) *Node {
return Node{Key: k, value: v} return &Node{Key: k, value: v}
} }
type LRUCache struct { type LRUCache struct {
@ -20,10 +27,10 @@ type LRUCache struct {
clearsize int64 // 清理长度 clearsize int64 // 清理长度
nowsize int64 // 现在的长度 nowsize int64 // 现在的长度
li *list.List li *list.List
LruMap map[interface{}]*list.Element LruMap map[interface{}]*list.Element // TODO LRU 对外暴露
} }
// NewLRUCache lru初始化 // NewLRUCache lru初始化 TODO 单例模式
func NewLRUCache(msize, csize int64) *LRUCache { func NewLRUCache(msize, csize int64) *LRUCache {
return &LRUCache{ return &LRUCache{
maxsize: msize, maxsize: msize,
@ -33,18 +40,22 @@ func NewLRUCache(msize, csize int64) *LRUCache {
LruMap: make(map[interface{}]*list.Element)} LruMap: make(map[interface{}]*list.Element)}
} }
// Add 增 // Add 增 TODO 大问题
func (lru *LRUCache) Add(k interface{}, v structure.KeyBaseInterface) error { func (lru *LRUCache) Add(k interface{}, v structure.KeyBaseInterface) error {
if lru.li == nil { if lru.li == nil {
return nil return nil // TODO err
} }
// TODO 封装 Key 迁移方法
if PreEle, ok := lru.LruMap[k]; ok { if PreEle, ok := lru.LruMap[k]; ok {
lru.li.MoveToFront(PreEle) lru.li.MoveToFront(PreEle)
PreEle.Value.(*Node).value = v PreEle.Value.(*Node).value = v
lru.nowsize += PreEle.Value.(*Node).value.SizeByte() lru.nowsize += PreEle.Value.(*Node).value.SizeByte()
return nil return nil
} }
// TODO 增加的情况下, 应该只维护 maxSize
if lru.nowsize >= lru.maxsize*1/3 { if lru.nowsize >= lru.maxsize*1/3 {
lru.nowsize -= lru.maxsize lru.nowsize -= lru.maxsize
if PreEle, ok := lru.LruMap[k]; ok { if PreEle, ok := lru.LruMap[k]; ok {
@ -54,16 +65,17 @@ func (lru *LRUCache) Add(k interface{}, v structure.KeyBaseInterface) error {
} }
} }
newEle := lru.li.PushFront(&Node{k, v}) newEle := lru.li.PushFront(&Node{k, v})
lru.LruMap[k] = newEle lru.LruMap[k] = newEle // TODO nil 指针
return nil return nil
} }
// Get 查 // Get 查 // TODO err
func (lru *LRUCache) Get(k interface{}) (v structure.KeyBaseInterface, ret bool) { func (lru *LRUCache) Get(k interface{}) (v structure.KeyBaseInterface, ret bool) {
if lru.LruMap == nil { if lru.LruMap == nil {
return v, false return v, false
} }
if PreEle, ok := lru.LruMap[k]; ok { if PreEle, ok := lru.LruMap[k]; ok {
// 移到最前面 // 移到最前面
lru.li.MoveToFront(PreEle) lru.li.MoveToFront(PreEle)
@ -72,7 +84,7 @@ func (lru *LRUCache) Get(k interface{}) (v structure.KeyBaseInterface, ret bool)
return v, false return v, false
} }
// Clear 删除 // Clear 删除 // TODO 方案不对
func (lru *LRUCache) Clear(k interface{}) error { func (lru *LRUCache) Clear(k interface{}) error {
if lru.LruMap == nil { if lru.LruMap == nil {
return errors.New("cache 为空") return errors.New("cache 为空")
@ -80,7 +92,7 @@ func (lru *LRUCache) Clear(k interface{}) error {
for lru.nowsize >= lru.clearsize*1/3 { for lru.nowsize >= lru.clearsize*1/3 {
if PreEle, ok := lru.LruMap[k]; ok { if PreEle, ok := lru.LruMap[k]; ok {
cacheNode := PreEle.Value.(*Node).value cacheNode := PreEle.Value.(*Node).value
delete(lru.LruMap, cacheNode.SizeByte()) delete(lru.LruMap, cacheNode)
lru.li.Remove(PreEle) lru.li.Remove(PreEle)
return nil return nil
} }

View File

@ -1,3 +1,3 @@
package lru package lru
// define type SingleWorkFunc func()