From fbc6edc60dc8e62323995f7407b1814be9a497e4 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Sat, 25 Sep 2021 13:37:41 +0800 Subject: [PATCH] request update @hjl --- conf/wheat-cache.yaml | 4 ++++ pkg/lru/Lru.go | 32 ++++++++++++++++++++++---------- pkg/lru/define.go | 2 +- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/conf/wheat-cache.yaml b/conf/wheat-cache.yaml index 4475627..c63eec3 100644 --- a/conf/wheat-cache.yaml +++ b/conf/wheat-cache.yaml @@ -5,4 +5,8 @@ env: 'dev' storage: host: '127.0.0.1' port: 5890 + +lru-cache: + mode: "single" # thread + diff --git a/pkg/lru/Lru.go b/pkg/lru/Lru.go index 73ecdcc..cf82df8 100644 --- a/pkg/lru/Lru.go +++ b/pkg/lru/Lru.go @@ -6,13 +6,20 @@ import ( "gitee.com/timedb/wheatCache/pkg/structure" ) +// feat +/* +1. cleanProduce +2. 定义 LRUSingle Work 函数 + +*/ + type Node struct { Key interface{} value structure.KeyBaseInterface } -func (*Node) NewCacheNode(k interface{}, v structure.KeyBaseInterface) Node { - return Node{Key: k, value: v} +func (*Node) NewCacheNode(k interface{}, v structure.KeyBaseInterface) *Node { + return &Node{Key: k, value: v} } type LRUCache struct { @@ -20,10 +27,10 @@ type LRUCache struct { clearsize int64 // 清理长度 nowsize int64 // 现在的长度 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 { return &LRUCache{ maxsize: msize, @@ -33,18 +40,22 @@ func NewLRUCache(msize, csize int64) *LRUCache { LruMap: make(map[interface{}]*list.Element)} } -// Add 增 +// Add 增 TODO 大问题 func (lru *LRUCache) Add(k interface{}, v structure.KeyBaseInterface) error { if lru.li == nil { - return nil + return nil // TODO err } + + // TODO 封装 Key 迁移方法 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 } + + // TODO 增加的情况下, 应该只维护 maxSize if lru.nowsize >= lru.maxsize*1/3 { lru.nowsize -= lru.maxsize 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}) - lru.LruMap[k] = newEle + lru.LruMap[k] = newEle // TODO nil 指针 return nil } -// Get 查 +// Get 查 // TODO err 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) @@ -72,7 +84,7 @@ func (lru *LRUCache) Get(k interface{}) (v structure.KeyBaseInterface, ret bool) return v, false } -// Clear 删除 +// Clear 删除 // TODO 方案不对 func (lru *LRUCache) Clear(k interface{}) error { if lru.LruMap == nil { return errors.New("cache 为空") @@ -80,7 +92,7 @@ func (lru *LRUCache) Clear(k interface{}) error { for lru.nowsize >= lru.clearsize*1/3 { if PreEle, ok := lru.LruMap[k]; ok { cacheNode := PreEle.Value.(*Node).value - delete(lru.LruMap, cacheNode.SizeByte()) + delete(lru.LruMap, cacheNode) lru.li.Remove(PreEle) return nil } diff --git a/pkg/lru/define.go b/pkg/lru/define.go index 2eb64ef..0525251 100644 --- a/pkg/lru/define.go +++ b/pkg/lru/define.go @@ -1,3 +1,3 @@ package lru -// define +type SingleWorkFunc func()