!52 增加了根据key删除value, 将key的类型修改成 KeyBase

Merge pull request !52 from K-on/feat-Lru-delete-key
This commit is contained in:
bandl 2021-10-06 12:47:04 +00:00 committed by Gitee
commit 60abd336b1
7 changed files with 96 additions and 40 deletions

View File

@ -1,6 +1,7 @@
package lru
import (
"gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/pkg/structure"
"sync"
)
@ -26,7 +27,8 @@ const (
type CacheInterface interface {
Del() error
Get(key string) (structure.KeyBaseInterface, bool)
Add(key string, val structure.KeyBaseInterface)
Get(key *proto.BaseKey) (structure.KeyBaseInterface, bool)
Add(key *proto.BaseKey, val structure.KeyBaseInterface)
UpdateLruSize(length structure.UpdateLength)
DelByKey(key *proto.BaseKey) error
}

View File

@ -5,6 +5,7 @@ import (
_ "gitee.com/timedb/wheatCache/conf"
"gitee.com/timedb/wheatCache/pkg/errorx"
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/pkg/structure"
"gitee.com/timedb/wheatCache/pkg/util"
"github.com/spf13/viper"
@ -86,30 +87,30 @@ func (lru *SingleCache) GetDriver() event.DriverInterface {
}
//Add 增加
func (lru *SingleCache) Add(key string, val structure.KeyBaseInterface) {
func (lru *SingleCache) Add(key *proto.BaseKey, val structure.KeyBaseInterface) {
keyBaseVal := &keyBaseValue{
key: key,
key: key.Key,
val: val,
}
if elVal, ok := lru.lruMap[key]; ok {
if elVal, ok := lru.lruMap[key.Key]; ok {
lru.li.MoveToFront(elVal)
elVal.Value = keyBaseVal
return
}
valEl := lru.li.PushFront(keyBaseVal)
lru.lruMap[key] = valEl
lru.lruMap[key.Key] = valEl
//增加大小
lru.UpdateLruSize(structure.UpdateLength(valEl.Value.(*keyBaseValue).val.SizeByte()))
}
// Get 查找key对应的value
func (lru *SingleCache) Get(key string) (structure.KeyBaseInterface, bool) {
func (lru *SingleCache) Get(key *proto.BaseKey) (structure.KeyBaseInterface, bool) {
if lru.lruMap == nil {
return nil, false
}
if elVal, ok := lru.lruMap[key]; ok {
if elVal, ok := lru.lruMap[key.Key]; ok {
lru.li.MoveToFront(elVal)
return elVal.Value.(*keyBaseValue).val, true
}
@ -128,3 +129,15 @@ func (lru *SingleCache) Del() error {
lru.li.Remove(data)
return nil
}
//DelByKey 根据key删除
func (lru *SingleCache)DelByKey(key *proto.BaseKey) error {
if lru.lruMap == nil {
return errorx.New("lru is nil")
}
if _, ok := lru.lruMap[key.Key]; ok {
delete(lru.lruMap, key.Key)
return nil
}
return errorx.New("lru no this key")
}

View File

@ -2,6 +2,7 @@ package lru
import (
"fmt"
"gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
"github.com/stretchr/testify/require"
"testing"
@ -12,13 +13,46 @@ func TestNewLRUCache(t *testing.T) {
v1 := stringx.NewStringSingle()
v2 := stringx.NewStringSingle()
v3 := stringx.NewStringSingle()
cache.Add("1", v1)
cache.Add("2", v2)
cache.Add("3", v3)
cache.Add("1", v1)
key1 := proto.BaseKey{
Key: "1",
}
key2 := proto.BaseKey{
Key: "2",
}
key3 := proto.BaseKey{
Key: "3",
}
cache.Add(&key1, v1)
cache.Add(&key2, v2)
cache.Add(&key3, v3)
cache.Add(&key1, v1)
fmt.Println(cache.nowSize)
cache.Del()
fmt.Println(cache.nowSize)
_, isTrue := cache.Get("1")
_, isTrue := cache.Get(&key1)
require.Equal(t, isTrue, true)
}
func TestNewLRUCache2(t *testing.T) {
//根据key删除
cache := NewLRUCache()
v1 := stringx.NewStringSingle()
v2 := stringx.NewStringSingle()
v3 := stringx.NewStringSingle()
key1 := proto.BaseKey{
Key: "1",
}
key2 := proto.BaseKey{
Key: "2",
}
key3 := proto.BaseKey{
Key: "3",
}
cache.Add(&key1, v1)
cache.Add(&key2, v2)
cache.Add(&key3, v3)
cache.DelByKey(&key1)
_, ok := cache.Get(&key1)
require.Equal(t, ok, false)
require.Error(t, cache.DelByKey(&key1))
}

View File

@ -3,6 +3,7 @@ package lru
import (
"context"
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
"github.com/stretchr/testify/require"
"testing"
@ -16,9 +17,11 @@ func TestWorker(t *testing.T) {
workEvent := event.NewEvent(OptionEventName)
workEvent.SetValue(WorkFuncEventKey, event.EventWorkFunc(func() (interface{}, error) {
v1 := stringx.NewStringSingle()
key := "v1"
key := proto.BaseKey{
Key: "v1",
}
res, _ := v1.Set("123")
lru.Add(key, v1)
lru.Add(&key, v1)
return res, nil
}))
workEvent.InitWaitEvent()

View File

@ -17,6 +17,7 @@ const (
REDUCE
SETBIT
GETBIT
)
var CommKeyString = map[string]int {"set": STRING_X,
@ -25,6 +26,7 @@ var CommKeyString = map[string]int{"set": STRING_X,
"reduce": STRING_X,
"setbit": STRING_X,
"getbit": STRING_X,
}
var CommKey = map[int]int {SET: STRING_X,
@ -33,4 +35,5 @@ var CommKey = map[int]int{SET: STRING_X,
REDUCE: STRING_X,
SETBIT: STRING_X,
GETBIT: STRING_X,
}

View File

@ -3,6 +3,7 @@ package dao
import (
"gitee.com/timedb/wheatCache/pkg/errorx"
"gitee.com/timedb/wheatCache/pkg/lru"
"gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/pkg/structure"
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
)
@ -17,7 +18,7 @@ func NewDao(lru lru.CacheInterface) *Dao {
}
}
func (d *Dao) Set(key string, strVal string) (string, error) {
func (d *Dao) Set(key *proto.BaseKey, strVal string) (string, error) {
value, ok := d.lru.Get(key)
if ok {
if val, ok := value.(structure.StringXInterface); ok {
@ -37,10 +38,10 @@ func (d *Dao) Set(key string, strVal string) (string, error) {
return result, nil
}
func (d *Dao) Get(key string) (string, error) {
func (d *Dao) Get(key *proto.BaseKey) (string, error) {
val, ok := d.lru.Get(key)
if !ok {
return "", errorx.NotKeyErr(key)
return "", errorx.NotKeyErr(key.Key)
}
strVal, ok := val.(structure.StringXInterface)
@ -51,7 +52,7 @@ func (d *Dao) Get(key string) (string, error) {
return strVal.Get(), nil
}
func (d *Dao) Add(key string, renewal int32) (string, error) {
func (d *Dao) Add(key *proto.BaseKey, renewal int32) (string, error) {
value, lruOk := d.lru.Get(key)
if !lruOk {
val := stringx.NewStringSingle()
@ -75,7 +76,7 @@ func (d *Dao) Add(key string, renewal int32) (string, error) {
return res, nil
}
func (d *Dao) Reduce(key string, renewal int32) (string, error) {
func (d *Dao) Reduce(key *proto.BaseKey, renewal int32) (string, error) {
value, lruOk := d.lru.Get(key)
if !lruOk {
val := stringx.NewStringSingle()
@ -99,7 +100,7 @@ func (d *Dao) Reduce(key string, renewal int32) (string, error) {
return res, nil
}
func (d *Dao) Setbit(key string, val bool, offer int32) error {
func (d *Dao) Setbit(key *proto.BaseKey, val bool, offer int32) error {
value, lruOk := d.lru.Get(key)
if !lruOk {
valStr := stringx.NewStringSingle()
@ -119,10 +120,10 @@ func (d *Dao) Setbit(key string, val bool, offer int32) error {
return nil
}
func (d *Dao) GetBit(key string, offer int32) (bool, error) {
func (d *Dao) GetBit(key *proto.BaseKey, offer int32) (bool, error) {
value, lruOk := d.lru.Get(key)
if !lruOk {
return false, errorx.NotKeyErr(key)
return false, errorx.NotKeyErr(key.Key)
}
strVal, ok := value.(structure.StringXInterface)
if !ok {

View File

@ -13,7 +13,7 @@ func (s *serverSingle) Set(
) (*proto.SetResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Set(req.Key.Key, req.Val)
return s.dao.Set(req.Key, req.Val)
})
lruEvent := event.NewEvent(lru.OptionEventName)
@ -35,7 +35,7 @@ func (s *serverSingle) Get(
req *proto.GetRequest,
) (*proto.GetResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Get(req.Key.Key)
return s.dao.Get(req.Key)
})
lruEvent := event.NewEvent(lru.OptionEventName)
@ -56,7 +56,7 @@ func (s serverSingle) Add(
req *proto.AddRequest,
) (*proto.AddResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Add(req.Key.Key, req.Renewal)
return s.dao.Add(req.Key, req.Renewal)
})
lruEvent := event.NewEvent(lru.OptionEventName)
@ -77,7 +77,7 @@ func (s *serverSingle) Reduce(
req *proto.ReduceRequest,
) (*proto.ReduceResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Add(req.Key.Key, req.Renewal)
return s.dao.Add(req.Key, req.Renewal)
})
lruEvent := event.NewEvent(lru.OptionEventName)
@ -98,7 +98,7 @@ func (s *serverSingle) Setbit(
req *proto.SetbitRequest,
) (*proto.SetbitResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return nil, s.dao.Setbit(req.Key.Key, req.Val, req.Offer)
return nil, s.dao.Setbit(req.Key, req.Val, req.Offer)
})
lruEvent := event.NewEvent(lru.OptionEventName)
@ -117,7 +117,7 @@ func (s *serverSingle) Getbit(
req *proto.GetbitRequest,
) (*proto.GetbitResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.GetBit(req.Key.Key, req.Offer)
return s.dao.GetBit(req.Key, req.Offer)
})
lruEvent := event.NewEvent(lru.OptionEventName)