From 18320080219fd794d46e86f4141ef99f8fd07b66 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Fri, 1 Sep 2023 17:35:22 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E6=9B=BF=E6=8D=A2group=5Fin=5Fuse?= =?UTF-8?q?r=5Fduration=5F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _const/redis_key/redisPrefix.go | 2 + domain/cache/room_c/userRoomVisit.go | 56 ++++++++++++---------------- domain/model/group_m/room.go | 15 ++------ 3 files changed, 29 insertions(+), 44 deletions(-) diff --git a/_const/redis_key/redisPrefix.go b/_const/redis_key/redisPrefix.go index 8bb25f5..d0f8b1c 100644 --- a/_const/redis_key/redisPrefix.go +++ b/_const/redis_key/redisPrefix.go @@ -760,6 +760,8 @@ func GetPrefixGroupMicNumType(groupUuid string) string { return strings.Replace(groupMicNumType, "{groupUuid}", groupUuid, -1) } +// Deprecated: 用户进房时间 +// 用 uer:g:${group_id}代替 func GetPrefixGroupInUserDuration(groupUuid string) string { return strings.Replace(groupInUserDuration, "{groupUuid}", groupUuid, -1) } diff --git a/domain/cache/room_c/userRoomVisit.go b/domain/cache/room_c/userRoomVisit.go index f9a5c43..f257d46 100644 --- a/domain/cache/room_c/userRoomVisit.go +++ b/domain/cache/room_c/userRoomVisit.go @@ -10,36 +10,36 @@ import ( redis2 "github.com/go-redis/redis/v8" "github.com/spf13/cast" "hilo-group/_const/redis_key" - "strconv" + "hilo-group/_const/redis_key/group_k" "time" ) // 处理访问房间的相关缓存 func ProcessRoomVisit(groupId string, userId uint64) error { - key := redis_key.GetPrefixGroupInUserDuration(groupId) - now := time.Now() - ret, err := redisCli.GetRedis().ZAdd(context.Background(), key, &redis2.Z{ - Score: float64(now.Unix()), - Member: userId, - }).Result() - if err != nil { - return err - } - mylogrus.MyLog.Infof("ProcessRoomVisit, ZADD %s, return %d", key, ret) - - // 每群定时清一次数据可以了 - if now.Second()%redis_key.GroupInDurationClearPeriod == 0 { - rc, err := clearRoomVisit(groupId, now.AddDate(0, 0, -redis_key.GroupInDurationTTL)) - if err == nil { - mylogrus.MyLog.Infof("ProcessRoomVisit, clearRoomVisit %s, return %d", key, rc) - } else { - mylogrus.MyLog.Warnf("ProcessRoomVisit, clearRoomVisit %s, failed %s", key, err.Error()) - } - } + //key := redis_key.GetPrefixGroupInUserDuration(groupId) + //now := time.Now() + //ret, err := redisCli.GetRedis().ZAdd(context.Background(), key, &redis2.Z{ + // Score: float64(now.Unix()), + // Member: userId, + //}).Result() + //if err != nil { + // return err + //} + //mylogrus.MyLog.Infof("ProcessRoomVisit, ZADD %s, return %d", key, ret) + // + //// 每群定时清一次数据可以了 + //if now.Second()%redis_key.GroupInDurationClearPeriod == 0 { + // rc, err := clearRoomVisit(groupId, now.AddDate(0, 0, -redis_key.GroupInDurationTTL)) + // if err == nil { + // mylogrus.MyLog.Infof("ProcessRoomVisit, clearRoomVisit %s, return %d", key, rc) + // } else { + // mylogrus.MyLog.Warnf("ProcessRoomVisit, clearRoomVisit %s, failed %s", key, err.Error()) + // } + //} // 马上更新roomVisitCount if _, err := GetSetRoomVisitCount(groupId); err != nil { - mylogrus.MyLog.Warnf("ProcessRoomVisit, failed for key %s, err: %s", key, err.Error()) + mylogrus.MyLog.Errorf("ProcessRoomVisit, failed for groupId %s, err: %s", groupId, err.Error()) } return nil @@ -108,7 +108,8 @@ func MGetRoomVisitCount(groupIds []string) (map[string]string, error) { } func GetSetRoomVisitCount(groupId string) (int64, error) { - key := redis_key.GetPrefixGroupInUserDuration(groupId) + //key := redis_key.GetPrefixGroupInUserDuration(groupId) + key := group_k.GetUserEnterRoomGroupKey(groupId) vc, err := redisCli.GetRedis().ZCard(context.Background(), key).Result() // 查到合法值后,更新二级缓存 if err == nil && vc >= 0 { @@ -133,12 +134,3 @@ func saveRoomVisitCount(groupId string, count int64) error { return nil //return redisCli.GetRedis().HSet(context.Background(), key, groupId, strconv.FormatInt(count, 10)).Result() } - -func clearRoomVisit(groupId string, t time.Time) (int64, error) { - value := strconv.FormatInt(t.Unix(), 10) - ret, err := redisCli.GetRedis().ZRemRangeByScore(context.Background(), redis_key.GetPrefixGroupInUserDuration(groupId), "0", value).Result() - if err != nil { - return 0, err - } - return ret, nil -} diff --git a/domain/model/group_m/room.go b/domain/model/group_m/room.go index 67604e3..2149aa3 100644 --- a/domain/model/group_m/room.go +++ b/domain/model/group_m/room.go @@ -14,6 +14,7 @@ import ( "gorm.io/gorm/clause" "hilo-group/_const/enum/group_e" "hilo-group/_const/redis_key" + "hilo-group/_const/redis_key/group_k" "hilo-group/domain/cache/room_c" "hilo-group/domain/model/user_m" "hilo-group/myerr" @@ -389,18 +390,8 @@ func GetAllGroupsSorted(model *domain.Model) ([]string, error) { // 取房间最近N天的访问人数 func GetRoomVisitCount(groupId string) (int64, error) { - // 每群定时请一次数据可以了 - now := time.Now() - if now.Second()%redis_key.GroupInDurationClearPeriod == 0 { - // 先移除(N天之前的),后统计 - if _, err := redisCli.GetRedis().ZRemRangeByScore(context.Background(), redis_key.GetPrefixGroupInUserDuration(groupId), - "0", strconv.FormatInt(time.Now().AddDate(0, 0, -redis_key.GroupInDurationTTL).Unix(), 10)). - Result(); err != nil { - return 0, err - } - } - - visitCount, err := redisCli.GetRedis().ZCard(context.Background(), redis_key.GetPrefixGroupInUserDuration(groupId)).Result() + key := group_k.GetUserEnterRoomGroupKey(groupId) + visitCount, err := redisCli.GetRedis().ZCard(context.Background(), key).Result() if err != nil { return 0, err } -- 2.22.0