diff --git a/_const/redis_key/redisPrefix.go b/_const/redis_key/redisPrefix.go index 4a884447f36b43a42f47e31291b2c906525fa040..8bb25f51b6eb81b5adf4d16fbec25ab452cd6c71 100644 --- a/_const/redis_key/redisPrefix.go +++ b/_const/redis_key/redisPrefix.go @@ -768,6 +768,7 @@ func GetUserEnterRoomKey(userId uint64) string { return strings.Replace(userEnterRoom, "{userId}", strconv.FormatUint(userId, 10), -1) } +// Deprecated: 用redis集群中的zset代替 func GetPrefixRoomVisitCount() string { return roomVisitCount } diff --git a/domain/cache/room_c/userRoomVisit.go b/domain/cache/room_c/userRoomVisit.go index 63df8108b5d11884911fd0d88a0a102fa501d0f4..f9a5c437b97e4600b126b5abe621327b3753bdc0 100644 --- a/domain/cache/room_c/userRoomVisit.go +++ b/domain/cache/room_c/userRoomVisit.go @@ -2,7 +2,6 @@ package room_c import ( "context" - "errors" "fmt" "git.hilo.cn/hilo-common/mylogrus" "git.hilo.cn/hilo-common/resource/config" @@ -106,34 +105,6 @@ func MGetRoomVisitCount(groupIds []string) (map[string]string, error) { visit[cast.ToString(v.Member)] = cast.ToString(v.Score) } return visit, nil - - var miss []string - for _, groupId := range groupIds { - if data, err := roomVisitCntCache.Get(groupId); err == nil { - visit[groupId] = data.(string) - } else { - miss = append(miss, groupId) - } - } - if len(miss) <= 0 { - return visit, nil - } - - key := redis_key.GetPrefixRoomVisitCount() - res, err := redisCli.GetRedis().HMGet(context.Background(), key, miss...).Result() // todo 后续用zset key的zrange拿出前面排名的 - if err != nil { - return visit, err - } - if len(res) != len(miss) { - return visit, errors.New(fmt.Sprintf("MGetRoomVisitCount fail,miss match len,%v-%v", len(res), len(miss))) - } - for i, groupId := range miss { - if cnt, ok := res[i].(string); ok { - visit[groupId] = cnt - _ = roomVisitCntCache.SetWithExpire(groupId, cnt, time.Minute*15) - } - } - return visit, nil } func GetSetRoomVisitCount(groupId string) (int64, error) { @@ -141,15 +112,15 @@ func GetSetRoomVisitCount(groupId string) (int64, error) { vc, err := redisCli.GetRedis().ZCard(context.Background(), key).Result() // 查到合法值后,更新二级缓存 if err == nil && vc >= 0 { - if ret, err := saveRoomVisitCount(groupId, vc); err != nil { - mylogrus.MyLog.Errorf("saveRoomVisitCount %s, ret = %d, err: %v", groupId, ret, err) + if err := saveRoomVisitCount(groupId, vc); err != nil { + mylogrus.MyLog.Errorf("saveRoomVisitCount %s, err: %v", groupId, err) } } return vc, err } -func saveRoomVisitCount(groupId string, count int64) (int64, error) { - key := redis_key.GetPrefixRoomVisitCount() +func saveRoomVisitCount(groupId string, count int64) error { + //key := redis_key.GetPrefixRoomVisitCount() zKey := redis_key.GetPrefixRoomVisitCountZset() // zset同步存一份到redis集群 if err := redisCli.GetClusterRedis().ZAdd(context.Background(), zKey, &redis2.Z{ @@ -157,8 +128,10 @@ func saveRoomVisitCount(groupId string, count int64) (int64, error) { Member: groupId, }).Err(); err != nil { mylogrus.MyLog.Errorf("GetPrefixRoomVisitCountZset fail %s", err.Error()) + return err } - return redisCli.GetRedis().HSet(context.Background(), key, groupId, strconv.FormatInt(count, 10)).Result() + return nil + //return redisCli.GetRedis().HSet(context.Background(), key, groupId, strconv.FormatInt(count, 10)).Result() } func clearRoomVisit(groupId string, t time.Time) (int64, error) { @@ -169,9 +142,3 @@ func clearRoomVisit(groupId string, t time.Time) (int64, error) { } return ret, nil } - -// Deprecated: hgetall有问题 -func GetAllRoomVisitCount() (map[string]string, error) { - key := redis_key.GetPrefixRoomVisitCount() - return redisCli.GetRedis().HGetAll(context.Background(), key).Result() -}