From 477adec1bb48edb93d0a7f2df00f387c4a5b7907 Mon Sep 17 00:00:00 2001 From: HuangJiaLuo <1820799930@qq.com> Date: Mon, 4 Oct 2021 20:32:20 +0800 Subject: [PATCH] fix(lru): fix the bug of lru --- pkg/lru/define.go | 10 +++++++-- pkg/lru/lru.go | 50 ++++++++++++++++++++++++++++++--------------- pkg/lru/lru_test.go | 3 +++ 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/pkg/lru/define.go b/pkg/lru/define.go index a2da432..84d56d1 100644 --- a/pkg/lru/define.go +++ b/pkg/lru/define.go @@ -5,8 +5,8 @@ import "sync" type SingleWorkFunc func() interface{} const ( - OpEventName = "operateEvent" - CleEventName = "clearEvent" + OptionEventName = "operateEvent" + CleanEventName = "clearEvent" WorkFuncEventKey = "workFunc" ) @@ -14,3 +14,9 @@ var ( lruCacheOnce sync.Once lruCache *singleCache ) + +const ( + lruMaxSize = 1*1024*1024*1024*8 + lruClearSize = 0.5*1024*1024*1024*8 + lruEventDriver = 2000 +) diff --git a/pkg/lru/lru.go b/pkg/lru/lru.go index d2b8d57..24522d2 100644 --- a/pkg/lru/lru.go +++ b/pkg/lru/lru.go @@ -2,7 +2,6 @@ package lru import ( "container/list" - "fmt" _ "gitee.com/timedb/wheatCache/conf" "gitee.com/timedb/wheatCache/pkg/errorx" "gitee.com/timedb/wheatCache/pkg/event" @@ -26,40 +25,55 @@ type singleCache struct { lruDriver event.DriverInterface lruConsumer event.ConsumerInterface - lruProduce event.ProduceInterface // 发送清理事件 + lruCleanProduce event.ProduceInterface // 发送清理事件 } -// UpdateLruLength 更新现在的长度 -func (lru *singleCache) UpdateLruLength(length int64) { +// UpdateLruSize 更新现在的长度 +func (lru *singleCache) UpdateLruSize(length int64) { atomic.AddInt64(&lru.nowSize, length) } -func cacheInit() (string, string, event.DriverInterface) { - viper.Set("lruCache.maxSize", "1GB") - viper.Set("lruCache.clearSize", "512MB") - viper.Set("lruCache.eventDriverSize", 2000) +func cacheInit() (int64, int64, event.DriverInterface) { maxSize := viper.GetString("lruCache.maxSize") + retMaxSize, maxErr:= util.ParseSizeToBit(maxSize) + if maxErr != nil{ + return 0, 0, nil + } + if retMaxSize == 0{ + retMaxSize = lruMaxSize + } + clearSize := viper.GetString("lruCache.clearSize") + retClearSize, clearErr := util.ParseSizeToBit(clearSize) + if clearErr != nil{ + return 0, 0, nil + } + if retClearSize == 0{ + retClearSize = lruClearSize + } + maxDriver := viper.GetInt("lruCache.eventDriverSize") + if maxDriver == 0{ + maxDriver = lruEventDriver + } lruDriver := event.NewDriver(maxDriver) - fmt.Println(clearSize) - return maxSize, clearSize, lruDriver + return retMaxSize, retClearSize, lruDriver } // NewLRUCache lru初始化 func NewLRUCache() *singleCache { - + maxSize, clearSize, lruDrivers := cacheInit() lruCacheOnce.Do(func() { - maxSize, clearSize, lruDriver := cacheInit() + _, _, lruDriver := cacheInit() lru := &singleCache{ - maxsize: util.ParseSizeToBit(maxSize).(int64), - clearSize: util.ParseSizeToBit(clearSize).(int64), + maxsize: maxSize, + clearSize: clearSize, nowSize: 0, li: list.New(), lruMap: make(map[string]*list.Element), lruDriver: lruDriver, - lruConsumer: event.NewConsumer(lruDriver), - lruProduce: event.NewProduce(lruDriver), + lruConsumer: event.NewConsumer(lruDrivers), + lruCleanProduce: event.NewProduce(lruDrivers), } lruCache = lru go lru.lruSingleWork() @@ -86,6 +100,8 @@ 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()) } // Get 查找key对应的value @@ -108,6 +124,8 @@ 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.li.Remove(data) return nil } diff --git a/pkg/lru/lru_test.go b/pkg/lru/lru_test.go index f266192..7cba185 100644 --- a/pkg/lru/lru_test.go +++ b/pkg/lru/lru_test.go @@ -1,6 +1,7 @@ package lru import ( + "fmt" "gitee.com/timedb/wheatCache/pkg/structure/stringx" "github.com/stretchr/testify/require" "testing" @@ -15,7 +16,9 @@ func TestNewLRUCache(t *testing.T) { cache.Add("2", v2) cache.Add("3", v3) cache.Add("1", v1) + fmt.Println(cache.nowSize) cache.Del() + fmt.Println(cache.nowSize) _, isTrue := cache.Get("1") require.Equal(t, isTrue, true) } \ No newline at end of file