wheat-cache/pkg/lru/lru.go

132 lines
3.1 KiB
Go
Raw Normal View History

2021-09-27 11:29:47 +08:00
package lru
import (
"container/list"
_ "gitee.com/timedb/wheatCache/conf"
"gitee.com/timedb/wheatCache/pkg/errorx"
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/pkg/structure"
"gitee.com/timedb/wheatCache/pkg/util"
"github.com/spf13/viper"
"sync/atomic"
)
type keyBaseValue struct {
key string
val structure.KeyBaseInterface
}
type singleCache struct {
maxsize int64 //最大的长度
clearSize int64 // 清理长度
nowSize int64 // 现在的长度
li *list.List
lruMap map[string]*list.Element
lruDriver event.DriverInterface
lruConsumer event.ConsumerInterface
2021-10-04 20:32:20 +08:00
lruCleanProduce event.ProduceInterface // 发送清理事件
2021-09-27 11:29:47 +08:00
}
2021-10-04 20:32:20 +08:00
// UpdateLruSize 更新现在的长度
func (lru *singleCache) UpdateLruSize(length int64) {
2021-09-27 11:29:47 +08:00
atomic.AddInt64(&lru.nowSize, length)
}
2021-10-04 20:32:20 +08:00
func cacheInit() (int64, int64, event.DriverInterface) {
2021-09-27 11:29:47 +08:00
maxSize := viper.GetString("lruCache.maxSize")
2021-10-04 20:32:20 +08:00
retMaxSize, maxErr:= util.ParseSizeToBit(maxSize)
if maxErr != nil{
return 0, 0, nil
}
if retMaxSize == 0{
retMaxSize = lruMaxSize
}
2021-09-27 11:29:47 +08:00
clearSize := viper.GetString("lruCache.clearSize")
2021-10-04 20:32:20 +08:00
retClearSize, clearErr := util.ParseSizeToBit(clearSize)
if clearErr != nil{
return 0, 0, nil
}
if retClearSize == 0{
retClearSize = lruClearSize
}
2021-09-27 11:29:47 +08:00
maxDriver := viper.GetInt("lruCache.eventDriverSize")
2021-10-04 20:32:20 +08:00
if maxDriver == 0{
maxDriver = lruEventDriver
}
2021-09-27 11:29:47 +08:00
lruDriver := event.NewDriver(maxDriver)
2021-10-04 20:32:20 +08:00
return retMaxSize, retClearSize, lruDriver
}
2021-09-27 11:29:47 +08:00
// NewLRUCache lru初始化
func NewLRUCache() *singleCache {
2021-10-04 20:32:20 +08:00
maxSize, clearSize, lruDrivers := cacheInit()
2021-10-04 16:23:37 +08:00
lruCacheOnce.Do(func() {
2021-10-04 20:32:20 +08:00
_, _, lruDriver := cacheInit()
2021-10-04 16:23:37 +08:00
lru := &singleCache{
2021-10-04 20:32:20 +08:00
maxsize: maxSize,
clearSize: clearSize,
2021-10-04 16:23:37 +08:00
nowSize: 0,
li: list.New(),
lruMap: make(map[string]*list.Element),
lruDriver: lruDriver,
2021-10-04 20:32:20 +08:00
lruConsumer: event.NewConsumer(lruDrivers),
lruCleanProduce: event.NewProduce(lruDrivers),
2021-10-04 16:23:37 +08:00
}
lruCache = lru
go lru.lruSingleWork()
})
return lruCache
2021-09-27 11:29:47 +08:00
}
2021-10-04 16:23:37 +08:00
// GetDriver 获取驱动
func (lru *singleCache) GetDriver() event.DriverInterface {
2021-09-27 11:29:47 +08:00
return lru.lruDriver
}
//Add 增加
func (lru *singleCache) Add(key string, val structure.KeyBaseInterface) {
keyBaseVal := &keyBaseValue{
key: key,
val: val,
}
if elVal, ok := lru.lruMap[key]; ok {
lru.li.MoveToFront(elVal)
elVal.Value = keyBaseVal
return
}
valEl := lru.li.PushFront(keyBaseVal)
lru.lruMap[key] = valEl
2021-10-04 20:32:20 +08:00
//增加大小
lru.UpdateLruSize(valEl.Value.(*keyBaseValue).val.SizeByte())
2021-09-27 11:29:47 +08:00
}
// Get 查找key对应的value
func (lru *singleCache) Get(key string) (structure.KeyBaseInterface, bool) {
if lru.lruMap == nil {
return nil, false
}
if elVal, ok := lru.lruMap[key]; ok {
lru.li.MoveToFront(elVal)
return elVal.Value.(*keyBaseValue).val, true
}
return nil, false
}
//Del 删除机制
func (lru *singleCache) Del() error {
if lru.lruMap == nil {
return errorx.New("lru is nil")
}
data := lru.li.Back()
delete(lru.lruMap, data.Value.(*keyBaseValue).key)
2021-10-04 20:32:20 +08:00
//删除大小
lru.UpdateLruSize(-1 * data.Value.(*keyBaseValue).val.SizeByte())
2021-09-27 11:29:47 +08:00
lru.li.Remove(data)
return nil
}