feat(lru): feat the init cache

This commit is contained in:
HuangJiaLuo 2021-10-04 16:23:37 +08:00
parent dbc0467de4
commit 75e1251d22
1 changed files with 25 additions and 14 deletions

View File

@ -2,6 +2,7 @@ package lru
import (
"container/list"
"fmt"
_ "gitee.com/timedb/wheatCache/conf"
"gitee.com/timedb/wheatCache/pkg/errorx"
"gitee.com/timedb/wheatCache/pkg/event"
@ -11,7 +12,6 @@ import (
"sync/atomic"
)
type keyBaseValue struct {
key string
val structure.KeyBaseInterface
@ -29,24 +29,31 @@ type singleCache struct {
lruProduce event.ProduceInterface // 发送清理事件
}
// UpdateLruLength 更新现在的长度
func (lru *singleCache) UpdateLruLength(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)
maxSize := viper.GetString("lruCache.maxSize")
clearSize := viper.GetString("lruCache.clearSize")
maxDriver := viper.GetInt("lruCache.eventDriverSize")
lruDriver := event.NewDriver(maxDriver)
fmt.Println(clearSize)
return maxSize, clearSize, lruDriver
}
// NewLRUCache lru初始化
func NewLRUCache() *singleCache {
lruCacheOnce.Do(func() {
maxSize, clearSize, lruDriver := cacheInit()
return &singleCache{
maxsize: util.ParseSizeToBit(maxSize),
clearSize: util.ParseSizeToBit(clearSize),
lru := &singleCache{
maxsize: util.ParseSizeToBit(maxSize).(int64),
clearSize: util.ParseSizeToBit(clearSize).(int64),
nowSize: 0,
li: list.New(),
lruMap: make(map[string]*list.Element),
@ -54,10 +61,14 @@ func NewLRUCache() *singleCache {
lruConsumer: event.NewConsumer(lruDriver),
lruProduce: event.NewProduce(lruDriver),
}
lruCache = lru
go lru.lruSingleWork()
})
return lruCache
}
// RetDriver 获取驱动
func (lru *singleCache) RetDriver() event.DriverInterface {
// GetDriver 获取驱动
func (lru *singleCache) GetDriver() event.DriverInterface {
return lru.lruDriver
}