From 73e91b3ff00ae8f4177f1ae69edd76878ac04b88 Mon Sep 17 00:00:00 2001 From: HuangJiaLuo <1820799930@qq.com> Date: Sun, 10 Oct 2021 20:53:34 +0800 Subject: [PATCH] test --- conf/wheat-cache.yaml | 2 +- pkg/lru/clean_work.go | 32 ++++++++++++++++++++++++++++++++ pkg/lru/cleanwork.go | 28 ---------------------------- pkg/lru/lru.go | 3 ++- pkg/lru/woker_test.go | 26 ++++++++++++++++++++++++++ pkg/middle-msg/lru.go | 3 +++ 6 files changed, 64 insertions(+), 30 deletions(-) create mode 100644 pkg/lru/clean_work.go delete mode 100644 pkg/lru/cleanwork.go create mode 100644 pkg/middle-msg/lru.go diff --git a/conf/wheat-cache.yaml b/conf/wheat-cache.yaml index 33925a8..dcf83ee 100644 --- a/conf/wheat-cache.yaml +++ b/conf/wheat-cache.yaml @@ -9,7 +9,7 @@ storage: # clearSize and maxSize must be Int lruCache: - clearSize: "512MB" + clearSize: "512mb" maxSize: "1GB" eventDriverSize: 2000 workTime: 1 diff --git a/pkg/lru/clean_work.go b/pkg/lru/clean_work.go new file mode 100644 index 0000000..6b08301 --- /dev/null +++ b/pkg/lru/clean_work.go @@ -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) + } + + } + } +} diff --git a/pkg/lru/cleanwork.go b/pkg/lru/cleanwork.go deleted file mode 100644 index af787dc..0000000 --- a/pkg/lru/cleanwork.go +++ /dev/null @@ -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 -} \ No newline at end of file diff --git a/pkg/lru/lru.go b/pkg/lru/lru.go index 2455a56..e109339 100644 --- a/pkg/lru/lru.go +++ b/pkg/lru/lru.go @@ -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 { diff --git a/pkg/lru/woker_test.go b/pkg/lru/woker_test.go index 77a6ad8..9e8a21b 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/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) +} diff --git a/pkg/middle-msg/lru.go b/pkg/middle-msg/lru.go new file mode 100644 index 0000000..b5aa092 --- /dev/null +++ b/pkg/middle-msg/lru.go @@ -0,0 +1,3 @@ +package middle_msg + +