Skip to content

Commit

Permalink
optimize: reduce lock latency for conversation cache (#1231)
Browse files Browse the repository at this point in the history
Signed-off-by: rfyiamcool <[email protected]>
  • Loading branch information
rfyiamcool authored Oct 18, 2023
1 parent abf8e37 commit a648bee
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/common/db/localcache/conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,28 @@ 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,
})
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
}

0 comments on commit a648bee

Please sign in to comment.