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

67 lines
1.7 KiB
Go
Raw Normal View History

2023-02-01 17:11:44 +08:00
package localcache
import (
2023-02-23 19:15:30 +08:00
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
"OpenIM/pkg/discoveryregistry"
"OpenIM/pkg/proto/group"
2023-02-01 17:11:44 +08:00
"context"
2023-02-01 19:31:39 +08:00
"sync"
2023-02-01 17:11:44 +08:00
)
2023-02-08 17:56:04 +08:00
type GroupLocalCacheInterface interface {
GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error)
}
2023-02-01 17:11:44 +08:00
type GroupLocalCache struct {
2023-02-08 18:29:11 +08:00
lock sync.Mutex
cache map[string]GroupMemberIDsHash
2023-02-22 19:51:14 +08:00
client discoveryregistry.SvcDiscoveryRegistry
2023-02-01 17:11:44 +08:00
}
type GroupMemberIDsHash struct {
memberListHash uint64
userIDs []string
}
2023-02-22 19:51:14 +08:00
func NewGroupMemberIDsLocalCache(client discoveryregistry.SvcDiscoveryRegistry) *GroupLocalCache {
2023-02-13 18:14:26 +08:00
return &GroupLocalCache{
2023-02-08 18:29:11 +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) {
g.lock.Lock()
defer g.lock.Unlock()
2023-02-22 19:51:14 +08:00
conn, err := g.client.GetConn(config.Config.RpcRegisterName.OpenImGroupName)
2023-02-08 17:56:04 +08:00
if err != nil {
return nil, err
}
client := group.NewGroupClient(conn)
resp, err := 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-02-07 18:43:43 +08:00
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
}
2023-02-09 16:33:40 +08:00
groupMembersResp, err := client.GetGroupMemberUserID(ctx, &group.GetGroupMemberUserIDReq{
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
}