fix(lru): fix the bug of lru

This commit is contained in:
HuangJiaLuo 2021-10-04 20:32:20 +08:00
parent ca04ba1686
commit 477adec1bb
3 changed files with 45 additions and 18 deletions

View File

@ -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
)

View File

@ -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
}

View File

@ -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)
}