forked from p93542168/wheat-cache
feat(lru): feat the function of cleanEvent
This commit is contained in:
parent
843fcd27ca
commit
64b38044c8
|
@ -20,9 +20,9 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
lruMaxSize = 1 * 1024 * 1024 * 1024 * 8
|
defaultLruMaxSize = 1 * 1024 * 1024 * 1024 * 8
|
||||||
lruClearSize = 0.5 * 1024 * 1024 * 1024 * 8
|
defaultLruClearSize = 0.5 * 1024 * 1024 * 1024 * 8
|
||||||
lruEventDriver = 2000
|
defaultLruEventDriver = 2000
|
||||||
)
|
)
|
||||||
|
|
||||||
type CacheInterface interface {
|
type CacheInterface interface {
|
||||||
|
@ -31,4 +31,5 @@ type CacheInterface interface {
|
||||||
Add(key *proto.BaseKey, val structure.KeyBaseInterface)
|
Add(key *proto.BaseKey, val structure.KeyBaseInterface)
|
||||||
UpdateLruSize(length structure.UpdateLength)
|
UpdateLruSize(length structure.UpdateLength)
|
||||||
DelByKey(key *proto.BaseKey) error
|
DelByKey(key *proto.BaseKey) error
|
||||||
|
DelToClearSize() error
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ type SingleCache struct {
|
||||||
nowSize int64 // 现在的长度
|
nowSize int64 // 现在的长度
|
||||||
li *list.List
|
li *list.List
|
||||||
lruMap map[string]*list.Element
|
lruMap map[string]*list.Element
|
||||||
|
lruMaxDiverSize int
|
||||||
lruDriver event.DriverInterface
|
lruDriver event.DriverInterface
|
||||||
lruConsumer event.ConsumerInterface
|
lruConsumer event.ConsumerInterface
|
||||||
lruCleanProduce event.ProduceInterface // 发送清理事件
|
lruCleanProduce event.ProduceInterface // 发送清理事件
|
||||||
|
@ -41,7 +41,7 @@ func cacheInit() (int64, int64, int) {
|
||||||
return 0, 0, 0
|
return 0, 0, 0
|
||||||
}
|
}
|
||||||
if retMaxSize == 0 {
|
if retMaxSize == 0 {
|
||||||
retMaxSize = lruMaxSize
|
retMaxSize = defaultLruMaxSize
|
||||||
}
|
}
|
||||||
|
|
||||||
clearSize := viper.GetString("lruCache.clearSize")
|
clearSize := viper.GetString("lruCache.clearSize")
|
||||||
|
@ -50,12 +50,12 @@ func cacheInit() (int64, int64, int) {
|
||||||
return 0, 0, 0
|
return 0, 0, 0
|
||||||
}
|
}
|
||||||
if retClearSize == 0 {
|
if retClearSize == 0 {
|
||||||
retClearSize = lruClearSize
|
retClearSize = defaultLruClearSize
|
||||||
}
|
}
|
||||||
|
|
||||||
maxDriver := viper.GetInt("lruCache.eventDriverSize")
|
maxDriver := viper.GetInt("lruCache.eventDriverSize")
|
||||||
if maxDriver == 0 {
|
if maxDriver == 0 {
|
||||||
maxDriver = lruEventDriver
|
maxDriver = defaultLruEventDriver
|
||||||
}
|
}
|
||||||
return retMaxSize, retClearSize, maxDriver
|
return retMaxSize, retClearSize, maxDriver
|
||||||
}
|
}
|
||||||
|
@ -71,6 +71,7 @@ func NewLRUCache() *SingleCache {
|
||||||
nowSize: 0,
|
nowSize: 0,
|
||||||
li: list.New(),
|
li: list.New(),
|
||||||
lruMap: make(map[string]*list.Element),
|
lruMap: make(map[string]*list.Element),
|
||||||
|
lruMaxDiverSize: maxDriverSize,
|
||||||
lruDriver: lruDriver,
|
lruDriver: lruDriver,
|
||||||
lruConsumer: event.NewConsumer(lruDriver),
|
lruConsumer: event.NewConsumer(lruDriver),
|
||||||
lruCleanProduce: event.NewProduce(lruDriver),
|
lruCleanProduce: event.NewProduce(lruDriver),
|
||||||
|
@ -141,3 +142,18 @@ func (lru *SingleCache)DelByKey(key *proto.BaseKey) error {
|
||||||
}
|
}
|
||||||
return errorx.New("lru no this key")
|
return errorx.New("lru no this key")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (lru *SingleCache) DelToClearSize() error {
|
||||||
|
if lru.lruMap == nil {
|
||||||
|
return errorx.New("lru is nil")
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
if lru.nowSize > lru.clearSize*1/3 {
|
||||||
|
//del自动给nowSize进行大小的改变
|
||||||
|
lru.Del()
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -15,9 +15,7 @@ func (lru *SingleCache) lruSingleWork() interface{} {
|
||||||
case OptionEventName:
|
case OptionEventName:
|
||||||
workFunc, ok := workEvent.GetValue(WorkFuncEventKey)
|
workFunc, ok := workEvent.GetValue(WorkFuncEventKey)
|
||||||
if !ok {
|
if !ok {
|
||||||
workEvent.ExecWorkAndSendResult(func() (interface{}, error) {
|
workEvent.SetResultErr(errorx.LruNotWorkFuncEventErr())
|
||||||
return nil, errorx.New("the event haven't work of function")
|
|
||||||
})
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,6 +23,22 @@ func (lru *SingleCache) lruSingleWork() interface{} {
|
||||||
workEvent.ExecWorkAndSendResult(work)
|
workEvent.ExecWorkAndSendResult(work)
|
||||||
}
|
}
|
||||||
case CleanEventName:
|
case CleanEventName:
|
||||||
|
workFunc, ok := workEvent.GetValue(WorkFuncEventKey)
|
||||||
|
if !ok {
|
||||||
|
workEvent.SetResultErr(errorx.LruNotWorkFuncEventErr())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// 对当前的io数量进行判断
|
||||||
|
ioNum := lru.GetDriver().GetLength()
|
||||||
|
if ioNum > lru.lruMaxDiverSize*1/2 {
|
||||||
|
lru.lruCleanProduce.Call(ctx, workEvent)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if work, ok := workFunc.(event.EventWorkFunc); ok {
|
||||||
|
workEvent.ExecWorkAndSendResult(work)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return errorx.New("no this name")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue