diff --git a/pkg/lru/define.go b/pkg/lru/define.go index 98e82e2..4d05af5 100644 --- a/pkg/lru/define.go +++ b/pkg/lru/define.go @@ -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) -} \ No newline at end of file + DelByKey(key *proto.BaseKey) error +} diff --git a/pkg/lru/lru.go b/pkg/lru/lru.go index 40cc014..7ad8f41 100644 --- a/pkg/lru/lru.go +++ b/pkg/lru/lru.go @@ -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") +} \ No newline at end of file diff --git a/pkg/lru/lru_test.go b/pkg/lru/lru_test.go index 7cba185..2c925d1 100644 --- a/pkg/lru/lru_test.go +++ b/pkg/lru/lru_test.go @@ -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)) } \ No newline at end of file diff --git a/pkg/lru/woker_test.go b/pkg/lru/woker_test.go index ef1a757..77a6ad8 100644 --- a/pkg/lru/woker_test.go +++ b/pkg/lru/woker_test.go @@ -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() diff --git a/pkg/structure/const.gen.go b/pkg/structure/const.gen.go index dce796a..3c421d2 100644 --- a/pkg/structure/const.gen.go +++ b/pkg/structure/const.gen.go @@ -5,32 +5,35 @@ package structure const ( DEFAULT_KEY = iota - + STRING_X ) const ( - DEFAULT_COMM = iota - SET + DEFAULT_COMM = iota + SET GET ADD REDUCE SETBIT GETBIT + ) -var CommKeyString = map[string]int{"set": STRING_X, - "get": STRING_X, - "add": STRING_X, +var CommKeyString = map[string]int {"set": STRING_X, + "get": STRING_X, + "add": STRING_X, "reduce": STRING_X, "setbit": STRING_X, "getbit": STRING_X, + } -var CommKey = map[int]int{SET: STRING_X, - GET: STRING_X, - ADD: STRING_X, +var CommKey = map[int]int {SET: STRING_X, + GET: STRING_X, + ADD: STRING_X, REDUCE: STRING_X, SETBIT: STRING_X, GETBIT: STRING_X, -} + +} \ No newline at end of file diff --git a/storage/dao/dao.go b/storage/dao/dao.go index 6e8eb70..8cd9b04 100644 --- a/storage/dao/dao.go +++ b/storage/dao/dao.go @@ -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 { diff --git a/storage/server/single/stringx.go b/storage/server/single/stringx.go index 66f6e36..825ba77 100644 --- a/storage/server/single/stringx.go +++ b/storage/server/single/stringx.go @@ -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)