This commit is contained in:
wangchuxiao 2023-06-21 10:37:18 +08:00
parent 2005a91891
commit fa9ff6709d
2 changed files with 19 additions and 34 deletions

View File

@ -4,16 +4,16 @@ import (
"context" "context"
"sync" "sync"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
) )
type ConversationLocalCache struct { type ConversationLocalCache struct {
lock sync.Mutex lock sync.Mutex
superGroupRecvMsgNotNotifyUserIDs map[string]Hash superGroupRecvMsgNotNotifyUserIDs map[string]Hash
conversationIDs map[string]Hash conversationIDs map[string]Hash
client discoveryregistry.SvcDiscoveryRegistry client *rpcclient.Conversation
} }
type Hash struct { type Hash struct {
@ -21,21 +21,16 @@ type Hash struct {
ids []string ids []string
} }
func NewConversationLocalCache(client discoveryregistry.SvcDiscoveryRegistry) *ConversationLocalCache { func NewConversationLocalCache(discov discoveryregistry.SvcDiscoveryRegistry) *ConversationLocalCache {
return &ConversationLocalCache{ return &ConversationLocalCache{
superGroupRecvMsgNotNotifyUserIDs: make(map[string]Hash), superGroupRecvMsgNotNotifyUserIDs: make(map[string]Hash),
conversationIDs: make(map[string]Hash), conversationIDs: make(map[string]Hash),
client: client, client: rpcclient.NewConversation(discov),
} }
} }
func (g *ConversationLocalCache) GetRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) ([]string, error) { func (g *ConversationLocalCache) GetRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) ([]string, error) {
conn, err := g.client.GetConn(ctx, config.Config.RpcRegisterName.OpenImConversationName) resp, err := g.client.Client.GetRecvMsgNotNotifyUserIDs(ctx, &conversation.GetRecvMsgNotNotifyUserIDsReq{
if err != nil {
return nil, err
}
client := conversation.NewConversationClient(conn)
resp, err := client.GetRecvMsgNotNotifyUserIDs(ctx, &conversation.GetRecvMsgNotNotifyUserIDsReq{
GroupID: groupID, GroupID: groupID,
}) })
if err != nil { if err != nil {
@ -45,12 +40,7 @@ func (g *ConversationLocalCache) GetRecvMsgNotNotifyUserIDs(ctx context.Context,
} }
func (g *ConversationLocalCache) GetConversationIDs(ctx context.Context, userID string) ([]string, error) { func (g *ConversationLocalCache) GetConversationIDs(ctx context.Context, userID string) ([]string, error) {
conn, err := g.client.GetConn(ctx, config.Config.RpcRegisterName.OpenImConversationName) resp, err := g.client.Client.GetUserConversationIDsHash(ctx, &conversation.GetUserConversationIDsHashReq{
if err != nil {
return nil, err
}
client := conversation.NewConversationClient(conn)
resp, err := client.GetUserConversationIDsHash(ctx, &conversation.GetUserConversationIDsHashReq{
OwnerUserID: userID, OwnerUserID: userID,
}) })
if err != nil { if err != nil {
@ -60,7 +50,7 @@ func (g *ConversationLocalCache) GetConversationIDs(ctx context.Context, userID
defer g.lock.Unlock() defer g.lock.Unlock()
hash, ok := g.conversationIDs[userID] hash, ok := g.conversationIDs[userID]
if !ok || hash.hash != resp.Hash { if !ok || hash.hash != resp.Hash {
conversationIDsResp, err := client.GetConversationIDs(ctx, &conversation.GetConversationIDsReq{ conversationIDsResp, err := g.client.Client.GetConversationIDs(ctx, &conversation.GetConversationIDsReq{
UserID: userID, UserID: userID,
}) })
if err != nil { if err != nil {

View File

@ -4,17 +4,16 @@ import (
"context" "context"
"sync" "sync"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs" "github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/group" "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/group"
"google.golang.org/grpc" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
) )
type GroupLocalCache struct { type GroupLocalCache struct {
lock sync.Mutex lock sync.Mutex
cache map[string]GroupMemberIDsHash cache map[string]GroupMemberIDsHash
conn *grpc.ClientConn client *rpcclient.Group
} }
type GroupMemberIDsHash struct { type GroupMemberIDsHash struct {
@ -22,22 +21,16 @@ type GroupMemberIDsHash struct {
userIDs []string userIDs []string
} }
func NewGroupLocalCache(client discoveryregistry.SvcDiscoveryRegistry) *GroupLocalCache { func NewGroupLocalCache(discov discoveryregistry.SvcDiscoveryRegistry) *GroupLocalCache {
conn, err := client.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImGroupName) client := rpcclient.NewGroup(discov)
if err != nil {
panic(err)
}
return &GroupLocalCache{ return &GroupLocalCache{
cache: make(map[string]GroupMemberIDsHash, 0), cache: make(map[string]GroupMemberIDsHash, 0),
conn: conn, client: client,
} }
} }
func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) { func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) {
g.lock.Lock() resp, err := g.client.Client.GetGroupAbstractInfo(ctx, &group.GetGroupAbstractInfoReq{
defer g.lock.Unlock()
client := group.NewGroupClient(g.conn)
resp, err := client.GetGroupAbstractInfo(ctx, &group.GetGroupAbstractInfoReq{
GroupIDs: []string{groupID}, GroupIDs: []string{groupID},
}) })
if err != nil { if err != nil {
@ -46,11 +39,13 @@ func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string)
if len(resp.GroupAbstractInfos) < 1 { if len(resp.GroupAbstractInfos) < 1 {
return nil, errs.ErrGroupIDNotFound return nil, errs.ErrGroupIDNotFound
} }
g.lock.Lock()
defer g.lock.Unlock()
localHashInfo, ok := g.cache[groupID] localHashInfo, ok := g.cache[groupID]
if ok && localHashInfo.memberListHash == resp.GroupAbstractInfos[0].GroupMemberListHash { if ok && localHashInfo.memberListHash == resp.GroupAbstractInfos[0].GroupMemberListHash {
return localHashInfo.userIDs, nil return localHashInfo.userIDs, nil
} }
groupMembersResp, err := client.GetGroupMemberUserIDs(ctx, &group.GetGroupMemberUserIDsReq{ groupMembersResp, err := g.client.Client.GetGroupMemberUserIDs(ctx, &group.GetGroupMemberUserIDsReq{
GroupID: groupID, GroupID: groupID,
}) })
if err != nil { if err != nil {