From a648beecb30f1c5e4590e5634dfbb4974e3db8fa Mon Sep 17 00:00:00 2001 From: "fengyun.rui" Date: Wed, 18 Oct 2023 19:47:57 +0800 Subject: [PATCH] optimize: reduce lock latency for conversation cache (#1231) Signed-off-by: rfyiamcool --- pkg/common/db/localcache/conversation.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/common/db/localcache/conversation.go b/pkg/common/db/localcache/conversation.go index 3ede6fd4c8..c40bcdbce6 100644 --- a/pkg/common/db/localcache/conversation.go +++ b/pkg/common/db/localcache/conversation.go @@ -60,9 +60,11 @@ func (g *ConversationLocalCache) GetConversationIDs(ctx context.Context, userID if err != nil { return nil, err } + g.lock.Lock() - defer g.lock.Unlock() hash, ok := g.conversationIDs[userID] + g.lock.Unlock() + if !ok || hash.hash != resp.Hash { conversationIDsResp, err := g.client.Client.GetConversationIDs(ctx, &conversation.GetConversationIDsReq{ UserID: userID, @@ -70,11 +72,16 @@ func (g *ConversationLocalCache) GetConversationIDs(ctx context.Context, userID if err != nil { return nil, err } + + g.lock.Lock() + defer g.lock.Unlock() g.conversationIDs[userID] = Hash{ hash: resp.Hash, ids: conversationIDsResp.ConversationIDs, } + return conversationIDsResp.ConversationIDs, nil } + return hash.ids, nil }