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 package lru
import "sync" import (
"gitee.com/timedb/wheatCache/pkg/structure"
"sync"
)
type SingleWorkFunc func() interface{} type SingleWorkFunc func() interface{}
const ( const (
OptionEventName = "operateEvent" OptionEventName = "operateEvent"
CleanEventName = "clearEvent" CleanEventName = "clearEvent"
WorkFuncEventKey = "workFunc" WorkFuncEventKey = "workFunc"
) )
var ( var (
lruCacheOnce sync.Once lruCacheOnce sync.Once
lruCache *singleCache lruCache *SingleCache
) )
const ( const (
lruMaxSize = 1*1024*1024*1024*8 lruMaxSize = 1 * 1024 * 1024 * 1024 * 8
lruClearSize = 0.5*1024*1024*1024*8 lruClearSize = 0.5 * 1024 * 1024 * 1024 * 8
lruEventDriver = 2000 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)) atomic.AddInt64(&lru.nowSize, int64(length))
} }
func cacheInit() (int64, int64, event.DriverInterface) { func cacheInit() (int64, int64, int) {
maxSize := viper.GetString("lruCache.maxSize") maxSize := viper.GetString("lruCache.maxSize")
retMaxSize, maxErr := util.ParseSizeToBit(maxSize) retMaxSize, maxErr := util.ParseSizeToBit(maxSize)
if maxErr != nil { if maxErr != nil {
return 0, 0, nil return 0, 0, 0
} }
if retMaxSize == 0 { if retMaxSize == 0 {
retMaxSize = lruMaxSize retMaxSize = lruMaxSize
@ -46,7 +46,7 @@ func cacheInit() (int64, int64, event.DriverInterface) {
clearSize := viper.GetString("lruCache.clearSize") clearSize := viper.GetString("lruCache.clearSize")
retClearSize, clearErr := util.ParseSizeToBit(clearSize) retClearSize, clearErr := util.ParseSizeToBit(clearSize)
if clearErr != nil { if clearErr != nil {
return 0, 0, nil return 0, 0, 0
} }
if retClearSize == 0 { if retClearSize == 0 {
retClearSize = lruClearSize retClearSize = lruClearSize
@ -56,15 +56,14 @@ func cacheInit() (int64, int64, event.DriverInterface) {
if maxDriver == 0 { if maxDriver == 0 {
maxDriver = lruEventDriver maxDriver = lruEventDriver
} }
lruDriver := event.NewDriver(maxDriver) return retMaxSize, retClearSize, maxDriver
return retMaxSize, retClearSize, lruDriver
} }
// NewLRUCache lru初始化 // NewLRUCache lru初始化
func NewLRUCache() *SingleCache { func NewLRUCache() *SingleCache {
maxSize, clearSize, lruDrivers := cacheInit() maxSize, clearSize, maxDriverSize := cacheInit()
lruDriver := event.NewDriver(maxDriverSize)
lruCacheOnce.Do(func() { lruCacheOnce.Do(func() {
_, _, lruDriver := cacheInit()
lru := &SingleCache{ lru := &SingleCache{
maxsize: maxSize, maxsize: maxSize,
clearSize: clearSize, clearSize: clearSize,
@ -72,8 +71,8 @@ func NewLRUCache() *SingleCache {
li: list.New(), li: list.New(),
lruMap: make(map[string]*list.Element), lruMap: make(map[string]*list.Element),
lruDriver: lruDriver, lruDriver: lruDriver,
lruConsumer: event.NewConsumer(lruDrivers), lruConsumer: event.NewConsumer(lruDriver),
lruCleanProduce: event.NewProduce(lruDrivers), lruCleanProduce: event.NewProduce(lruDriver),
} }
lruCache = lru lruCache = lru
go lru.lruSingleWork() go lru.lruSingleWork()

View File

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