Commit b6f697a4 authored by iamhujiebin's avatar iamhujiebin

zset同步存一份到redis集群

parent 97801bdd
...@@ -249,6 +249,9 @@ const groupInUserDuration = "group_in_user_duration_{groupUuid}" ...@@ -249,6 +249,9 @@ const groupInUserDuration = "group_in_user_duration_{groupUuid}"
// 15天内进入房间的人数 // 15天内进入房间的人数
const roomVisitCount = "room_visit_count" const roomVisitCount = "room_visit_count"
// 15天内进入房间的人数 member groupId score 麦位有人的数量
const roomVisitCountZSet = "room_visit_count_zset"
// 用户进入过的房间及时间 // 用户进入过的房间及时间
const userEnterRoom = "enter_room_{userId}" const userEnterRoom = "enter_room_{userId}"
...@@ -769,6 +772,10 @@ func GetPrefixRoomVisitCount() string { ...@@ -769,6 +772,10 @@ func GetPrefixRoomVisitCount() string {
return roomVisitCount return roomVisitCount
} }
func GetPrefixRoomVisitCountZset() string {
return roomVisitCountZSet
}
func GetPrefixSupportLevel(date string) string { func GetPrefixSupportLevel(date string) string {
return strings.Replace(supportLevel, "{date}", date, -1) return strings.Replace(supportLevel, "{date}", date, -1)
} }
......
...@@ -100,7 +100,7 @@ func MGetRoomVisitCount(groupIds []string) (map[string]string, error) { ...@@ -100,7 +100,7 @@ func MGetRoomVisitCount(groupIds []string) (map[string]string, error) {
} }
key := redis_key.GetPrefixRoomVisitCount() key := redis_key.GetPrefixRoomVisitCount()
res, err := redisCli.GetRedis().HMGet(context.Background(), key, miss...).Result() res, err := redisCli.GetRedis().HMGet(context.Background(), key, miss...).Result() // todo 后续用zset key的zrange拿出前面排名的
if err != nil { if err != nil {
return visit, err return visit, err
} }
...@@ -121,14 +121,23 @@ func GetSetRoomVisitCount(groupId string) (int64, error) { ...@@ -121,14 +121,23 @@ func GetSetRoomVisitCount(groupId string) (int64, error) {
vc, err := redisCli.GetRedis().ZCard(context.Background(), key).Result() vc, err := redisCli.GetRedis().ZCard(context.Background(), key).Result()
// 查到合法值后,更新二级缓存 // 查到合法值后,更新二级缓存
if err == nil && vc >= 0 { if err == nil && vc >= 0 {
ret, err := saveRoomVisitCount(groupId, vc) if ret, err := saveRoomVisitCount(groupId, vc); err != nil {
mylogrus.MyLog.Infof("saveRoomVisitCount %s, ret = %d, err: %v", groupId, ret, err) mylogrus.MyLog.Errorf("saveRoomVisitCount %s, ret = %d, err: %v", groupId, ret, err)
}
} }
return vc, err return vc, err
} }
func saveRoomVisitCount(groupId string, count int64) (int64, error) { func saveRoomVisitCount(groupId string, count int64) (int64, error) {
key := redis_key.GetPrefixRoomVisitCount() key := redis_key.GetPrefixRoomVisitCount()
zKey := redis_key.GetPrefixRoomVisitCountZset()
// zset同步存一份到redis集群
if err := redisCli.GetClusterRedis().ZAdd(context.Background(), zKey, &redis2.Z{
Score: float64(count),
Member: groupId,
}).Err(); err != nil {
mylogrus.MyLog.Errorf("GetPrefixRoomVisitCountZset fail %s", err.Error())
}
return redisCli.GetRedis().HSet(context.Background(), key, groupId, strconv.FormatInt(count, 10)).Result() return redisCli.GetRedis().HSet(context.Background(), key, groupId, strconv.FormatInt(count, 10)).Result()
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment