From 2500251699d46a0a02a9b8e3dc511880ef5e4e97 Mon Sep 17 00:00:00 2001 From: HuangJiaLuo <1820799930@qq.com> Date: Sat, 9 Oct 2021 22:06:38 +0800 Subject: [PATCH] feat(lru): feat the function of cleanEvent --- pkg/lru/define.go | 7 ++++--- pkg/lru/lru.go | 38 +++++++++++++++++++++++++++----------- pkg/lru/worker.go | 20 +++++++++++++++++--- 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/pkg/lru/define.go b/pkg/lru/define.go index 4d05af5..67f54a2 100644 --- a/pkg/lru/define.go +++ b/pkg/lru/define.go @@ -20,9 +20,9 @@ var ( ) const ( - lruMaxSize = 1 * 1024 * 1024 * 1024 * 8 - lruClearSize = 0.5 * 1024 * 1024 * 1024 * 8 - lruEventDriver = 2000 + defaultLruMaxSize = 1 * 1024 * 1024 * 1024 * 8 + defaultLruClearSize = 0.5 * 1024 * 1024 * 1024 * 8 + defaultLruEventDriver = 2000 ) type CacheInterface interface { @@ -31,4 +31,5 @@ type CacheInterface interface { Add(key *proto.BaseKey, val structure.KeyBaseInterface) UpdateLruSize(length structure.UpdateLength) DelByKey(key *proto.BaseKey) error + DelToClearSize() error } diff --git a/pkg/lru/lru.go b/pkg/lru/lru.go index 7ad8f41..2455a56 100644 --- a/pkg/lru/lru.go +++ b/pkg/lru/lru.go @@ -18,12 +18,12 @@ type keyBaseValue struct { } type SingleCache struct { - maxsize int64 //最大的长度 - clearSize int64 // 清理长度 - nowSize int64 // 现在的长度 - li *list.List - lruMap map[string]*list.Element - + maxsize int64 //最大的长度 + clearSize int64 // 清理长度 + nowSize int64 // 现在的长度 + li *list.List + lruMap map[string]*list.Element + lruMaxDiverSize int lruDriver event.DriverInterface lruConsumer event.ConsumerInterface lruCleanProduce event.ProduceInterface // 发送清理事件 @@ -41,7 +41,7 @@ func cacheInit() (int64, int64, int) { return 0, 0, 0 } if retMaxSize == 0 { - retMaxSize = lruMaxSize + retMaxSize = defaultLruMaxSize } clearSize := viper.GetString("lruCache.clearSize") @@ -50,12 +50,12 @@ func cacheInit() (int64, int64, int) { return 0, 0, 0 } if retClearSize == 0 { - retClearSize = lruClearSize + retClearSize = defaultLruClearSize } maxDriver := viper.GetInt("lruCache.eventDriverSize") if maxDriver == 0 { - maxDriver = lruEventDriver + maxDriver = defaultLruEventDriver } return retMaxSize, retClearSize, maxDriver } @@ -71,6 +71,7 @@ func NewLRUCache() *SingleCache { nowSize: 0, li: list.New(), lruMap: make(map[string]*list.Element), + lruMaxDiverSize: maxDriverSize, lruDriver: lruDriver, lruConsumer: event.NewConsumer(lruDriver), lruCleanProduce: event.NewProduce(lruDriver), @@ -131,7 +132,7 @@ func (lru *SingleCache) Del() error { } //DelByKey 根据key删除 -func (lru *SingleCache)DelByKey(key *proto.BaseKey) error { +func (lru *SingleCache) DelByKey(key *proto.BaseKey) error { if lru.lruMap == nil { return errorx.New("lru is nil") } @@ -140,4 +141,19 @@ func (lru *SingleCache)DelByKey(key *proto.BaseKey) error { return nil } return errorx.New("lru no this key") -} \ No newline at end of file +} + +func (lru *SingleCache) DelToClearSize() error { + if lru.lruMap == nil { + return errorx.New("lru is nil") + } + for { + if lru.nowSize > lru.clearSize*1/3 { + //del自动给nowSize进行大小的改变 + lru.Del() + } else { + break + } + } + return nil +} diff --git a/pkg/lru/worker.go b/pkg/lru/worker.go index 1120ca7..74d5705 100644 --- a/pkg/lru/worker.go +++ b/pkg/lru/worker.go @@ -15,9 +15,7 @@ func (lru *SingleCache) lruSingleWork() interface{} { case OptionEventName: workFunc, ok := workEvent.GetValue(WorkFuncEventKey) if !ok { - workEvent.ExecWorkAndSendResult(func() (interface{}, error) { - return nil, errorx.New("the event haven't work of function") - }) + workEvent.SetResultErr(errorx.LruNotWorkFuncEventErr()) continue } @@ -25,6 +23,22 @@ func (lru *SingleCache) lruSingleWork() interface{} { workEvent.ExecWorkAndSendResult(work) } case CleanEventName: + workFunc, ok := workEvent.GetValue(WorkFuncEventKey) + if !ok { + workEvent.SetResultErr(errorx.LruNotWorkFuncEventErr()) + continue + } + // 对当前的io数量进行判断 + ioNum := lru.GetDriver().GetLength() + if ioNum > lru.lruMaxDiverSize*1/2 { + lru.lruCleanProduce.Call(ctx, workEvent) + continue + } + if work, ok := workFunc.(event.EventWorkFunc); ok { + workEvent.ExecWorkAndSendResult(work) + } + default: + return errorx.New("no this name") } } }