From 1de8e1b1418c575e5d690a3749be621226b8f011 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Mon, 4 Oct 2021 22:37:04 +0800 Subject: [PATCH] feat(lru): update sizebyte --- pkg/lru/lru.go | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/pkg/lru/lru.go b/pkg/lru/lru.go index ae40a8f..4f7b949 100644 --- a/pkg/lru/lru.go +++ b/pkg/lru/lru.go @@ -23,37 +23,37 @@ type SingleCache struct { li *list.List lruMap map[string]*list.Element - lruDriver event.DriverInterface - lruConsumer event.ConsumerInterface - lruCleanProduce event.ProduceInterface // 发送清理事件 + lruDriver event.DriverInterface + lruConsumer event.ConsumerInterface + lruCleanProduce event.ProduceInterface // 发送清理事件 } // UpdateLruSize 更新现在的长度 -func (lru *SingleCache) UpdateLruSize(length int64) { - atomic.AddInt64(&lru.nowSize, length) +func (lru *SingleCache) UpdateLruSize(length structure.UpdateLength) { + atomic.AddInt64(&lru.nowSize, int64(length)) } func cacheInit() (int64, int64, event.DriverInterface) { maxSize := viper.GetString("lruCache.maxSize") - retMaxSize, maxErr:= util.ParseSizeToBit(maxSize) - if maxErr != nil{ + retMaxSize, maxErr := util.ParseSizeToBit(maxSize) + if maxErr != nil { return 0, 0, nil } - if retMaxSize == 0{ + if retMaxSize == 0 { retMaxSize = lruMaxSize } clearSize := viper.GetString("lruCache.clearSize") retClearSize, clearErr := util.ParseSizeToBit(clearSize) - if clearErr != nil{ + if clearErr != nil { return 0, 0, nil } - if retClearSize == 0{ + if retClearSize == 0 { retClearSize = lruClearSize } maxDriver := viper.GetInt("lruCache.eventDriverSize") - if maxDriver == 0{ + if maxDriver == 0 { maxDriver = lruEventDriver } lruDriver := event.NewDriver(maxDriver) @@ -66,14 +66,14 @@ func NewLRUCache() *SingleCache { lruCacheOnce.Do(func() { _, _, lruDriver := cacheInit() lru := &SingleCache{ - maxsize: maxSize, - clearSize: clearSize, - nowSize: 0, - li: list.New(), - lruMap: make(map[string]*list.Element), - lruDriver: lruDriver, - lruConsumer: event.NewConsumer(lruDrivers), - lruCleanProduce: event.NewProduce(lruDrivers), + maxsize: maxSize, + clearSize: clearSize, + nowSize: 0, + li: list.New(), + lruMap: make(map[string]*list.Element), + lruDriver: lruDriver, + lruConsumer: event.NewConsumer(lruDrivers), + lruCleanProduce: event.NewProduce(lruDrivers), } lruCache = lru go lru.lruSingleWork() @@ -101,7 +101,7 @@ func (lru *SingleCache) Add(key string, val structure.KeyBaseInterface) { valEl := lru.li.PushFront(keyBaseVal) lru.lruMap[key] = valEl //增加大小 - lru.UpdateLruSize(valEl.Value.(*keyBaseValue).val.SizeByte()) + lru.UpdateLruSize(structure.UpdateLength(valEl.Value.(*keyBaseValue).val.SizeByte())) } // Get 查找key对应的value @@ -125,7 +125,7 @@ func (lru *SingleCache) Del() error { data := lru.li.Back() delete(lru.lruMap, data.Value.(*keyBaseValue).key) //删除大小 - lru.UpdateLruSize(-1 * data.Value.(*keyBaseValue).val.SizeByte()) + lru.UpdateLruSize(structure.UpdateLength(-1 * data.Value.(*keyBaseValue).val.SizeByte())) lru.li.Remove(data) return nil }