Commit aa8bf199 authored by hujiebin's avatar hujiebin

群等级lru

parent 0601ede0
...@@ -4,8 +4,8 @@ import ( ...@@ -4,8 +4,8 @@ import (
"encoding/json" "encoding/json"
"git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/mycontext" "git.hilo.cn/hilo-common/mycontext"
"git.hilo.cn/hilo-common/resource/config"
"git.hilo.cn/hilo-common/resource/mysql" "git.hilo.cn/hilo-common/resource/mysql"
"github.com/bluele/gcache"
"gorm.io/gorm" "gorm.io/gorm"
"hilo-group/_const/enum/group_e" "hilo-group/_const/enum/group_e"
"hilo-group/_const/enum/mgr_e" "hilo-group/_const/enum/mgr_e"
...@@ -18,7 +18,6 @@ import ( ...@@ -18,7 +18,6 @@ import (
"hilo-group/domain/model/user_m" "hilo-group/domain/model/user_m"
"hilo-group/domain/service/signal_s" "hilo-group/domain/service/signal_s"
"hilo-group/myerr" "hilo-group/myerr"
"strconv"
"time" "time"
) )
...@@ -31,7 +30,7 @@ func NewGroupService(myContext *mycontext.MyContext) *GroupService { ...@@ -31,7 +30,7 @@ func NewGroupService(myContext *mycontext.MyContext) *GroupService {
return &GroupService{svc} return &GroupService{svc}
} }
// 取本周最高的扶持等级 fixme:删除这个过渡函数 // 取本周最高的扶持等级
func (s *GroupService) GetWeekMaxSupportLevelMap() (map[string]uint8, error) { func (s *GroupService) GetWeekMaxSupportLevelMap() (map[string]uint8, error) {
return s.GetSupportLevelMap(time.Now().AddDate(0, 0, -group_e.SUPPORT_LEVEL_PERIOD_DAY)) return s.GetSupportLevelMap(time.Now().AddDate(0, 0, -group_e.SUPPORT_LEVEL_PERIOD_DAY))
} }
...@@ -42,90 +41,57 @@ func (s *GroupService) GetSupportLevelMap(now time.Time) (map[string]uint8, erro ...@@ -42,90 +41,57 @@ func (s *GroupService) GetSupportLevelMap(now time.Time) (map[string]uint8, erro
_, _, period := group_m.GetSupportLevelTime(now) _, _, period := group_m.GetSupportLevelTime(now)
levels, err := GetAllSupportLevel(model, period) levels, err := GetAllSupportLevel(model, period)
if err != nil { if err == nil {
return nil, err return levels, nil
} }
model.Log.Debugf("GetSupportLevelMap, GET %s: %v", period, levels) // cache miss
result := make(map[string]uint8, 0) result, err := group_m.GetAllGroupSupportResult(model.Db, period)
if len(levels) > 0 { if err == nil {
for g, l := range levels { SaveAllSupportLevel(model, period, result)
le, err := strconv.ParseUint(l, 10, 8)
if err == nil {
result[g] = uint8(le)
}
}
} else {
result, err = group_m.GetAllGroupSupportResult(model.Db, period)
if err == nil {
ret, err := SaveAllSupportLevel(model, period, result)
model.Log.Infof("GetSupportLevelMap SAVE ret = %d, err: %v", ret, err)
}
} }
return result, nil return result, nil
} }
// 取本周最高的扶持等级 //// 取本周最高的扶持等级
func (s *GroupService) GetWeekMaxSupportLevelMapByIds(groupIds []string) (map[string]uint8, error) { //func (s *GroupService) GetWeekMaxSupportLevelMapByIds(groupIds []string) (map[string]uint8, error) {
return s.GetSupportLevelMapByIds(groupIds, time.Now().AddDate(0, 0, -group_e.SUPPORT_LEVEL_PERIOD_DAY)) // return s.GetSupportLevelMapByIds(groupIds, time.Now().AddDate(0, 0, -group_e.SUPPORT_LEVEL_PERIOD_DAY))
} //}
//
func (s *GroupService) GetSupportLevelMapByIds(groupIds []string, now time.Time) (map[string]uint8, error) { //func (s *GroupService) GetSupportLevelMapByIds(groupIds []string, now time.Time) (map[string]uint8, error) {
model := domain.CreateModel(s.svc.CtxAndDb) // model := domain.CreateModel(s.svc.CtxAndDb)
//
_, _, period := group_m.GetSupportLevelTime(now) // _, _, period := group_m.GetSupportLevelTime(now)
//
levels, err := GetAllSupportLevel(model, period) // levels, err := GetAllSupportLevel(model, period)
if err != nil { // if err == nil {
return nil, err // return levels, nil
} // }
model.Log.Debugf("GetSupportLevelMapByIds, GET %s: %v", period, levels) // // cache miss
//
// result, err := group_m.GetGroupSupportResult(model.DB(), period, groupIds)
// if err == nil {
// SaveAllSupportLevel(model, period, result)
// }
// return result, nil
//}
result := make(map[string]uint8, 0) // supportLevelLru Cache
if len(levels) > 0 { var supportLevelCache = gcache.New(100).LRU().Build()
for g, l := range levels {
le, err := strconv.ParseUint(l, 10, 8)
if err == nil {
result[g] = uint8(le)
}
}
} else {
result, err = group_m.GetGroupSupportResult(model.DB(), period, groupIds)
if err == nil {
ret, err := SaveAllSupportLevel(model, period, result)
model.Log.Infof("GetSupportLevelMapByIds SAVE ret = %d, err: %v", ret, err)
}
}
return result, nil
}
func SaveAllSupportLevel(model *domain.Model, date string, levels map[string]uint8) (int64, error) { func SaveAllSupportLevel(model *domain.Model, date string, levels map[string]uint8) {
values := make(map[string]interface{}, 0)
for g, l := range levels {
if l > 0 {
values[g] = l
}
}
if len(values) <= 0 {
return 0, nil
}
key := redis_key.GetPrefixSupportLevel(date) key := redis_key.GetPrefixSupportLevel(date)
ret, err := model.Redis.HSet(model, key, values).Result() _ = supportLevelCache.SetWithExpire(key, levels, time.Hour)
return
if err == nil {
// 设置一个TTL保险一些 TODO: 可以优化,保证数据总是有的
ttl := time.Hour
if !config.AppIsRelease() {
ttl = time.Minute
}
model.Redis.Expire(model, key, ttl)
}
return ret, err
} }
func GetAllSupportLevel(model *domain.Model, date string) (map[string]string, error) { func GetAllSupportLevel(model *domain.Model, date string) (map[string]uint8, error) {
key := redis_key.GetPrefixSupportLevel(date) key := redis_key.GetPrefixSupportLevel(date)
return model.Redis.HGetAll(model, key).Result() if data, err := supportLevelCache.Get(key); err == nil {
return data.(map[string]uint8), nil
} else {
return map[string]uint8{}, err
}
} }
func (s *GroupService) GetJoinGroupLimit(userId mysql.ID) (uint, error) { func (s *GroupService) GetJoinGroupLimit(userId mysql.ID) (uint, error) {
......
...@@ -1712,7 +1712,7 @@ func GetGroupByCountryV2(c *gin.Context) (*mycontext.MyContext, error) { ...@@ -1712,7 +1712,7 @@ func GetGroupByCountryV2(c *gin.Context) (*mycontext.MyContext, error) {
// 正在进行的游戏 // 正在进行的游戏
games := game_m.GetNotEndGamesMap(model) games := game_m.GetNotEndGamesMap(model)
// 扶持等级 // 扶持等级
supportLevels, err := group_s.NewGroupService(myContext).GetWeekMaxSupportLevelMapByIds(groupIds) supportLevels, err := group_s.NewGroupService(myContext).GetWeekMaxSupportLevelMap()
if err != nil { if err != nil {
return myContext, err return myContext, err
} }
......
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