From f1506e40c469ac6e6c4d7f3dd4aa8a2c565e1dcd Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Tue, 16 Jan 2024 15:53:00 +0800 Subject: [PATCH 1/2] feat: local cache --- internal/rpc/msg/verify.go | 8 +------- pkg/rpccache/conversation.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/internal/rpc/msg/verify.go b/internal/rpc/msg/verify.go index 8c494beb8..cd569a3ea 100644 --- a/internal/rpc/msg/verify.go +++ b/internal/rpc/msg/verify.go @@ -204,13 +204,7 @@ func (m *msgServer) modifyMessageByUserMessageReceiveOpt( return true, nil } // conversationID := utils.GetConversationIDBySessionType(conversationID, sessionType) - log.ZDebug(ctx, "GetSingleConversationRecvMsgOpt req", "userID", userID, "conversationID", conversationID) - singleOpt, err := m.Conversation.GetSingleConversationRecvMsgOpt(ctx, userID, conversationID) // todo local cache - if err == nil { - log.ZDebug(ctx, "GetSingleConversationRecvMsgOpt resp", "userID", userID, "conversationID", conversationID, "RecvMsgOpt", singleOpt) - } else { - log.ZError(ctx, "GetSingleConversationRecvMsgOpt resp", err, "userID", userID, "conversationID", conversationID, "RecvMsgOpt", singleOpt) - } + singleOpt, err := m.ConversationLocalCache.GetSingleConversationRecvMsgOpt(ctx, userID, conversationID) if errs.ErrRecordNotFound.Is(err) { return true, nil } else if err != nil { diff --git a/pkg/rpccache/conversation.go b/pkg/rpccache/conversation.go index 45c37529b..8ca2f323f 100644 --- a/pkg/rpccache/conversation.go +++ b/pkg/rpccache/conversation.go @@ -2,6 +2,7 @@ package rpccache import ( "context" + pbconversation "github.com/OpenIMSDK/protocol/conversation" "github.com/OpenIMSDK/tools/log" "github.com/openimsdk/localcache" "github.com/openimsdk/open-im-server/v3/pkg/common/cachekey" @@ -44,3 +45,26 @@ func (c *ConversationLocalCache) GetConversationIDs(ctx context.Context, ownerUs return c.client.GetConversationIDs(ctx, ownerUserID) })) } + +func (c *ConversationLocalCache) GetConversation(ctx context.Context, userID, conversationID string) (val *pbconversation.Conversation, err error) { + log.ZDebug(ctx, "ConversationLocalCache GetConversation req", "userID", userID, "conversationID", conversationID) + defer func() { + if err == nil { + log.ZDebug(ctx, "ConversationLocalCache GetConversation return", "value", val) + } else { + log.ZError(ctx, "ConversationLocalCache GetConversation return", err) + } + }() + return localcache.AnyValue[*pbconversation.Conversation](c.local.Get(ctx, cachekey.GetConversationKey(userID, conversationID), func(ctx context.Context) (any, error) { + log.ZDebug(ctx, "ConversationLocalCache GetConversation rpc", "userID", userID, "conversationID", conversationID) + return c.client.GetConversation(ctx, userID, conversationID) + })) +} + +func (c *ConversationLocalCache) GetSingleConversationRecvMsgOpt(ctx context.Context, userID, conversationID string) (int32, error) { + conv, err := c.GetConversation(ctx, userID, conversationID) + if err != nil { + return 0, err + } + return conv.RecvMsgOpt, nil +} From f53dd855aa47b91c2ce654e6732a54b9419b02f7 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Tue, 16 Jan 2024 16:42:47 +0800 Subject: [PATCH 2/2] feat: local cache --- pkg/rpccache/conversation.go | 1 + pkg/rpccache/friend.go | 1 + pkg/rpccache/group.go | 1 + 3 files changed, 3 insertions(+) diff --git a/pkg/rpccache/conversation.go b/pkg/rpccache/conversation.go index 8ca2f323f..95520f7cb 100644 --- a/pkg/rpccache/conversation.go +++ b/pkg/rpccache/conversation.go @@ -13,6 +13,7 @@ import ( func NewConversationLocalCache(client rpcclient.ConversationRpcClient, cli redis.UniversalClient) *ConversationLocalCache { lc := config.Config.LocalCache.Conversation + log.ZDebug(context.Background(), "ConversationLocalCache", "topic", lc.Topic, "slotNum", lc.SlotNum, "slotSize", lc.SlotSize, "enable", lc.Enable()) x := &ConversationLocalCache{ client: client, local: localcache.New[any]( diff --git a/pkg/rpccache/friend.go b/pkg/rpccache/friend.go index f1dec36cc..d9ee377b3 100644 --- a/pkg/rpccache/friend.go +++ b/pkg/rpccache/friend.go @@ -12,6 +12,7 @@ import ( func NewFriendLocalCache(client rpcclient.FriendRpcClient, cli redis.UniversalClient) *FriendLocalCache { lc := config.Config.LocalCache.Friend + log.ZDebug(context.Background(), "FriendLocalCache", "topic", lc.Topic, "slotNum", lc.SlotNum, "slotSize", lc.SlotSize, "enable", lc.Enable()) x := &FriendLocalCache{ client: client, local: localcache.New[any]( diff --git a/pkg/rpccache/group.go b/pkg/rpccache/group.go index 4c3e32170..c3dfd9d93 100644 --- a/pkg/rpccache/group.go +++ b/pkg/rpccache/group.go @@ -12,6 +12,7 @@ import ( func NewGroupLocalCache(client rpcclient.GroupRpcClient, cli redis.UniversalClient) *GroupLocalCache { lc := config.Config.LocalCache.Group + log.ZDebug(context.Background(), "GroupLocalCache", "topic", lc.Topic, "slotNum", lc.SlotNum, "slotSize", lc.SlotSize, "enable", lc.Enable()) x := &GroupLocalCache{ client: client, local: localcache.New[any](