Commit aef54de6 authored by hujiebin's avatar hujiebin

lru批量获取房间访问人数

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