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 (
|
||||
lruMaxSize = 1 * 1024 * 1024 * 1024 * 8
|
||||
lruClearSize = 0.5 * 1024 * 1024 * 1024 * 8
|
||||
lruEventDriver = 2000
|
||||
defaultLruMaxSize = 1 * 1024 * 1024 * 1024 * 8
|
||||
defaultLruClearSize = 0.5 * 1024 * 1024 * 1024 * 8
|
||||
defaultLruEventDriver = 2000
|
||||
)
|
||||
|
||||
type CacheInterface interface {
|
||||
|
@ -31,4 +31,5 @@ type CacheInterface interface {
|
|||
Add(key *proto.BaseKey, val structure.KeyBaseInterface)
|
||||
UpdateLruSize(length structure.UpdateLength)
|
||||
DelByKey(key *proto.BaseKey) error
|
||||
DelToClearSize() error
|
||||
}
|
||||
|
|
|
@ -18,12 +18,12 @@ type keyBaseValue struct {
|
|||
}
|
||||
|
||||
type SingleCache struct {
|
||||
maxsize int64 //最大的长度
|
||||
clearSize int64 // 清理长度
|
||||
nowSize int64 // 现在的长度
|
||||
li *list.List
|
||||
lruMap map[string]*list.Element
|
||||
|
||||
maxsize int64 //最大的长度
|
||||
clearSize int64 // 清理长度
|
||||
nowSize int64 // 现在的长度
|
||||
li *list.List
|
||||
lruMap map[string]*list.Element
|
||||
lruMaxDiverSize int
|
||||
lruDriver event.DriverInterface
|
||||
lruConsumer event.ConsumerInterface
|
||||
lruCleanProduce event.ProduceInterface // 发送清理事件
|
||||
|
@ -41,7 +41,7 @@ func cacheInit() (int64, int64, int) {
|
|||
return 0, 0, 0
|
||||
}
|
||||
if retMaxSize == 0 {
|
||||
retMaxSize = lruMaxSize
|
||||
retMaxSize = defaultLruMaxSize
|
||||
}
|
||||
|
||||
clearSize := viper.GetString("lruCache.clearSize")
|
||||
|
@ -50,12 +50,12 @@ func cacheInit() (int64, int64, int) {
|
|||
return 0, 0, 0
|
||||
}
|
||||
if retClearSize == 0 {
|
||||
retClearSize = lruClearSize
|
||||
retClearSize = defaultLruClearSize
|
||||
}
|
||||
|
||||
maxDriver := viper.GetInt("lruCache.eventDriverSize")
|
||||
if maxDriver == 0 {
|
||||
maxDriver = lruEventDriver
|
||||
maxDriver = defaultLruEventDriver
|
||||
}
|
||||
return retMaxSize, retClearSize, maxDriver
|
||||
}
|
||||
|
@ -71,6 +71,7 @@ func NewLRUCache() *SingleCache {
|
|||
nowSize: 0,
|
||||
li: list.New(),
|
||||
lruMap: make(map[string]*list.Element),
|
||||
lruMaxDiverSize: maxDriverSize,
|
||||
lruDriver: lruDriver,
|
||||
lruConsumer: event.NewConsumer(lruDriver),
|
||||
lruCleanProduce: event.NewProduce(lruDriver),
|
||||
|
@ -131,7 +132,7 @@ func (lru *SingleCache) Del() error {
|
|||
}
|
||||
|
||||
//DelByKey 根据key删除
|
||||
func (lru *SingleCache)DelByKey(key *proto.BaseKey) error {
|
||||
func (lru *SingleCache) DelByKey(key *proto.BaseKey) error {
|
||||
if lru.lruMap == nil {
|
||||
return errorx.New("lru is nil")
|
||||
}
|
||||
|
@ -141,3 +142,18 @@ func (lru *SingleCache)DelByKey(key *proto.BaseKey) error {
|
|||
}
|
||||
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:
|
||||
workFunc, ok := workEvent.GetValue(WorkFuncEventKey)
|
||||
if !ok {
|
||||
workEvent.ExecWorkAndSendResult(func() (interface{}, error) {
|
||||
return nil, errorx.New("the event haven't work of function")
|
||||
})
|
||||
workEvent.SetResultErr(errorx.LruNotWorkFuncEventErr())
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -25,6 +23,22 @@ func (lru *SingleCache) lruSingleWork() interface{} {
|
|||
workEvent.ExecWorkAndSendResult(work)
|
||||
}
|
||||
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