forked from p53841790/wheat-cache
feat(lru): update sizebyte
This commit is contained in:
parent
8bcac6f739
commit
1de8e1b141
|
@ -23,37 +23,37 @@ type SingleCache struct {
|
|||
li *list.List
|
||||
lruMap map[string]*list.Element
|
||||
|
||||
lruDriver event.DriverInterface
|
||||
lruConsumer event.ConsumerInterface
|
||||
lruCleanProduce event.ProduceInterface // 发送清理事件
|
||||
lruDriver event.DriverInterface
|
||||
lruConsumer event.ConsumerInterface
|
||||
lruCleanProduce event.ProduceInterface // 发送清理事件
|
||||
}
|
||||
|
||||
// UpdateLruSize 更新现在的长度
|
||||
func (lru *SingleCache) UpdateLruSize(length int64) {
|
||||
atomic.AddInt64(&lru.nowSize, length)
|
||||
func (lru *SingleCache) UpdateLruSize(length structure.UpdateLength) {
|
||||
atomic.AddInt64(&lru.nowSize, int64(length))
|
||||
}
|
||||
|
||||
func cacheInit() (int64, int64, event.DriverInterface) {
|
||||
maxSize := viper.GetString("lruCache.maxSize")
|
||||
retMaxSize, maxErr:= util.ParseSizeToBit(maxSize)
|
||||
if maxErr != nil{
|
||||
retMaxSize, maxErr := util.ParseSizeToBit(maxSize)
|
||||
if maxErr != nil {
|
||||
return 0, 0, nil
|
||||
}
|
||||
if retMaxSize == 0{
|
||||
if retMaxSize == 0 {
|
||||
retMaxSize = lruMaxSize
|
||||
}
|
||||
|
||||
clearSize := viper.GetString("lruCache.clearSize")
|
||||
retClearSize, clearErr := util.ParseSizeToBit(clearSize)
|
||||
if clearErr != nil{
|
||||
if clearErr != nil {
|
||||
return 0, 0, nil
|
||||
}
|
||||
if retClearSize == 0{
|
||||
if retClearSize == 0 {
|
||||
retClearSize = lruClearSize
|
||||
}
|
||||
|
||||
maxDriver := viper.GetInt("lruCache.eventDriverSize")
|
||||
if maxDriver == 0{
|
||||
if maxDriver == 0 {
|
||||
maxDriver = lruEventDriver
|
||||
}
|
||||
lruDriver := event.NewDriver(maxDriver)
|
||||
|
@ -66,14 +66,14 @@ func NewLRUCache() *SingleCache {
|
|||
lruCacheOnce.Do(func() {
|
||||
_, _, lruDriver := cacheInit()
|
||||
lru := &SingleCache{
|
||||
maxsize: maxSize,
|
||||
clearSize: clearSize,
|
||||
nowSize: 0,
|
||||
li: list.New(),
|
||||
lruMap: make(map[string]*list.Element),
|
||||
lruDriver: lruDriver,
|
||||
lruConsumer: event.NewConsumer(lruDrivers),
|
||||
lruCleanProduce: event.NewProduce(lruDrivers),
|
||||
maxsize: maxSize,
|
||||
clearSize: clearSize,
|
||||
nowSize: 0,
|
||||
li: list.New(),
|
||||
lruMap: make(map[string]*list.Element),
|
||||
lruDriver: lruDriver,
|
||||
lruConsumer: event.NewConsumer(lruDrivers),
|
||||
lruCleanProduce: event.NewProduce(lruDrivers),
|
||||
}
|
||||
lruCache = lru
|
||||
go lru.lruSingleWork()
|
||||
|
@ -101,7 +101,7 @@ func (lru *SingleCache) Add(key string, val structure.KeyBaseInterface) {
|
|||
valEl := lru.li.PushFront(keyBaseVal)
|
||||
lru.lruMap[key] = valEl
|
||||
//增加大小
|
||||
lru.UpdateLruSize(valEl.Value.(*keyBaseValue).val.SizeByte())
|
||||
lru.UpdateLruSize(structure.UpdateLength(valEl.Value.(*keyBaseValue).val.SizeByte()))
|
||||
}
|
||||
|
||||
// Get 查找key对应的value
|
||||
|
@ -125,7 +125,7 @@ func (lru *SingleCache) Del() error {
|
|||
data := lru.li.Back()
|
||||
delete(lru.lruMap, data.Value.(*keyBaseValue).key)
|
||||
//删除大小
|
||||
lru.UpdateLruSize(-1 * data.Value.(*keyBaseValue).val.SizeByte())
|
||||
lru.UpdateLruSize(structure.UpdateLength(-1 * data.Value.(*keyBaseValue).val.SizeByte()))
|
||||
lru.li.Remove(data)
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue