From 04ad6fb07e9de408aaa9f3272c88885f6a7b6e30 Mon Sep 17 00:00:00 2001 From: iamhujiebin <458249864@qq.com> Date: Mon, 28 Aug 2023 23:25:25 +0800 Subject: [PATCH] BatchGetAllMicUser --- _const/redis_key/mic_k/keys.go | 9 +++++++++ domain/model/group_m/factory.go | 15 +++++++++++++++ domain/model/group_m/mic.go | 9 +++++++++ 3 files changed, 33 insertions(+) diff --git a/_const/redis_key/mic_k/keys.go b/_const/redis_key/mic_k/keys.go index 14ee6a5..bf6da96 100644 --- a/_const/redis_key/mic_k/keys.go +++ b/_const/redis_key/mic_k/keys.go @@ -13,6 +13,8 @@ const ( MicGroupPowerOnMic = MicPrefix + "groupPower:${userId}:${date}" // 家族内群上麦分钟数 MicUserOnMic = MicPrefix + "user:onMic:${tz}:${userId}:${date}" + + MicGroupOnMic = MicPrefix + "group:onMic:${groupId}" // 在群组麦位上的人 hset field:micIndex member:userId ) func GetUserMicDayInvite(userId mysql.ID) string { @@ -31,3 +33,10 @@ func GetMicGroupPowerOnMic(userId uint64) string { func GetUserOnMicKey(userId uint64, tz, date string) string { return redis_key.ReplaceKey(MicUserOnMic, tz, fmt.Sprintf("%d", userId), date) } + +// 获取群组麦位上的人 +// 可能会有数据不一致,已原来麦位的key为准 +// 这里用户快速获取群上麦位的人用,代替大量mget命令慢查询 +func GetGroupOnMicUser(groupId string) string { + return redis_key.ReplaceKey(MicGroupOnMic, groupId) +} diff --git a/domain/model/group_m/factory.go b/domain/model/group_m/factory.go index a1c5d39..a3e8479 100644 --- a/domain/model/group_m/factory.go +++ b/domain/model/group_m/factory.go @@ -13,6 +13,7 @@ import ( "gorm.io/gorm" "hilo-group/_const/enum/group_e" "hilo-group/_const/redis_key" + "hilo-group/_const/redis_key/mic_k" "hilo-group/myerr" "strconv" "strings" @@ -242,6 +243,20 @@ func BatchGetAllMicUser(model *domain.Model, groupIds []string) (map[string][]my if len(groupIds) <= 0 { return result, nil } + // 获取群组中上麦用户 + for _, groupId := range groupIds { + groupMicUserKey := mic_k.GetGroupOnMicUser(groupId) + userIds, err := model.Redis.HVals(model, groupMicUserKey).Result() + if err != nil { + model.Log.Errorf("BatchGetAllMicUser fail:%v", err) + } + for _, userIdStr := range userIds { + if userId, _ := strconv.ParseUint(userIdStr, 10, 64); userId > 0 { + result[groupId] = append(result[groupId], userId) + } + } + } + return result, nil keys := make([]string, 0) for _, g := range groupIds { diff --git a/domain/model/group_m/mic.go b/domain/model/group_m/mic.go index 3dce551..2abd233 100644 --- a/domain/model/group_m/mic.go +++ b/domain/model/group_m/mic.go @@ -14,6 +14,7 @@ import ( "gorm.io/gorm" "hilo-group/_const/enum/group_e" "hilo-group/_const/redis_key" + "hilo-group/_const/redis_key/mic_k" "hilo-group/common" "hilo-group/domain/model/noble_m" "hilo-group/domain/model/res_m" @@ -255,6 +256,10 @@ func (mic *Mic) In(userId uint64, externalId string) error { return bizerr.GroupMicUserHasIn } + // 增加群组中上麦用户 + groupMicUserKey := mic_k.GetGroupOnMicUser(mic.GroupUuid) + mic.model.Redis.HSet(mic.model, groupMicUserKey, mic.I, userId) + //离开动作已结束,增加到队列中 MicChangeRPush(mic.model, mic.GroupUuid, mic.I) @@ -385,6 +390,10 @@ func (micUser *MicUser) leave(operateUserId uint64, operateExternalId string) er //return bizerr.GroupMicErr } + // 减少群组中上麦用户 + groupMicUserKey := mic_k.GetGroupOnMicUser(micUser.GroupUuid) + micUser.model.Redis.HSet(micUser.model, groupMicUserKey, micUser.I, micUser.UserId) + //离开动作已结束,增加到队列中 MicChangeRPush(micUser.model, micUser.GroupUuid, micUser.I) -- 2.22.0