You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/db/localcache/group.go

64 lines
1.7 KiB

2 years ago
package localcache
import (
"context"
2 years ago
"sync"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/group"
2 years ago
)
type GroupLocalCache struct {
2 years ago
lock sync.Mutex
cache map[string]GroupMemberIDsHash
2 years ago
client discoveryregistry.SvcDiscoveryRegistry
2 years ago
}
type GroupMemberIDsHash struct {
memberListHash uint64
userIDs []string
}
2 years ago
func NewGroupLocalCache(client discoveryregistry.SvcDiscoveryRegistry) *GroupLocalCache {
2 years ago
return &GroupLocalCache{
2 years ago
cache: make(map[string]GroupMemberIDsHash, 0),
client: client,
2 years ago
}
}
2 years ago
func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) {
g.lock.Lock()
defer g.lock.Unlock()
2 years ago
conn, err := g.client.GetConn(ctx, config.Config.RpcRegisterName.OpenImGroupName)
2 years ago
if err != nil {
return nil, err
}
client := group.NewGroupClient(conn)
resp, err := client.GetGroupAbstractInfo(ctx, &group.GetGroupAbstractInfoReq{
2 years ago
GroupIDs: []string{groupID},
2 years ago
})
if err != nil {
2 years ago
return nil, err
2 years ago
}
2 years ago
if len(resp.GroupAbstractInfos) < 1 {
2 years ago
return nil, errs.ErrGroupIDNotFound
2 years ago
}
localHashInfo, ok := g.cache[groupID]
if ok && localHashInfo.memberListHash == resp.GroupAbstractInfos[0].GroupMemberListHash {
return localHashInfo.userIDs, nil
}
2 years ago
groupMembersResp, err := client.GetGroupMemberUserIDs(ctx, &group.GetGroupMemberUserIDsReq{
2 years ago
GroupID: groupID,
})
if err != nil {
return nil, err
}
g.cache[groupID] = GroupMemberIDsHash{
memberListHash: resp.GroupAbstractInfos[0].GroupMemberListHash,
2 years ago
userIDs: groupMembersResp.UserIDs,
2 years ago
}
return g.cache[groupID].userIDs, nil
2 years ago
}