mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-05-02 07:54:19 +08:00
64 lines
2.1 KiB
Go
64 lines
2.1 KiB
Go
package rpccache
|
|
|
|
import (
|
|
"context"
|
|
"github.com/OpenIMSDK/tools/log"
|
|
"github.com/openimsdk/localcache"
|
|
"github.com/openimsdk/open-im-server/v3/pkg/common/cachekey"
|
|
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
|
"github.com/openimsdk/open-im-server/v3/pkg/rpcclient"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
func NewGroupLocalCache(client rpcclient.GroupRpcClient, cli redis.UniversalClient) *GroupLocalCache {
|
|
lc := config.Config.LocalCache.Group
|
|
log.ZDebug(context.Background(), "GroupLocalCache", "topic", lc.Topic, "slotNum", lc.SlotNum, "slotSize", lc.SlotSize, "enable", lc.Enable())
|
|
x := &GroupLocalCache{
|
|
client: client,
|
|
local: localcache.New[any](
|
|
localcache.WithLocalSlotNum(lc.SlotNum),
|
|
localcache.WithLocalSlotSize(lc.SlotSize),
|
|
),
|
|
}
|
|
if lc.Enable() {
|
|
go subscriberRedisDeleteCache(context.Background(), cli, lc.Topic, x.local.DelLocal)
|
|
}
|
|
return x
|
|
}
|
|
|
|
type GroupLocalCache struct {
|
|
client rpcclient.GroupRpcClient
|
|
local localcache.Cache[any]
|
|
}
|
|
|
|
func (g *GroupLocalCache) getGroupMemberIDs(ctx context.Context, groupID string) (val *listMap[string], err error) {
|
|
log.ZDebug(ctx, "GroupLocalCache getGroupMemberIDs req", "groupID", groupID)
|
|
defer func() {
|
|
if err == nil {
|
|
log.ZDebug(ctx, "GroupLocalCache getGroupMemberIDs return", "value", val.List)
|
|
} else {
|
|
log.ZError(ctx, "GroupLocalCache getGroupMemberIDs return", err)
|
|
}
|
|
}()
|
|
return localcache.AnyValue[*listMap[string]](g.local.Get(ctx, cachekey.GetGroupMemberIDsKey(groupID), func(ctx context.Context) (any, error) {
|
|
log.ZDebug(ctx, "GroupLocalCache getGroupMemberIDs rpc", "groupID", groupID)
|
|
return newListMap(g.client.GetGroupMemberIDs(ctx, groupID))
|
|
}))
|
|
}
|
|
|
|
func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) {
|
|
res, err := g.getGroupMemberIDs(ctx, groupID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return res.List, nil
|
|
}
|
|
|
|
func (g *GroupLocalCache) GetGroupMemberIDMap(ctx context.Context, groupID string) (map[string]struct{}, error) {
|
|
res, err := g.getGroupMemberIDs(ctx, groupID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return res.Map, nil
|
|
}
|