forked from p93542168/wheat-cache
!52 增加了根据key删除value, 将key的类型修改成 KeyBase
Merge pull request !52 from K-on/feat-Lru-delete-key
This commit is contained in:
commit
60abd336b1
|
@ -1,6 +1,7 @@
|
||||||
package lru
|
package lru
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"gitee.com/timedb/wheatCache/pkg/proto"
|
||||||
"gitee.com/timedb/wheatCache/pkg/structure"
|
"gitee.com/timedb/wheatCache/pkg/structure"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
@ -26,7 +27,8 @@ const (
|
||||||
|
|
||||||
type CacheInterface interface {
|
type CacheInterface interface {
|
||||||
Del() error
|
Del() error
|
||||||
Get(key string) (structure.KeyBaseInterface, bool)
|
Get(key *proto.BaseKey) (structure.KeyBaseInterface, bool)
|
||||||
Add(key string, val structure.KeyBaseInterface)
|
Add(key *proto.BaseKey, val structure.KeyBaseInterface)
|
||||||
UpdateLruSize(length structure.UpdateLength)
|
UpdateLruSize(length structure.UpdateLength)
|
||||||
}
|
DelByKey(key *proto.BaseKey) error
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
_ "gitee.com/timedb/wheatCache/conf"
|
_ "gitee.com/timedb/wheatCache/conf"
|
||||||
"gitee.com/timedb/wheatCache/pkg/errorx"
|
"gitee.com/timedb/wheatCache/pkg/errorx"
|
||||||
"gitee.com/timedb/wheatCache/pkg/event"
|
"gitee.com/timedb/wheatCache/pkg/event"
|
||||||
|
"gitee.com/timedb/wheatCache/pkg/proto"
|
||||||
"gitee.com/timedb/wheatCache/pkg/structure"
|
"gitee.com/timedb/wheatCache/pkg/structure"
|
||||||
"gitee.com/timedb/wheatCache/pkg/util"
|
"gitee.com/timedb/wheatCache/pkg/util"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
@ -86,30 +87,30 @@ func (lru *SingleCache) GetDriver() event.DriverInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
//Add 增加
|
//Add 增加
|
||||||
func (lru *SingleCache) Add(key string, val structure.KeyBaseInterface) {
|
func (lru *SingleCache) Add(key *proto.BaseKey, val structure.KeyBaseInterface) {
|
||||||
|
|
||||||
keyBaseVal := &keyBaseValue{
|
keyBaseVal := &keyBaseValue{
|
||||||
key: key,
|
key: key.Key,
|
||||||
val: val,
|
val: val,
|
||||||
}
|
}
|
||||||
if elVal, ok := lru.lruMap[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
|
||||||
}
|
}
|
||||||
valEl := lru.li.PushFront(keyBaseVal)
|
valEl := lru.li.PushFront(keyBaseVal)
|
||||||
lru.lruMap[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()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get 查找key对应的value
|
// 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 {
|
if lru.lruMap == nil {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
if elVal, ok := lru.lruMap[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
|
||||||
}
|
}
|
||||||
|
@ -128,3 +129,15 @@ func (lru *SingleCache) Del() error {
|
||||||
lru.li.Remove(data)
|
lru.li.Remove(data)
|
||||||
return nil
|
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")
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ package lru
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gitee.com/timedb/wheatCache/pkg/proto"
|
||||||
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
|
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -12,13 +13,46 @@ func TestNewLRUCache(t *testing.T) {
|
||||||
v1 := stringx.NewStringSingle()
|
v1 := stringx.NewStringSingle()
|
||||||
v2 := stringx.NewStringSingle()
|
v2 := stringx.NewStringSingle()
|
||||||
v3 := stringx.NewStringSingle()
|
v3 := stringx.NewStringSingle()
|
||||||
cache.Add("1", v1)
|
key1 := proto.BaseKey{
|
||||||
cache.Add("2", v2)
|
Key: "1",
|
||||||
cache.Add("3", v3)
|
}
|
||||||
cache.Add("1", v1)
|
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)
|
fmt.Println(cache.nowSize)
|
||||||
cache.Del()
|
cache.Del()
|
||||||
fmt.Println(cache.nowSize)
|
fmt.Println(cache.nowSize)
|
||||||
_, isTrue := cache.Get("1")
|
_, isTrue := cache.Get(&key1)
|
||||||
require.Equal(t, isTrue, true)
|
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))
|
||||||
}
|
}
|
|
@ -3,6 +3,7 @@ package lru
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"gitee.com/timedb/wheatCache/pkg/event"
|
"gitee.com/timedb/wheatCache/pkg/event"
|
||||||
|
"gitee.com/timedb/wheatCache/pkg/proto"
|
||||||
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
|
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -16,9 +17,11 @@ func TestWorker(t *testing.T) {
|
||||||
workEvent := event.NewEvent(OptionEventName)
|
workEvent := event.NewEvent(OptionEventName)
|
||||||
workEvent.SetValue(WorkFuncEventKey, event.EventWorkFunc(func() (interface{}, error) {
|
workEvent.SetValue(WorkFuncEventKey, event.EventWorkFunc(func() (interface{}, error) {
|
||||||
v1 := stringx.NewStringSingle()
|
v1 := stringx.NewStringSingle()
|
||||||
key := "v1"
|
key := proto.BaseKey{
|
||||||
|
Key: "v1",
|
||||||
|
}
|
||||||
res, _ := v1.Set("123")
|
res, _ := v1.Set("123")
|
||||||
lru.Add(key, v1)
|
lru.Add(&key, v1)
|
||||||
return res, nil
|
return res, nil
|
||||||
}))
|
}))
|
||||||
workEvent.InitWaitEvent()
|
workEvent.InitWaitEvent()
|
||||||
|
|
|
@ -5,32 +5,35 @@ package structure
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DEFAULT_KEY = iota
|
DEFAULT_KEY = iota
|
||||||
|
|
||||||
STRING_X
|
STRING_X
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DEFAULT_COMM = iota
|
DEFAULT_COMM = iota
|
||||||
SET
|
SET
|
||||||
GET
|
GET
|
||||||
ADD
|
ADD
|
||||||
REDUCE
|
REDUCE
|
||||||
SETBIT
|
SETBIT
|
||||||
GETBIT
|
GETBIT
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var CommKeyString = map[string]int{"set": STRING_X,
|
var CommKeyString = map[string]int {"set": STRING_X,
|
||||||
"get": STRING_X,
|
"get": STRING_X,
|
||||||
"add": STRING_X,
|
"add": STRING_X,
|
||||||
"reduce": STRING_X,
|
"reduce": STRING_X,
|
||||||
"setbit": STRING_X,
|
"setbit": STRING_X,
|
||||||
"getbit": STRING_X,
|
"getbit": STRING_X,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var CommKey = map[int]int{SET: STRING_X,
|
var CommKey = map[int]int {SET: STRING_X,
|
||||||
GET: STRING_X,
|
GET: STRING_X,
|
||||||
ADD: STRING_X,
|
ADD: STRING_X,
|
||||||
REDUCE: STRING_X,
|
REDUCE: STRING_X,
|
||||||
SETBIT: STRING_X,
|
SETBIT: STRING_X,
|
||||||
GETBIT: STRING_X,
|
GETBIT: STRING_X,
|
||||||
}
|
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ package dao
|
||||||
import (
|
import (
|
||||||
"gitee.com/timedb/wheatCache/pkg/errorx"
|
"gitee.com/timedb/wheatCache/pkg/errorx"
|
||||||
"gitee.com/timedb/wheatCache/pkg/lru"
|
"gitee.com/timedb/wheatCache/pkg/lru"
|
||||||
|
"gitee.com/timedb/wheatCache/pkg/proto"
|
||||||
"gitee.com/timedb/wheatCache/pkg/structure"
|
"gitee.com/timedb/wheatCache/pkg/structure"
|
||||||
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
|
"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)
|
value, ok := d.lru.Get(key)
|
||||||
if ok {
|
if ok {
|
||||||
if val, ok := value.(structure.StringXInterface); 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
|
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)
|
val, ok := d.lru.Get(key)
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", errorx.NotKeyErr(key)
|
return "", errorx.NotKeyErr(key.Key)
|
||||||
}
|
}
|
||||||
|
|
||||||
strVal, ok := val.(structure.StringXInterface)
|
strVal, ok := val.(structure.StringXInterface)
|
||||||
|
@ -51,7 +52,7 @@ func (d *Dao) Get(key string) (string, error) {
|
||||||
return strVal.Get(), nil
|
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)
|
value, lruOk := d.lru.Get(key)
|
||||||
if !lruOk {
|
if !lruOk {
|
||||||
val := stringx.NewStringSingle()
|
val := stringx.NewStringSingle()
|
||||||
|
@ -75,7 +76,7 @@ func (d *Dao) Add(key string, renewal int32) (string, error) {
|
||||||
return res, nil
|
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)
|
value, lruOk := d.lru.Get(key)
|
||||||
if !lruOk {
|
if !lruOk {
|
||||||
val := stringx.NewStringSingle()
|
val := stringx.NewStringSingle()
|
||||||
|
@ -99,7 +100,7 @@ func (d *Dao) Reduce(key string, renewal int32) (string, error) {
|
||||||
return res, nil
|
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)
|
value, lruOk := d.lru.Get(key)
|
||||||
if !lruOk {
|
if !lruOk {
|
||||||
valStr := stringx.NewStringSingle()
|
valStr := stringx.NewStringSingle()
|
||||||
|
@ -119,10 +120,10 @@ func (d *Dao) Setbit(key string, val bool, offer int32) error {
|
||||||
return nil
|
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)
|
value, lruOk := d.lru.Get(key)
|
||||||
if !lruOk {
|
if !lruOk {
|
||||||
return false, errorx.NotKeyErr(key)
|
return false, errorx.NotKeyErr(key.Key)
|
||||||
}
|
}
|
||||||
strVal, ok := value.(structure.StringXInterface)
|
strVal, ok := value.(structure.StringXInterface)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
|
@ -13,7 +13,7 @@ func (s *serverSingle) Set(
|
||||||
) (*proto.SetResponse, error) {
|
) (*proto.SetResponse, error) {
|
||||||
|
|
||||||
work := event.EventWorkFunc(func() (interface{}, 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)
|
lruEvent := event.NewEvent(lru.OptionEventName)
|
||||||
|
@ -35,7 +35,7 @@ func (s *serverSingle) Get(
|
||||||
req *proto.GetRequest,
|
req *proto.GetRequest,
|
||||||
) (*proto.GetResponse, error) {
|
) (*proto.GetResponse, error) {
|
||||||
work := event.EventWorkFunc(func() (interface{}, 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)
|
lruEvent := event.NewEvent(lru.OptionEventName)
|
||||||
|
@ -56,7 +56,7 @@ func (s serverSingle) Add(
|
||||||
req *proto.AddRequest,
|
req *proto.AddRequest,
|
||||||
) (*proto.AddResponse, error) {
|
) (*proto.AddResponse, error) {
|
||||||
work := event.EventWorkFunc(func() (interface{}, 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)
|
lruEvent := event.NewEvent(lru.OptionEventName)
|
||||||
|
@ -77,7 +77,7 @@ func (s *serverSingle) Reduce(
|
||||||
req *proto.ReduceRequest,
|
req *proto.ReduceRequest,
|
||||||
) (*proto.ReduceResponse, error) {
|
) (*proto.ReduceResponse, error) {
|
||||||
work := event.EventWorkFunc(func() (interface{}, 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)
|
lruEvent := event.NewEvent(lru.OptionEventName)
|
||||||
|
@ -98,7 +98,7 @@ func (s *serverSingle) Setbit(
|
||||||
req *proto.SetbitRequest,
|
req *proto.SetbitRequest,
|
||||||
) (*proto.SetbitResponse, error) {
|
) (*proto.SetbitResponse, error) {
|
||||||
work := event.EventWorkFunc(func() (interface{}, 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)
|
lruEvent := event.NewEvent(lru.OptionEventName)
|
||||||
|
@ -117,7 +117,7 @@ func (s *serverSingle) Getbit(
|
||||||
req *proto.GetbitRequest,
|
req *proto.GetbitRequest,
|
||||||
) (*proto.GetbitResponse, error) {
|
) (*proto.GetbitResponse, error) {
|
||||||
work := event.EventWorkFunc(func() (interface{}, 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)
|
lruEvent := event.NewEvent(lru.OptionEventName)
|
||||||
|
|
Loading…
Reference in New Issue