mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-01-11 15:32:25 +08:00
feat: solve the problem that modifying the cached data affects other
This commit is contained in:
parent
04f440f1ef
commit
548f45ebd3
@ -14,6 +14,11 @@
|
||||
|
||||
package rpccache
|
||||
|
||||
import (
|
||||
"github.com/openimsdk/tools/errs"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
func newListMap[V comparable](values []V, err error) (*listMap[V], error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -32,3 +37,41 @@ type listMap[V comparable] struct {
|
||||
List []V
|
||||
Map map[V]struct{}
|
||||
}
|
||||
|
||||
func respProtoMarshal(resp proto.Message, err error) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return proto.Marshal(resp)
|
||||
}
|
||||
|
||||
func cacheUnmarshal[V any](resp []byte, err error) (*V, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var val V
|
||||
if err := proto.Unmarshal(resp, any(&val).(proto.Message)); err != nil {
|
||||
return nil, errs.WrapMsg(err, "local cache proto.Unmarshal error")
|
||||
}
|
||||
return &val, nil
|
||||
}
|
||||
|
||||
type cacheProto[V any] struct{}
|
||||
|
||||
func (cacheProto[V]) Marshal(resp *V, err error) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return proto.Marshal(any(resp).(proto.Message))
|
||||
}
|
||||
|
||||
func (cacheProto[V]) Unmarshal(resp []byte, err error) (*V, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var val V
|
||||
if err := proto.Unmarshal(resp, any(&val).(proto.Message)); err != nil {
|
||||
return nil, errs.WrapMsg(err, "local cache proto.Unmarshal error")
|
||||
}
|
||||
return &val, nil
|
||||
}
|
||||
|
||||
@ -23,6 +23,7 @@ import (
|
||||
pbconversation "github.com/openimsdk/protocol/conversation"
|
||||
"github.com/openimsdk/tools/errs"
|
||||
"github.com/openimsdk/tools/log"
|
||||
"github.com/openimsdk/tools/utils/datautil"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
@ -36,7 +37,7 @@ func NewConversationLocalCache(client rpcclient.ConversationRpcClient, localCach
|
||||
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](
|
||||
local: localcache.New[[]byte](
|
||||
localcache.WithLocalSlotNum(lc.SlotNum),
|
||||
localcache.WithLocalSlotSize(lc.SlotSize),
|
||||
localcache.WithLinkSlotNum(lc.SlotNum),
|
||||
@ -52,21 +53,30 @@ func NewConversationLocalCache(client rpcclient.ConversationRpcClient, localCach
|
||||
|
||||
type ConversationLocalCache struct {
|
||||
client rpcclient.ConversationRpcClient
|
||||
local localcache.Cache[any]
|
||||
local localcache.Cache[[]byte]
|
||||
}
|
||||
|
||||
func (c *ConversationLocalCache) GetConversationIDs(ctx context.Context, ownerUserID string) (val []string, err error) {
|
||||
log.ZDebug(ctx, "ConversationLocalCache GetConversationIDs req", "ownerUserID", ownerUserID)
|
||||
resp, err := c.getConversationIDs(ctx, ownerUserID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.ConversationIDs, nil
|
||||
}
|
||||
|
||||
func (c *ConversationLocalCache) getConversationIDs(ctx context.Context, ownerUserID string) (val *pbconversation.GetConversationIDsResp, err error) {
|
||||
log.ZDebug(ctx, "ConversationLocalCache getConversationIDs req", "ownerUserID", ownerUserID)
|
||||
defer func() {
|
||||
if err == nil {
|
||||
log.ZDebug(ctx, "ConversationLocalCache GetConversationIDs return", "ownerUserID", ownerUserID, "value", val)
|
||||
log.ZDebug(ctx, "ConversationLocalCache getConversationIDs return", "ownerUserID", ownerUserID, "value", val)
|
||||
} else {
|
||||
log.ZError(ctx, "ConversationLocalCache GetConversationIDs return", err, "ownerUserID", ownerUserID)
|
||||
log.ZError(ctx, "ConversationLocalCache getConversationIDs return", err, "ownerUserID", ownerUserID)
|
||||
}
|
||||
}()
|
||||
return localcache.AnyValue[[]string](c.local.Get(ctx, cachekey.GetConversationIDsKey(ownerUserID), func(ctx context.Context) (any, error) {
|
||||
log.ZDebug(ctx, "ConversationLocalCache GetConversationIDs rpc", "ownerUserID", ownerUserID)
|
||||
return c.client.GetConversationIDs(ctx, ownerUserID)
|
||||
var cache cacheProto[pbconversation.GetConversationIDsResp]
|
||||
return cache.Unmarshal(c.local.Get(ctx, cachekey.GetConversationIDsKey(ownerUserID), func(ctx context.Context) ([]byte, error) {
|
||||
log.ZDebug(ctx, "ConversationLocalCache getConversationIDs rpc", "ownerUserID", ownerUserID)
|
||||
return cache.Marshal(c.client.Client.GetConversationIDs(ctx, &pbconversation.GetConversationIDsReq{UserID: ownerUserID}))
|
||||
}))
|
||||
}
|
||||
|
||||
@ -79,9 +89,10 @@ func (c *ConversationLocalCache) GetConversation(ctx context.Context, userID, co
|
||||
log.ZError(ctx, "ConversationLocalCache GetConversation return", err, "userID", userID, "conversationID", conversationID)
|
||||
}
|
||||
}()
|
||||
return localcache.AnyValue[*pbconversation.Conversation](c.local.Get(ctx, cachekey.GetConversationKey(userID, conversationID), func(ctx context.Context) (any, error) {
|
||||
var cache cacheProto[pbconversation.Conversation]
|
||||
return cache.Unmarshal(c.local.Get(ctx, cachekey.GetConversationKey(userID, conversationID), func(ctx context.Context) ([]byte, error) {
|
||||
log.ZDebug(ctx, "ConversationLocalCache GetConversation rpc", "userID", userID, "conversationID", conversationID)
|
||||
return c.client.GetConversation(ctx, userID, conversationID)
|
||||
return cache.Marshal(c.client.GetConversation(ctx, userID, conversationID))
|
||||
}))
|
||||
}
|
||||
|
||||
@ -126,9 +137,19 @@ func (c *ConversationLocalCache) GetConversations(ctx context.Context, ownerUser
|
||||
return conversations, nil
|
||||
}
|
||||
|
||||
func (c *ConversationLocalCache) getConversationNotReceiveMessageUserIDs(ctx context.Context, conversationID string) (*listMap[string], error) {
|
||||
return localcache.AnyValue[*listMap[string]](c.local.Get(ctx, cachekey.GetConversationNotReceiveMessageUserIDsKey(conversationID), func(ctx context.Context) (any, error) {
|
||||
return newListMap(c.client.GetConversationNotReceiveMessageUserIDs(ctx, conversationID))
|
||||
func (c *ConversationLocalCache) getConversationNotReceiveMessageUserIDs(ctx context.Context, conversationID string) (val *pbconversation.GetConversationNotReceiveMessageUserIDsResp, err error) {
|
||||
log.ZDebug(ctx, "ConversationLocalCache getConversationNotReceiveMessageUserIDs req", "conversationID", conversationID)
|
||||
defer func() {
|
||||
if err == nil {
|
||||
log.ZDebug(ctx, "ConversationLocalCache getConversationNotReceiveMessageUserIDs return", "conversationID", conversationID, "value", val)
|
||||
} else {
|
||||
log.ZError(ctx, "ConversationLocalCache getConversationNotReceiveMessageUserIDs return", err, "conversationID", conversationID)
|
||||
}
|
||||
}()
|
||||
var cache cacheProto[pbconversation.GetConversationNotReceiveMessageUserIDsResp]
|
||||
return cache.Unmarshal(c.local.Get(ctx, cachekey.GetConversationNotReceiveMessageUserIDsKey(conversationID), func(ctx context.Context) ([]byte, error) {
|
||||
log.ZDebug(ctx, "ConversationLocalCache getConversationNotReceiveMessageUserIDs rpc", "conversationID", conversationID)
|
||||
return cache.Marshal(c.client.Client.GetConversationNotReceiveMessageUserIDs(ctx, &pbconversation.GetConversationNotReceiveMessageUserIDsReq{ConversationID: conversationID}))
|
||||
}))
|
||||
}
|
||||
|
||||
@ -137,7 +158,7 @@ func (c *ConversationLocalCache) GetConversationNotReceiveMessageUserIDs(ctx con
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res.List, nil
|
||||
return res.UserIDs, nil
|
||||
}
|
||||
|
||||
func (c *ConversationLocalCache) GetConversationNotReceiveMessageUserIDMap(ctx context.Context, conversationID string) (map[string]struct{}, error) {
|
||||
@ -145,5 +166,5 @@ func (c *ConversationLocalCache) GetConversationNotReceiveMessageUserIDMap(ctx c
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res.Map, nil
|
||||
return datautil.SliceSet(res.UserIDs), nil
|
||||
}
|
||||
|
||||
@ -16,7 +16,8 @@ package rpccache
|
||||
|
||||
import (
|
||||
"context"
|
||||
cachekey2 "github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey"
|
||||
"github.com/openimsdk/protocol/relation"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/localcache"
|
||||
@ -30,7 +31,7 @@ func NewFriendLocalCache(client rpcclient.FriendRpcClient, localCache *config.Lo
|
||||
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](
|
||||
local: localcache.New[[]byte](
|
||||
localcache.WithLocalSlotNum(lc.SlotNum),
|
||||
localcache.WithLocalSlotSize(lc.SlotSize),
|
||||
localcache.WithLinkSlotNum(lc.SlotNum),
|
||||
@ -46,36 +47,55 @@ func NewFriendLocalCache(client rpcclient.FriendRpcClient, localCache *config.Lo
|
||||
|
||||
type FriendLocalCache struct {
|
||||
client rpcclient.FriendRpcClient
|
||||
local localcache.Cache[any]
|
||||
local localcache.Cache[[]byte]
|
||||
}
|
||||
|
||||
func (f *FriendLocalCache) IsFriend(ctx context.Context, possibleFriendUserID, userID string) (val bool, err error) {
|
||||
log.ZDebug(ctx, "FriendLocalCache IsFriend req", "possibleFriendUserID", possibleFriendUserID, "userID", userID)
|
||||
res, err := f.isFriend(ctx, possibleFriendUserID, userID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return res.InUser1Friends, nil
|
||||
}
|
||||
|
||||
func (f *FriendLocalCache) isFriend(ctx context.Context, possibleFriendUserID, userID string) (val *relation.IsFriendResp, err error) {
|
||||
log.ZDebug(ctx, "FriendLocalCache isFriend req", "possibleFriendUserID", possibleFriendUserID, "userID", userID)
|
||||
defer func() {
|
||||
if err == nil {
|
||||
log.ZDebug(ctx, "FriendLocalCache IsFriend return", "possibleFriendUserID", possibleFriendUserID, "userID", userID, "value", val)
|
||||
log.ZDebug(ctx, "FriendLocalCache isFriend return", "possibleFriendUserID", possibleFriendUserID, "userID", userID, "value", val)
|
||||
} else {
|
||||
log.ZError(ctx, "FriendLocalCache IsFriend return", err, "possibleFriendUserID", possibleFriendUserID, "userID", userID)
|
||||
log.ZError(ctx, "FriendLocalCache isFriend return", err, "possibleFriendUserID", possibleFriendUserID, "userID", userID)
|
||||
}
|
||||
}()
|
||||
return localcache.AnyValue[bool](f.local.GetLink(ctx, cachekey2.GetIsFriendKey(possibleFriendUserID, userID), func(ctx context.Context) (any, error) {
|
||||
log.ZDebug(ctx, "FriendLocalCache IsFriend rpc", "possibleFriendUserID", possibleFriendUserID, "userID", userID)
|
||||
return f.client.IsFriend(ctx, possibleFriendUserID, userID)
|
||||
}, cachekey2.GetFriendIDsKey(possibleFriendUserID)))
|
||||
var cache cacheProto[relation.IsFriendResp]
|
||||
return cache.Unmarshal(f.local.GetLink(ctx, cachekey.GetIsFriendKey(possibleFriendUserID, userID), func(ctx context.Context) ([]byte, error) {
|
||||
log.ZDebug(ctx, "FriendLocalCache isFriend rpc", "possibleFriendUserID", possibleFriendUserID, "userID", userID)
|
||||
return cache.Marshal(f.client.Client.IsFriend(ctx, &relation.IsFriendReq{UserID1: userID, UserID2: possibleFriendUserID}))
|
||||
}, cachekey.GetFriendIDsKey(possibleFriendUserID)))
|
||||
}
|
||||
|
||||
// IsBlack possibleBlackUserID selfUserID.
|
||||
func (f *FriendLocalCache) IsBlack(ctx context.Context, possibleBlackUserID, userID string) (val bool, err error) {
|
||||
log.ZDebug(ctx, "FriendLocalCache IsBlack req", "possibleBlackUserID", possibleBlackUserID, "userID", userID)
|
||||
res, err := f.isBlack(ctx, possibleBlackUserID, userID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return res.InUser2Blacks, nil
|
||||
}
|
||||
|
||||
// IsBlack possibleBlackUserID selfUserID.
|
||||
func (f *FriendLocalCache) isBlack(ctx context.Context, possibleBlackUserID, userID string) (val *relation.IsBlackResp, err error) {
|
||||
log.ZDebug(ctx, "FriendLocalCache isBlack req", "possibleBlackUserID", possibleBlackUserID, "userID", userID)
|
||||
defer func() {
|
||||
if err == nil {
|
||||
log.ZDebug(ctx, "FriendLocalCache IsBlack return", "possibleBlackUserID", possibleBlackUserID, "userID", userID, "value", val)
|
||||
log.ZDebug(ctx, "FriendLocalCache isBlack return", "possibleBlackUserID", possibleBlackUserID, "userID", userID, "value", val)
|
||||
} else {
|
||||
log.ZError(ctx, "FriendLocalCache IsBlack return", err, "possibleBlackUserID", possibleBlackUserID, "userID", userID)
|
||||
log.ZError(ctx, "FriendLocalCache isBlack return", err, "possibleBlackUserID", possibleBlackUserID, "userID", userID)
|
||||
}
|
||||
}()
|
||||
return localcache.AnyValue[bool](f.local.GetLink(ctx, cachekey2.GetIsBlackIDsKey(possibleBlackUserID, userID), func(ctx context.Context) (any, error) {
|
||||
var cache cacheProto[relation.IsBlackResp]
|
||||
return cache.Unmarshal(f.local.GetLink(ctx, cachekey.GetIsBlackIDsKey(possibleBlackUserID, userID), func(ctx context.Context) ([]byte, error) {
|
||||
log.ZDebug(ctx, "FriendLocalCache IsBlack rpc", "possibleBlackUserID", possibleBlackUserID, "userID", userID)
|
||||
return f.client.IsBlack(ctx, possibleBlackUserID, userID)
|
||||
}, cachekey2.GetBlackIDsKey(userID)))
|
||||
return cache.Marshal(f.client.Client.IsBlack(ctx, &relation.IsBlackReq{UserID1: possibleBlackUserID, UserID2: userID}))
|
||||
}, cachekey.GetBlackIDsKey(userID)))
|
||||
}
|
||||
|
||||
@ -17,6 +17,8 @@ package rpccache
|
||||
import (
|
||||
"context"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey"
|
||||
"github.com/openimsdk/protocol/group"
|
||||
"github.com/openimsdk/tools/utils/datautil"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/localcache"
|
||||
@ -32,7 +34,7 @@ func NewGroupLocalCache(client rpcclient.GroupRpcClient, localCache *config.Loca
|
||||
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](
|
||||
local: localcache.New[[]byte](
|
||||
localcache.WithLocalSlotNum(lc.SlotNum),
|
||||
localcache.WithLocalSlotSize(lc.SlotSize),
|
||||
localcache.WithLinkSlotNum(lc.SlotNum),
|
||||
@ -48,10 +50,10 @@ func NewGroupLocalCache(client rpcclient.GroupRpcClient, localCache *config.Loca
|
||||
|
||||
type GroupLocalCache struct {
|
||||
client rpcclient.GroupRpcClient
|
||||
local localcache.Cache[any]
|
||||
local localcache.Cache[[]byte]
|
||||
}
|
||||
|
||||
func (g *GroupLocalCache) getGroupMemberIDs(ctx context.Context, groupID string) (val *listMap[string], err error) {
|
||||
func (g *GroupLocalCache) getGroupMemberIDs(ctx context.Context, groupID string) (val *group.GetGroupMemberUserIDsResp, err error) {
|
||||
log.ZDebug(ctx, "GroupLocalCache getGroupMemberIDs req", "groupID", groupID)
|
||||
defer func() {
|
||||
if err == nil {
|
||||
@ -60,9 +62,10 @@ func (g *GroupLocalCache) getGroupMemberIDs(ctx context.Context, groupID string)
|
||||
log.ZError(ctx, "GroupLocalCache getGroupMemberIDs return", err, "groupID", groupID)
|
||||
}
|
||||
}()
|
||||
return localcache.AnyValue[*listMap[string]](g.local.Get(ctx, cachekey.GetGroupMemberIDsKey(groupID), func(ctx context.Context) (any, error) {
|
||||
var cache cacheProto[group.GetGroupMemberUserIDsResp]
|
||||
return cache.Unmarshal(g.local.Get(ctx, cachekey.GetGroupMemberIDsKey(groupID), func(ctx context.Context) ([]byte, error) {
|
||||
log.ZDebug(ctx, "GroupLocalCache getGroupMemberIDs rpc", "groupID", groupID)
|
||||
return newListMap(g.client.GetGroupMemberIDs(ctx, groupID))
|
||||
return cache.Marshal(g.client.Client.GetGroupMemberUserIDs(ctx, &group.GetGroupMemberUserIDsReq{GroupID: groupID}))
|
||||
}))
|
||||
}
|
||||
|
||||
@ -75,9 +78,10 @@ func (g *GroupLocalCache) GetGroupMember(ctx context.Context, groupID, userID st
|
||||
log.ZError(ctx, "GroupLocalCache GetGroupInfo return", err, "groupID", groupID, "userID", userID)
|
||||
}
|
||||
}()
|
||||
return localcache.AnyValue[*sdkws.GroupMemberFullInfo](g.local.Get(ctx, cachekey.GetGroupMemberInfoKey(groupID, userID), func(ctx context.Context) (any, error) {
|
||||
var cache cacheProto[sdkws.GroupMemberFullInfo]
|
||||
return cache.Unmarshal(g.local.Get(ctx, cachekey.GetGroupMemberInfoKey(groupID, userID), func(ctx context.Context) ([]byte, error) {
|
||||
log.ZDebug(ctx, "GroupLocalCache GetGroupInfo rpc", "groupID", groupID, "userID", userID)
|
||||
return g.client.GetGroupMemberCache(ctx, groupID, userID)
|
||||
return cache.Marshal(g.client.GetGroupMemberCache(ctx, groupID, userID))
|
||||
}))
|
||||
}
|
||||
|
||||
@ -90,9 +94,10 @@ func (g *GroupLocalCache) GetGroupInfo(ctx context.Context, groupID string) (val
|
||||
log.ZError(ctx, "GroupLocalCache GetGroupInfo return", err, "groupID", groupID)
|
||||
}
|
||||
}()
|
||||
return localcache.AnyValue[*sdkws.GroupInfo](g.local.Get(ctx, cachekey.GetGroupInfoKey(groupID), func(ctx context.Context) (any, error) {
|
||||
var cache cacheProto[sdkws.GroupInfo]
|
||||
return cache.Unmarshal(g.local.Get(ctx, cachekey.GetGroupInfoKey(groupID), func(ctx context.Context) ([]byte, error) {
|
||||
log.ZDebug(ctx, "GroupLocalCache GetGroupInfo rpc", "groupID", groupID)
|
||||
return g.client.GetGroupInfoCache(ctx, groupID)
|
||||
return cache.Marshal(g.client.GetGroupInfoCache(ctx, groupID))
|
||||
}))
|
||||
}
|
||||
|
||||
@ -101,7 +106,7 @@ func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res.List, nil
|
||||
return res.UserIDs, nil
|
||||
}
|
||||
|
||||
func (g *GroupLocalCache) GetGroupMemberIDMap(ctx context.Context, groupID string) (map[string]struct{}, error) {
|
||||
@ -109,7 +114,7 @@ func (g *GroupLocalCache) GetGroupMemberIDMap(ctx context.Context, groupID strin
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res.Map, nil
|
||||
return datautil.SliceSet(res.UserIDs), nil
|
||||
}
|
||||
|
||||
func (g *GroupLocalCache) GetGroupInfos(ctx context.Context, groupIDs []string) ([]*sdkws.GroupInfo, error) {
|
||||
|
||||
@ -47,7 +47,7 @@ type OnlineCache struct {
|
||||
local lru.LRU[string, []int32]
|
||||
}
|
||||
|
||||
func (o *OnlineCache) GetUserOnlinePlatform(ctx context.Context, userID string) ([]int32, error) {
|
||||
func (o *OnlineCache) getUserOnlinePlatform(ctx context.Context, userID string) ([]int32, error) {
|
||||
platformIDs, err := o.local.Get(userID, func() ([]int32, error) {
|
||||
return o.user.GetUserOnlinePlatform(ctx, userID)
|
||||
})
|
||||
@ -59,8 +59,18 @@ func (o *OnlineCache) GetUserOnlinePlatform(ctx context.Context, userID string)
|
||||
return platformIDs, nil
|
||||
}
|
||||
|
||||
func (o *OnlineCache) GetUserOnlinePlatform(ctx context.Context, userID string) ([]int32, error) {
|
||||
platformIDs, err := o.getUserOnlinePlatform(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tmp := make([]int32, len(platformIDs))
|
||||
copy(tmp, platformIDs)
|
||||
return platformIDs, nil
|
||||
}
|
||||
|
||||
func (o *OnlineCache) GetUserOnline(ctx context.Context, userID string) (bool, error) {
|
||||
platformIDs, err := o.GetUserOnlinePlatform(ctx, userID)
|
||||
platformIDs, err := o.getUserOnlinePlatform(ctx, userID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
@ -16,12 +16,12 @@ package rpccache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/localcache"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/rpcclient"
|
||||
"github.com/openimsdk/protocol/sdkws"
|
||||
"github.com/openimsdk/protocol/user"
|
||||
"github.com/openimsdk/tools/errs"
|
||||
"github.com/openimsdk/tools/log"
|
||||
"github.com/redis/go-redis/v9"
|
||||
@ -32,7 +32,7 @@ func NewUserLocalCache(client rpcclient.UserRpcClient, localCache *config.LocalC
|
||||
log.ZDebug(context.Background(), "UserLocalCache", "topic", lc.Topic, "slotNum", lc.SlotNum, "slotSize", lc.SlotSize, "enable", lc.Enable())
|
||||
x := &UserLocalCache{
|
||||
client: client,
|
||||
local: localcache.New[any](
|
||||
local: localcache.New[[]byte](
|
||||
localcache.WithLocalSlotNum(lc.SlotNum),
|
||||
localcache.WithLocalSlotSize(lc.SlotSize),
|
||||
localcache.WithLinkSlotNum(lc.SlotNum),
|
||||
@ -48,7 +48,7 @@ func NewUserLocalCache(client rpcclient.UserRpcClient, localCache *config.LocalC
|
||||
|
||||
type UserLocalCache struct {
|
||||
client rpcclient.UserRpcClient
|
||||
local localcache.Cache[any]
|
||||
local localcache.Cache[[]byte]
|
||||
}
|
||||
|
||||
func (u *UserLocalCache) GetUserInfo(ctx context.Context, userID string) (val *sdkws.UserInfo, err error) {
|
||||
@ -60,24 +60,34 @@ func (u *UserLocalCache) GetUserInfo(ctx context.Context, userID string) (val *s
|
||||
log.ZError(ctx, "UserLocalCache GetUserInfo return", err)
|
||||
}
|
||||
}()
|
||||
return localcache.AnyValue[*sdkws.UserInfo](u.local.Get(ctx, cachekey.GetUserInfoKey(userID), func(ctx context.Context) (any, error) {
|
||||
var cache cacheProto[sdkws.UserInfo]
|
||||
return cache.Unmarshal(u.local.Get(ctx, cachekey.GetUserInfoKey(userID), func(ctx context.Context) ([]byte, error) {
|
||||
log.ZDebug(ctx, "UserLocalCache GetUserInfo rpc", "userID", userID)
|
||||
return u.client.GetUserInfo(ctx, userID)
|
||||
return cache.Marshal(u.client.GetUserInfo(ctx, userID))
|
||||
}))
|
||||
}
|
||||
|
||||
func (u *UserLocalCache) GetUserGlobalMsgRecvOpt(ctx context.Context, userID string) (val int32, err error) {
|
||||
log.ZDebug(ctx, "UserLocalCache GetUserGlobalMsgRecvOpt req", "userID", userID)
|
||||
resp, err := u.getUserGlobalMsgRecvOpt(ctx, userID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return resp.GlobalRecvMsgOpt, nil
|
||||
}
|
||||
|
||||
func (u *UserLocalCache) getUserGlobalMsgRecvOpt(ctx context.Context, userID string) (val *user.GetGlobalRecvMessageOptResp, err error) {
|
||||
log.ZDebug(ctx, "UserLocalCache getUserGlobalMsgRecvOpt req", "userID", userID)
|
||||
defer func() {
|
||||
if err == nil {
|
||||
log.ZDebug(ctx, "UserLocalCache GetUserGlobalMsgRecvOpt return", "value", val)
|
||||
log.ZDebug(ctx, "UserLocalCache getUserGlobalMsgRecvOpt return", "value", val)
|
||||
} else {
|
||||
log.ZError(ctx, "UserLocalCache GetUserGlobalMsgRecvOpt return", err)
|
||||
log.ZError(ctx, "UserLocalCache getUserGlobalMsgRecvOpt return", err)
|
||||
}
|
||||
}()
|
||||
return localcache.AnyValue[int32](u.local.Get(ctx, cachekey.GetUserGlobalRecvMsgOptKey(userID), func(ctx context.Context) (any, error) {
|
||||
var cache cacheProto[user.GetGlobalRecvMessageOptResp]
|
||||
return cache.Unmarshal(u.local.Get(ctx, cachekey.GetUserGlobalRecvMsgOptKey(userID), func(ctx context.Context) ([]byte, error) {
|
||||
log.ZDebug(ctx, "UserLocalCache GetUserGlobalMsgRecvOpt rpc", "userID", userID)
|
||||
return u.client.GetUserGlobalMsgRecvOpt(ctx, userID)
|
||||
return cache.Marshal(u.client.Client.GetGlobalRecvMessageOpt(ctx, &user.GetGlobalRecvMessageOptReq{UserID: userID}))
|
||||
}))
|
||||
}
|
||||
|
||||
@ -110,18 +120,3 @@ func (u *UserLocalCache) GetUsersInfoMap(ctx context.Context, userIDs []string)
|
||||
}
|
||||
return users, nil
|
||||
}
|
||||
|
||||
//func (u *UserLocalCache) GetUserOnlinePlatform(ctx context.Context, userID string) (val []int32, err error) {
|
||||
// log.ZDebug(ctx, "UserLocalCache GetUserOnlinePlatform req", "userID", userID)
|
||||
// defer func() {
|
||||
// if err == nil {
|
||||
// log.ZDebug(ctx, "UserLocalCache GetUserOnlinePlatform return", "value", val)
|
||||
// } else {
|
||||
// log.ZError(ctx, "UserLocalCache GetUserOnlinePlatform return", err)
|
||||
// }
|
||||
// }()
|
||||
// return localcache.AnyValue[[]int32](u.local.Get(ctx, cachekey.GetOnlineKey(userID), func(ctx context.Context) (any, error) {
|
||||
// log.ZDebug(ctx, "UserLocalCache GetUserGlobalMsgRecvOpt rpc", "userID", userID)
|
||||
// return u.client.GetUserGlobalMsgRecvOpt(ctx, userID)
|
||||
// }))
|
||||
//}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user