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

59 lines
1.4 KiB

2 years ago
package localcache
import (
2 years ago
"Open_IM/pkg/common/constant"
2 years ago
"Open_IM/pkg/proto/group"
"context"
"google.golang.org/grpc"
2 years ago
"sync"
2 years ago
)
type GroupLocalCache struct {
2 years ago
lock sync.Mutex
2 years ago
cache map[string]GroupMemberIDsHash
rpc *grpc.ClientConn
group group.GroupClient
}
type GroupMemberIDsHash struct {
memberListHash uint64
userIDs []string
}
func NewGroupMemberIDsLocalCache(rpc *grpc.ClientConn) GroupLocalCache {
return GroupLocalCache{
cache: make(map[string]GroupMemberIDsHash, 0),
rpc: rpc,
group: group.NewGroupClient(rpc),
}
}
2 years ago
func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) {
g.lock.Lock()
defer g.lock.Unlock()
2 years ago
resp, err := g.group.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) < 0 {
return nil, constant.ErrGroupIDNotFound
}
localHashInfo, ok := g.cache[groupID]
if ok && localHashInfo.memberListHash == resp.GroupAbstractInfos[0].GroupMemberListHash {
return localHashInfo.userIDs, nil
}
groupMembersResp, err := g.group.GetGroupMemberList(ctx, &group.GetGroupMemberListReq{
GroupID: groupID,
})
if err != nil {
return nil, err
}
g.cache[groupID] = GroupMemberIDsHash{
memberListHash: resp.GroupAbstractInfos[0].GroupMemberListHash,
userIDs: groupMembersResp.Members,
}
return g.cache[groupID].userIDs, nil
2 years ago
}