Commit 80afaac2 authored by hujiebin's avatar hujiebin

feat:优化MGetRoomVisittCnt LRU

parent cd3386b3
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"git.hilo.cn/hilo-common/mylogrus" "git.hilo.cn/hilo-common/mylogrus"
"git.hilo.cn/hilo-common/resource/config" "git.hilo.cn/hilo-common/resource/config"
"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"
"github.com/spf13/cast" "github.com/spf13/cast"
"hilo-group/_const/redis_key" "hilo-group/_const/redis_key"
...@@ -79,19 +80,25 @@ func GetUserRoomVisit(userId uint64) (map[string]int64, error) { ...@@ -79,19 +80,25 @@ func GetUserRoomVisit(userId uint64) (map[string]int64, error) {
// 批量获取房间访问人数 // 批量获取房间访问人数
// 带上lru // 带上lru
var roomVisitCountLru = gcache.New(100).LRU().Build()
func MGetRoomVisitCount(groupIds []string) (map[string]string, error) { func MGetRoomVisitCount(groupIds []string) (map[string]string, error) {
visit := make(map[string]string) visit := make(map[string]string)
if len(groupIds) <= 0 { if len(groupIds) <= 0 {
return visit, nil return visit, nil
} }
// 用redisCluster中的zset代替,只是取出大于100的 lKey := "room:visit:count"
if data, err := roomVisitCountLru.Get(lKey); err == nil {
return data.(map[string]string), nil
}
// 用redisCluster中的zset代替,只是取出大于10的
zKey := redis_key.GetPrefixRoomVisitCountZset() zKey := redis_key.GetPrefixRoomVisitCountZset()
cnt := 100 cnt := 10
if !config.AppIsRelease() { if !config.AppIsRelease() {
cnt = 0 cnt = 0
} }
zRes, err := redisCli.GetClusterRedis().ZRevRangeByScoreWithScores(context.Background(), zKey, &redis2.ZRangeBy{ zRes, err := redisCli.GetClusterRedis().ZRevRangeByScoreWithScores(context.Background(), zKey, &redis2.ZRangeBy{
Min: fmt.Sprintf("%d", cnt), // 100人数以上的。 Min: fmt.Sprintf("%d", cnt),
Max: "+inf", Max: "+inf",
}).Result() }).Result()
if err != nil { if err != nil {
...@@ -101,6 +108,7 @@ func MGetRoomVisitCount(groupIds []string) (map[string]string, error) { ...@@ -101,6 +108,7 @@ func MGetRoomVisitCount(groupIds []string) (map[string]string, error) {
for _, v := range zRes { for _, v := range zRes {
visit[cast.ToString(v.Member)] = cast.ToString(v.Score) visit[cast.ToString(v.Member)] = cast.ToString(v.Score)
} }
_ = roomVisitCountLru.SetWithExpire(lKey, visit, time.Minute*15)
return visit, nil return visit, nil
} }
......
...@@ -31,11 +31,10 @@ var bannedGroupCache = gcache.New(100).LRU().Build() ...@@ -31,11 +31,10 @@ var bannedGroupCache = gcache.New(100).LRU().Build()
func GetBannedGroups(model *domain.Model) ([]GroupBanned, error) { func GetBannedGroups(model *domain.Model) ([]GroupBanned, error) {
key := "banned" key := "banned"
if data, err := bannedGroupCache.Get(key); err == nil { if data, err := bannedGroupCache.Get(key); err == nil {
model.Log.Infof("GetBannedGroups cache:%v", len(data.([]GroupBanned)))
return data.([]GroupBanned), nil return data.([]GroupBanned), nil
} }
result := make([]GroupBanned, 0) result := make([]GroupBanned, 0)
err := model.Db.Find(&result).Error err := model.DB().Find(&result).Error
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -926,7 +926,6 @@ func GetMicHasInGroupNum(model *domain.Model) (map[string]int64, error) { ...@@ -926,7 +926,6 @@ func GetMicHasInGroupNum(model *domain.Model) (map[string]int64, error) {
} }
// cache 1min // cache 1min
_ = micGroupNumCache.SetWithExpire(micGroupNumKey, resultGroupUuids, time.Minute*15) _ = micGroupNumCache.SetWithExpire(micGroupNumKey, resultGroupUuids, time.Minute*15)
//model.Log.Infof("GetMicHasInGroupNum cache miss:%v", resultGroupUuids)
return resultGroupUuids, nil return resultGroupUuids, nil
} }
......
...@@ -344,7 +344,7 @@ func RoomLivingExistsGroup(model *domain.Model) (map[string]map[uint64]struct{}, ...@@ -344,7 +344,7 @@ func RoomLivingExistsGroup(model *domain.Model) (map[string]map[uint64]struct{},
groupGroup[tempGroupUid] = map[uint64]struct{}{userId: {}} groupGroup[tempGroupUid] = map[uint64]struct{}{userId: {}}
} }
} }
model.Log.Infof("RoomLivingExistsGroup size = %d", len(groupGroup)) //model.Log.Infof("RoomLivingExistsGroup size = %d", len(groupGroup))
return groupGroup, nil return groupGroup, nil
} }
...@@ -420,15 +420,12 @@ func BatchGetRoomVisitCount(logE *logrus.Entry, groupIds []string) (map[string]i ...@@ -420,15 +420,12 @@ func BatchGetRoomVisitCount(logE *logrus.Entry, groupIds []string) (map[string]i
if err != nil { if err != nil {
return nil, err return nil, err
} }
//logE.Infof("MGetRoomVisitCount:%v", roomVisitCount)
visitCount := make(map[string]int64) visitCount := make(map[string]int64)
for _, groupId := range groupIds { for _, groupId := range groupIds {
// 先从二级缓存中找 // 先从二级缓存中找
if c, ok := roomVisitCount[groupId]; ok { if c, ok := roomVisitCount[groupId]; ok {
if vc, err := strconv.ParseInt(c, 10, 64); err == nil && vc > 0 { if vc, err := strconv.ParseInt(c, 10, 64); err == nil && vc > 0 {
logE.Debugf("GetRoomVisitCount, from roomVisitCount %s - %d", groupId, vc)
visitCount[groupId] = vc visitCount[groupId] = vc
} }
} else { } else {
......
...@@ -81,14 +81,6 @@ func GetPopularGroups(c *gin.Context) (*mycontext.MyContext, error) { ...@@ -81,14 +81,6 @@ func GetPopularGroups(c *gin.Context) (*mycontext.MyContext, error) {
model.Log.Errorf("GetUserCountryArea 获取国家资源错误 userId:%d, err:%v", myUserId, err) model.Log.Errorf("GetUserCountryArea 获取国家资源错误 userId:%d, err:%v", myUserId, err)
return myContext, err return myContext, err
} }
/* 2022-06-30 老板说先不分区
regions, err := res_m.GetAllLangRegion(model)
if err != nil {
return myContext, err
}
myRegion := regions[myCountry]
model.Log.Infof("GetPopularGroups: user %d, name = %s, country = %s, region = %s", myUserId, myNick, myCountry, myRegion)
*/
bannedGroups, err := group_m.GetBannedGroupsMap(model) bannedGroups, err := group_m.GetBannedGroupsMap(model)
if err != nil { if err != nil {
return myContext, err return myContext, err
...@@ -100,7 +92,6 @@ func GetPopularGroups(c *gin.Context) (*mycontext.MyContext, error) { ...@@ -100,7 +92,6 @@ func GetPopularGroups(c *gin.Context) (*mycontext.MyContext, error) {
return myContext, err return myContext, err
} }
gameGroups := group_m.GetGameGroupsMap(model) gameGroups := group_m.GetGameGroupsMap(model)
//model.Log.Infof("GetPopularGroups: page size = %d, page index = %d, banMap %v, hidenMap %v,cost:%v", pageSize, pageIndex, bannedGroups, hiddenGroups, time.Now().Sub(start))
hotGroupList := make([]group_m.GroupInfo, 0) hotGroupList := make([]group_m.GroupInfo, 0)
...@@ -109,7 +100,7 @@ func GetPopularGroups(c *gin.Context) (*mycontext.MyContext, error) { ...@@ -109,7 +100,7 @@ func GetPopularGroups(c *gin.Context) (*mycontext.MyContext, error) {
if err != nil { if err != nil {
return myContext, err return myContext, err
} }
model.Log.Infof("GetMicHasInGroupNum: cost %v", time.Now().Sub(start)) model.Log.Infof("GetMicHasInGroupNum: cost %v", time.Now().Sub(start)) // 正服高峰期耗时也很短的。
banCount := 0 banCount := 0
hiddenCount := 0 hiddenCount := 0
...@@ -131,7 +122,6 @@ func GetPopularGroups(c *gin.Context) (*mycontext.MyContext, error) { ...@@ -131,7 +122,6 @@ func GetPopularGroups(c *gin.Context) (*mycontext.MyContext, error) {
} }
groupIds = append(groupIds, i) groupIds = append(groupIds, i)
} }
//model.Log.Infof("GetPopularGroups, micGroupNum: %v, banned %d, hidden %d,cost:%v", micGroupNum, banCount, hiddenCount, time.Now().Sub(start))
// 3. 处理置顶群 // 3. 处理置顶群
topGroupIds, err := getTopGroups(model, bannedGroups, hiddenGroups, area) topGroupIds, err := getTopGroups(model, bannedGroups, hiddenGroups, area)
...@@ -161,24 +151,10 @@ func GetPopularGroups(c *gin.Context) (*mycontext.MyContext, error) { ...@@ -161,24 +151,10 @@ func GetPopularGroups(c *gin.Context) (*mycontext.MyContext, error) {
} }
for _, i := range topGroupIds { for _, i := range topGroupIds {
/* 2022-06-30 老板说先不分区 // 置顶只对同语言区的生效
if myRegion != regions[topGroupInfo[i].Country] {
continue
}
*/
// 已经置顶的,直接进队列,不再参与排序 // 已经置顶的,直接进队列,不再参与排序
hotGroupList = append(hotGroupList, groups[i]) hotGroupList = append(hotGroupList, groups[i])
//delete(groupIds, i)
} }
// for pretty log
//logstr := ""
//for _, i := range hotGroupList {
//logstr += " " + i.ImGroupId
//}
//logstr += " |"
myArea := fmt.Sprintf("%d", area) myArea := fmt.Sprintf("%d", area)
// 国家区域信息 // 国家区域信息
resAreaMap, err := res_c.GetCountryAreaMap(model) resAreaMap, err := res_c.GetCountryAreaMap(model)
...@@ -204,16 +180,14 @@ func GetPopularGroups(c *gin.Context) (*mycontext.MyContext, error) { ...@@ -204,16 +180,14 @@ func GetPopularGroups(c *gin.Context) (*mycontext.MyContext, error) {
} }
} }
} }
//model.Log.Infof("GetPopularGroups, countryScore[*]: %v,cost:%v", countryScore, time.Now().Sub(start))
now := time.Now() now := time.Now()
bTime := now.Add(-time.Minute * 30) bTime := now.Add(-time.Minute * 30)
g := gift_cv.GiftOperate{SceneType: gift_e.GroupSceneType} g := gift_cv.GiftOperate{SceneType: gift_e.GroupSceneType}
diamonds, err := g.GetRangeConsumeSummaryV2(bTime, now, groupIds) diamonds, err := g.GetRangeConsumeSummaryV2(bTime, now, groupIds) // 获取1小时内的送礼榜
if err != nil { if err != nil {
return myContext, err return myContext, err
} }
//model.Log.Infof("GetPopularGroups, diamonds in 30 mins: %v,cost:%v", diamonds, time.Now().Sub(start))
visitCount, err := group_m.BatchGetRoomVisitCount(model.Log, groupIds) visitCount, err := group_m.BatchGetRoomVisitCount(model.Log, groupIds)
if err != nil { if err != nil {
...@@ -269,12 +243,6 @@ func GetPopularGroups(c *gin.Context) (*mycontext.MyContext, error) { ...@@ -269,12 +243,6 @@ func GetPopularGroups(c *gin.Context) (*mycontext.MyContext, error) {
// * 同国家 ^ 麦上有人 + 开放群 - 需要等级的群 // * 同国家 ^ 麦上有人 + 开放群 - 需要等级的群
for _, g := range sortedGroupIds { for _, g := range sortedGroupIds {
hotGroupList = append(hotGroupList, groups[g]) hotGroupList = append(hotGroupList, groups[g])
//prefix := " "
//if countryScore[g] == 0 {
//prefix += "*"
//}
//logstr += prefix + g + ":" + groups[g].Code + ":" + strconv.Itoa(int(micGroupNum[g])) +
// ":" + strconv.FormatUint(diamonds[g], 10) + ":" + strconv.Itoa(int(visitCount[g]))
} }
total := len(hotGroupList) total := len(hotGroupList)
//model.Log.Infof("GetPopularGroups: hotGroupList size = %d,cost:%v", total, time.Now().Sub(start)) //model.Log.Infof("GetPopularGroups: hotGroupList size = %d,cost:%v", total, time.Now().Sub(start))
......
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