...
 
Commits (1)
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"git.hilo.cn/hilo-common/mylogrus" "git.hilo.cn/hilo-common/mylogrus"
"git.hilo.cn/hilo-common/resource/redisCli" "git.hilo.cn/hilo-common/resource/redisCli"
"github.com/bluele/gcache"
redis2 "github.com/go-redis/redis/v8" redis2 "github.com/go-redis/redis/v8"
"hilo-group/_const/redis_key" "hilo-group/_const/redis_key"
"strconv" "strconv"
...@@ -76,23 +77,40 @@ func GetUserRoomVisit(userId uint64) (map[string]int64, error) { ...@@ -76,23 +77,40 @@ func GetUserRoomVisit(userId uint64) (map[string]int64, error) {
return result, nil return result, nil
} }
var roomVisitCntCache = gcache.New(50000).LRU().Build()
// 批量获取房间访问人数 // 批量获取房间访问人数
// 带上lru
func MGetRoomVisitCount(groupIds []string) (map[string]string, error) { func MGetRoomVisitCount(groupIds []string) (map[string]string, error) {
key := redis_key.GetPrefixRoomVisitCount()
visit := make(map[string]string) visit := make(map[string]string)
if len(groupIds) <= 0 { if len(groupIds) <= 0 {
return visit, nil return visit, nil
} }
res, err := redisCli.GetRedis().HMGet(context.Background(), key, groupIds...).Result()
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()
if err != nil { if err != nil {
return visit, err return visit, err
} }
if len(res) != len(groupIds) { if len(res) != len(miss) {
return visit, errors.New(fmt.Sprintf("MGetRoomVisitCount fail,miss match len,%v-%v", len(res), len(groupIds))) return visit, errors.New(fmt.Sprintf("MGetRoomVisitCount fail,miss match len,%v-%v", len(res), len(miss)))
} }
for i, groupId := range groupIds { for i, groupId := range miss {
if cnt, ok := res[i].(string); ok { if cnt, ok := res[i].(string); ok {
visit[groupId] = cnt visit[groupId] = cnt
_ = roomVisitCntCache.SetWithExpire(groupId, cnt, time.Minute*15)
} }
} }
return visit, nil return visit, nil
......