From 9e4a7d082d2e3b06beb450417a3f213ec980da14 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Tue, 16 Jan 2024 17:01:38 +0800 Subject: [PATCH 1/3] feat: local cache --- pkg/common/db/cache/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/common/db/cache/config.go b/pkg/common/db/cache/config.go index dc5c55143..7fd08e247 100644 --- a/pkg/common/db/cache/config.go +++ b/pkg/common/db/cache/config.go @@ -31,7 +31,7 @@ func getPublishKey(topic string, key []string) []string { }, { Local: config.Config.LocalCache.Conversation, - Keys: []string{cachekey.ConversationIDsKey}, + Keys: []string{cachekey.ConversationIDsKey, cachekey.ConversationKey}, }, } subscribe = make(map[string][]string) From 4a042489bfc3f5eff53652ea1e6d8d7cb592fad8 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Tue, 16 Jan 2024 17:19:29 +0800 Subject: [PATCH 2/3] feat: local cache --- internal/push/push_rpc_server.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/push/push_rpc_server.go b/internal/push/push_rpc_server.go index 8a9da0577..92a6c4b8f 100644 --- a/internal/push/push_rpc_server.go +++ b/internal/push/push_rpc_server.go @@ -51,8 +51,8 @@ func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e client, offlinePusher, database, - rpccache.NewGroupLocalCache(rpcclient.NewGroupRpcClient(client), rdb), - rpccache.NewConversationLocalCache(rpcclient.NewConversationRpcClient(client), rdb), + rpccache.NewGroupLocalCache(groupRpcClient, rdb), + rpccache.NewConversationLocalCache(conversationRpcClient, rdb), &conversationRpcClient, &groupRpcClient, &msgRpcClient, From af5e191144440f88adff61cfaef09d32d83bb674 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Tue, 16 Jan 2024 17:46:28 +0800 Subject: [PATCH 3/3] feat: local cache --- pkg/localcache/cache_test.go | 79 ++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 pkg/localcache/cache_test.go diff --git a/pkg/localcache/cache_test.go b/pkg/localcache/cache_test.go new file mode 100644 index 000000000..90413fd20 --- /dev/null +++ b/pkg/localcache/cache_test.go @@ -0,0 +1,79 @@ +package localcache + +import ( + "context" + "fmt" + "math/rand" + "sync" + "sync/atomic" + "testing" + "time" +) + +func TestName(t *testing.T) { + c := New[string](WithActively()) + //c := New[string]() + ctx := context.Background() + + const ( + num = 10000 + tNum = 10000 + kNum = 100000 + pNum = 100 + ) + + getKey := func(v uint64) string { + return fmt.Sprintf("key_%d", v%kNum) + } + + start := time.Now() + t.Log("start", start) + + var ( + get atomic.Int64 + del atomic.Int64 + ) + + incrGet := func() { + if v := get.Add(1); v%pNum == 0 { + //t.Log("#get count", v/pNum) + } + } + incrDel := func() { + if v := del.Add(1); v%pNum == 0 { + //t.Log("@del count", v/pNum) + } + } + + var wg sync.WaitGroup + + for i := 0; i < tNum; i++ { + wg.Add(2) + go func() { + defer wg.Done() + for i := 0; i < num; i++ { + c.Get(ctx, getKey(rand.Uint64()), func(ctx context.Context) (string, error) { + return fmt.Sprintf("index_%d", i), nil + }) + incrGet() + } + }() + + go func() { + defer wg.Done() + time.Sleep(time.Second / 10) + for i := 0; i < num; i++ { + c.Del(ctx, getKey(rand.Uint64())) + incrDel() + } + }() + } + + wg.Wait() + end := time.Now() + t.Log("end", end) + t.Log("time", end.Sub(start)) + t.Log("get", get.Load()) + t.Log("del", del.Load()) + // 137.35s +}