feat(lru): update lru

This commit is contained in:
bandl 2021-10-05 16:53:16 +08:00
parent 212e025b23
commit 4afe3dabb6
3 changed files with 23 additions and 18 deletions

View File

@ -1,22 +1,32 @@
package lru
import "sync"
import (
"gitee.com/timedb/wheatCache/pkg/structure"
"sync"
)
type SingleWorkFunc func() interface{}
const (
OptionEventName = "operateEvent"
CleanEventName = "clearEvent"
CleanEventName = "clearEvent"
WorkFuncEventKey = "workFunc"
)
var (
lruCacheOnce sync.Once
lruCache *singleCache
lruCache *SingleCache
)
const (
lruMaxSize = 1*1024*1024*1024*8
lruClearSize = 0.5*1024*1024*1024*8
lruMaxSize = 1 * 1024 * 1024 * 1024 * 8
lruClearSize = 0.5 * 1024 * 1024 * 1024 * 8
lruEventDriver = 2000
)
type CacheInterface interface {
Del() error
Get(key string) (structure.KeyBaseInterface, bool)
Add(key string, val structure.KeyBaseInterface)
UpdateLruSize(length structure.UpdateLength)
}

View File

@ -33,11 +33,11 @@ func (lru *SingleCache) UpdateLruSize(length structure.UpdateLength) {
atomic.AddInt64(&lru.nowSize, int64(length))
}
func cacheInit() (int64, int64, event.DriverInterface) {
func cacheInit() (int64, int64, int) {
maxSize := viper.GetString("lruCache.maxSize")
retMaxSize, maxErr := util.ParseSizeToBit(maxSize)
if maxErr != nil {
return 0, 0, nil
return 0, 0, 0
}
if retMaxSize == 0 {
retMaxSize = lruMaxSize
@ -46,7 +46,7 @@ func cacheInit() (int64, int64, event.DriverInterface) {
clearSize := viper.GetString("lruCache.clearSize")
retClearSize, clearErr := util.ParseSizeToBit(clearSize)
if clearErr != nil {
return 0, 0, nil
return 0, 0, 0
}
if retClearSize == 0 {
retClearSize = lruClearSize
@ -56,15 +56,14 @@ func cacheInit() (int64, int64, event.DriverInterface) {
if maxDriver == 0 {
maxDriver = lruEventDriver
}
lruDriver := event.NewDriver(maxDriver)
return retMaxSize, retClearSize, lruDriver
return retMaxSize, retClearSize, maxDriver
}
// NewLRUCache lru初始化
func NewLRUCache() *SingleCache {
maxSize, clearSize, lruDrivers := cacheInit()
maxSize, clearSize, maxDriverSize := cacheInit()
lruDriver := event.NewDriver(maxDriverSize)
lruCacheOnce.Do(func() {
_, _, lruDriver := cacheInit()
lru := &SingleCache{
maxsize: maxSize,
clearSize: clearSize,
@ -72,8 +71,8 @@ func NewLRUCache() *SingleCache {
li: list.New(),
lruMap: make(map[string]*list.Element),
lruDriver: lruDriver,
lruConsumer: event.NewConsumer(lruDrivers),
lruCleanProduce: event.NewProduce(lruDrivers),
lruConsumer: event.NewConsumer(lruDriver),
lruCleanProduce: event.NewProduce(lruDriver),
}
lruCache = lru
go lru.lruSingleWork()

View File

@ -6,10 +6,6 @@ import (
"log"
)
func lruCleanWork() {
}
func (lru *SingleCache) lruSingleWork() interface{} {
ctx := context.Background()
for {