perf(lru): perf lru keybase err
This commit is contained in:
parent
95d3fdbd17
commit
d17f8e243c
|
@ -3,3 +3,7 @@ package errorx
|
||||||
func LruNotWorkFuncEventErr() error {
|
func LruNotWorkFuncEventErr() error {
|
||||||
return New("the event haven't work of function")
|
return New("the event haven't work of function")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func KeyBaseIsNilErr() error {
|
||||||
|
return New("key base not is nil")
|
||||||
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ const (
|
||||||
type CacheInterface interface {
|
type CacheInterface interface {
|
||||||
Del() error
|
Del() error
|
||||||
Get(key *proto.BaseKey) (structure.KeyBaseInterface, bool)
|
Get(key *proto.BaseKey) (structure.KeyBaseInterface, bool)
|
||||||
Add(key *proto.BaseKey, val structure.KeyBaseInterface)
|
Add(key *proto.BaseKey, val structure.KeyBaseInterface) error
|
||||||
UpdateLruSize(length structure.UpdateLength)
|
UpdateLruSize(length structure.UpdateLength)
|
||||||
DelByKey(key *proto.BaseKey) error
|
DelByKey(key *proto.BaseKey) error
|
||||||
DelToClearSize() error
|
DelToClearSize() error
|
||||||
|
|
|
@ -108,7 +108,11 @@ func (lru *SingleCache) GetDriver() event.DriverInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
//Add 增加
|
//Add 增加
|
||||||
func (lru *SingleCache) Add(key *proto.BaseKey, val structure.KeyBaseInterface) {
|
func (lru *SingleCache) Add(key *proto.BaseKey, val structure.KeyBaseInterface) error {
|
||||||
|
|
||||||
|
if key == nil {
|
||||||
|
return errorx.KeyBaseIsNilErr()
|
||||||
|
}
|
||||||
|
|
||||||
exp := lru.lruTtlManage.setKeys(key)
|
exp := lru.lruTtlManage.setKeys(key)
|
||||||
keyBaseVal := &keyBaseValue{
|
keyBaseVal := &keyBaseValue{
|
||||||
|
@ -119,17 +123,22 @@ func (lru *SingleCache) Add(key *proto.BaseKey, val structure.KeyBaseInterface)
|
||||||
if elVal, ok := lru.lruMap[key.Key]; ok {
|
if elVal, ok := lru.lruMap[key.Key]; ok {
|
||||||
lru.li.MoveToFront(elVal)
|
lru.li.MoveToFront(elVal)
|
||||||
elVal.Value = keyBaseVal
|
elVal.Value = keyBaseVal
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
valEl := lru.li.PushFront(keyBaseVal)
|
valEl := lru.li.PushFront(keyBaseVal)
|
||||||
lru.lruMap[key.Key] = valEl
|
lru.lruMap[key.Key] = valEl
|
||||||
//增加大小
|
//增加大小
|
||||||
lru.UpdateLruSize(structure.UpdateLength(valEl.Value.(*keyBaseValue).val.SizeByte()))
|
lru.UpdateLruSize(structure.UpdateLength(valEl.Value.(*keyBaseValue).val.SizeByte()))
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get 查找key对应的value
|
// Get 查找key对应的value
|
||||||
func (lru *SingleCache) Get(key *proto.BaseKey) (structure.KeyBaseInterface, bool) {
|
func (lru *SingleCache) Get(key *proto.BaseKey) (structure.KeyBaseInterface, bool) {
|
||||||
|
|
||||||
|
if key == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
if elVal, ok := lru.lruMap[key.Key]; ok {
|
if elVal, ok := lru.lruMap[key.Key]; ok {
|
||||||
lru.li.MoveToFront(elVal)
|
lru.li.MoveToFront(elVal)
|
||||||
return elVal.Value.(*keyBaseValue).val, true
|
return elVal.Value.(*keyBaseValue).val, true
|
||||||
|
@ -152,6 +161,11 @@ func (lru *SingleCache) Del() error {
|
||||||
|
|
||||||
//DelByKey 根据key删除
|
//DelByKey 根据key删除
|
||||||
func (lru *SingleCache) DelByKey(key *proto.BaseKey) error {
|
func (lru *SingleCache) DelByKey(key *proto.BaseKey) error {
|
||||||
|
|
||||||
|
if key == nil {
|
||||||
|
return errorx.KeyBaseIsNilErr()
|
||||||
|
}
|
||||||
|
|
||||||
if lru.lruMap == nil {
|
if lru.lruMap == nil {
|
||||||
return errorx.New("lru is nil")
|
return errorx.New("lru is nil")
|
||||||
}
|
}
|
||||||
|
@ -194,6 +208,10 @@ func (lru *SingleCache) DelToClearSize() error {
|
||||||
// 更新过期时间
|
// 更新过期时间
|
||||||
func (lru *SingleCache) UpdateTTl(key *proto.BaseKey) error {
|
func (lru *SingleCache) UpdateTTl(key *proto.BaseKey) error {
|
||||||
|
|
||||||
|
if key == nil {
|
||||||
|
return errorx.KeyBaseIsNilErr()
|
||||||
|
}
|
||||||
|
|
||||||
if elVal, ok := lru.lruMap[key.Key]; ok {
|
if elVal, ok := lru.lruMap[key.Key]; ok {
|
||||||
expire := lru.lruTtlManage.setKeys(key)
|
expire := lru.lruTtlManage.setKeys(key)
|
||||||
elVal.Value.(*keyBaseValue).expire = expire
|
elVal.Value.(*keyBaseValue).expire = expire
|
||||||
|
|
Loading…
Reference in New Issue