forked from p93542168/wheat-cache
test
This commit is contained in:
parent
dcce00a32d
commit
73e91b3ff0
|
@ -9,7 +9,7 @@ storage:
|
|||
|
||||
# clearSize and maxSize must be Int
|
||||
lruCache:
|
||||
clearSize: "512MB"
|
||||
clearSize: "512mb"
|
||||
maxSize: "1GB"
|
||||
eventDriverSize: 2000
|
||||
workTime: 1
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
package lru
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gitee.com/timedb/wheatCache/pkg/event"
|
||||
"gitee.com/timedb/wheatCache/pkg/logx"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (lru *SingleCache) cleanWork() {
|
||||
cxt := context.Background()
|
||||
for {
|
||||
time.Sleep(2 * time.Second)
|
||||
if lru.clearSize < lru.nowSize {
|
||||
lruCleanEvent := event.NewEvent(CleanEventName)
|
||||
lruCleanEvent.InitWaitEvent()
|
||||
work := event.EventWorkFunc(func() (interface{}, error) {
|
||||
err := lru.DelToClearSize()
|
||||
return nil, err
|
||||
})
|
||||
|
||||
lruCleanEvent.SetValue(WorkFuncEventKey, work)
|
||||
|
||||
lru.lruCleanProduce.Call(cxt, lruCleanEvent)
|
||||
_, err := lruCleanEvent.StartWaitEvent(20 * time.Minute) //常量
|
||||
if err != nil {
|
||||
logx.With(cxt, ).Error("cleanWork err: %v", err)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
package lru
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gitee.com/timedb/wheatCache/pkg/event"
|
||||
"gitee.com/timedb/wheatCache/pkg/proto"
|
||||
"time"
|
||||
)
|
||||
|
||||
type cleanWorkSingle struct {
|
||||
timeOut time.Duration
|
||||
lru *SingleCache
|
||||
}
|
||||
|
||||
func (clean *cleanWorkSingle) cleanWork() (*proto.ReduceResponse, error) {
|
||||
cxt := context.Background()
|
||||
lruCleanEvent := event.NewEvent(CleanEventName)
|
||||
lruCleanEvent.InitWaitEvent()
|
||||
lruCleanEvent.SetValue(WorkFuncEventKey, clean.lru.DelToClearSize())
|
||||
clean.lru.lruCleanProduce.Call(cxt, lruCleanEvent)
|
||||
resp, err := lruCleanEvent.StartWaitEvent(clean.timeOut)
|
||||
if err != nil{
|
||||
return nil, err
|
||||
}
|
||||
return &proto.ReduceResponse{
|
||||
Result: resp.(string),
|
||||
}, nil
|
||||
}
|
|
@ -78,6 +78,7 @@ func NewLRUCache() *SingleCache {
|
|||
}
|
||||
lruCache = lru
|
||||
go lru.lruSingleWork()
|
||||
go lru.cleanWork()
|
||||
})
|
||||
return lruCache
|
||||
}
|
||||
|
@ -148,7 +149,7 @@ func (lru *SingleCache) DelToClearSize() error {
|
|||
return errorx.New("lru is nil")
|
||||
}
|
||||
for {
|
||||
if lru.nowSize > lru.clearSize*1/3 {
|
||||
if lru.nowSize > lru.clearSize {
|
||||
//del自动给nowSize进行大小的改变
|
||||
lru.Del()
|
||||
} else {
|
||||
|
|
|
@ -3,6 +3,7 @@ package lru
|
|||
import (
|
||||
"context"
|
||||
"gitee.com/timedb/wheatCache/pkg/event"
|
||||
"gitee.com/timedb/wheatCache/pkg/logx"
|
||||
"gitee.com/timedb/wheatCache/pkg/proto"
|
||||
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
@ -30,3 +31,28 @@ func TestWorker(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
require.Equal(t, res, "123")
|
||||
}
|
||||
|
||||
func TestSingleCache_DelToClearSize(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
lru := NewLRUCache()
|
||||
produce := event.NewProduce(lru.GetDriver())
|
||||
|
||||
for i := int32(20000); i >= 1; i-- {
|
||||
workEvent := event.NewEvent(OptionEventName)
|
||||
workEvent.SetValue(WorkFuncEventKey, event.EventWorkFunc(func() (interface{}, error) {
|
||||
v1 := stringx.NewStringSingle()
|
||||
key := proto.BaseKey{
|
||||
Key: string(i),
|
||||
}
|
||||
u := v1.Setbit(i, true)
|
||||
lru.Add(&key, v1)
|
||||
return u, nil
|
||||
}))
|
||||
workEvent.InitWaitEvent()
|
||||
produce.Call(ctx, workEvent)
|
||||
workEvent.StartWaitEvent(2 * time.Second)
|
||||
}
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
logx.Info("end size is %d", lru.nowSize)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
package middle_msg
|
||||
|
||||
|
Loading…
Reference in New Issue