Files
open-im-server/pkg/common/db/localcache/group.go
T

60 lines
1.6 KiB
Go
Raw Normal View History

2023-02-01 17:11:44 +08:00
package localcache
import (
"context"
2023-05-08 12:39:45 +08:00
"sync"
2023-03-16 10:46:06 +08:00
"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"
2023-06-21 10:37:18 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
2023-02-01 17:11:44 +08:00
)
type GroupLocalCache struct {
2023-06-21 10:37:18 +08:00
lock sync.Mutex
cache map[string]GroupMemberIDsHash
client *rpcclient.Group
2023-02-01 17:11:44 +08:00
}
type GroupMemberIDsHash struct {
memberListHash uint64
userIDs []string
}
2023-06-21 10:37:18 +08:00
func NewGroupLocalCache(discov discoveryregistry.SvcDiscoveryRegistry) *GroupLocalCache {
client := rpcclient.NewGroup(discov)
2023-02-13 18:14:26 +08:00
return &GroupLocalCache{
2023-06-21 10:37:18 +08:00
cache: make(map[string]GroupMemberIDsHash, 0),
client: client,
2023-02-01 17:11:44 +08:00
}
}
2023-02-07 18:43:43 +08:00
func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) {
2023-06-21 10:37:18 +08:00
resp, err := g.client.Client.GetGroupAbstractInfo(ctx, &group.GetGroupAbstractInfoReq{
2023-02-07 18:43:43 +08:00
GroupIDs: []string{groupID},
2023-02-01 17:11:44 +08:00
})
if err != nil {
2023-02-07 18:43:43 +08:00
return nil, err
2023-02-01 17:11:44 +08:00
}
2023-05-11 21:32:12 +08:00
if len(resp.GroupAbstractInfos) < 1 {
2023-03-07 12:19:30 +08:00
return nil, errs.ErrGroupIDNotFound
2023-02-07 18:43:43 +08:00
}
2023-06-21 10:37:18 +08:00
g.lock.Lock()
defer g.lock.Unlock()
2023-02-07 18:43:43 +08:00
localHashInfo, ok := g.cache[groupID]
if ok && localHashInfo.memberListHash == resp.GroupAbstractInfos[0].GroupMemberListHash {
return localHashInfo.userIDs, nil
}
2023-06-21 10:37:18 +08:00
groupMembersResp, err := g.client.Client.GetGroupMemberUserIDs(ctx, &group.GetGroupMemberUserIDsReq{
2023-02-07 18:43:43 +08:00
GroupID: groupID,
})
if err != nil {
return nil, err
}
g.cache[groupID] = GroupMemberIDsHash{
memberListHash: resp.GroupAbstractInfos[0].GroupMemberListHash,
2023-02-09 16:33:40 +08:00
userIDs: groupMembersResp.UserIDs,
2023-02-07 18:43:43 +08:00
}
return g.cache[groupID].userIDs, nil
2023-02-01 17:11:44 +08:00
}