From e8e1510214ca40a6cc891150a0bca5a02c8cc203 Mon Sep 17 00:00:00 2001 From: JiebinHu <458249864@qq.com> Date: Sun, 27 Aug 2023 14:08:20 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9Aredis=E7=89=88=E6=9C=AC=E5=AE=B6?= =?UTF-8?q?=E6=97=8F=E6=A6=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关键还是脚本同步逻辑! --- .../groupPower_k/group_power_star.go | 16 ++++ cron/gift_cron/send_gift_redis.go | 7 +- domain/cache/groupPower_c/group_power_star.go | 83 +++++++++++++++++++ domain/model/groupPower_m/group_star.go | 23 +---- domain/service/group_mic_s/group_mic.go | 6 +- route/group_power_r/group_power.go | 3 +- route/group_power_r/group_rank.go | 3 +- 7 files changed, 114 insertions(+), 27 deletions(-) create mode 100644 _const/redis_key/groupPower_k/group_power_star.go create mode 100644 domain/cache/groupPower_c/group_power_star.go diff --git a/_const/redis_key/groupPower_k/group_power_star.go b/_const/redis_key/groupPower_k/group_power_star.go new file mode 100644 index 0000000..f54f759 --- /dev/null +++ b/_const/redis_key/groupPower_k/group_power_star.go @@ -0,0 +1,16 @@ +package groupPower_k + +import ( + "fmt" + "hilo-group/_const/enum/groupPower_e" + "hilo-group/_const/redis_key" +) + +// 家族之星榜单 +// type: 1:送礼 2:活跃 3:收礼 +const GroupPowerStarPrefix = "groupPowerStar:${type}:${period}:${groupPowerId}" // zset member:userId score:分数 + +func GetGroupPowerStarRankKey(_type groupPower_e.GroupPowerStarType, period string, groupPowerId uint64) string { + return redis_key.ReplaceKey(GroupPowerStarPrefix, + fmt.Sprintf("%d", _type), period, fmt.Sprintf("%d", groupPowerId)) +} diff --git a/cron/gift_cron/send_gift_redis.go b/cron/gift_cron/send_gift_redis.go index 908b17f..1a93f16 100644 --- a/cron/gift_cron/send_gift_redis.go +++ b/cron/gift_cron/send_gift_redis.go @@ -9,6 +9,7 @@ import ( "hilo-group/_const/enum/gift_e" "hilo-group/_const/enum/groupPower_e" "hilo-group/domain/cache/gift_c" + "hilo-group/domain/cache/groupPower_c" "hilo-group/domain/event/gift_ev" "hilo-group/domain/model/groupPower_m" "hilo-group/domain/model/group_m" @@ -77,7 +78,8 @@ func groupPowerStar(model *domain.Model, sendGiftEvent *gift_ev.SendGiftEvent) { if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeFamous, diamonds, 0); err != nil { model.Log.Errorf("IncrGroupPowerMonthStarScore famous fail:%v", err) } - if err := groupPower_m.IncrGroupPowerDayStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeFamous, diamonds, 0); err != nil { + if err := groupPower_c.IncrGroupPowerDayStarScore(model, data.GroupPowerId, data.UserId, + groupPower_e.GroupPowerStarTypeFamous, diamonds); err != nil { model.Log.Errorf("IncrGroupPowerDayStarScore famous fail:%v", err) } } @@ -88,7 +90,8 @@ func groupPowerStar(model *domain.Model, sendGiftEvent *gift_ev.SendGiftEvent) { if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeCharm, diamonds, 0); err != nil { model.Log.Errorf("IncrGroupPowerMonthStarScore charm fail:%v", err) } - if err := groupPower_m.IncrGroupPowerDayStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeCharm, diamonds, 0); err != nil { + if err := groupPower_c.IncrGroupPowerDayStarScore(model, data.GroupPowerId, data.UserId, + groupPower_e.GroupPowerStarTypeCharm, diamonds); err != nil { model.Log.Errorf("IncrGroupPowerDayStarScore charm fail:%v", err) } } diff --git a/domain/cache/groupPower_c/group_power_star.go b/domain/cache/groupPower_c/group_power_star.go new file mode 100644 index 0000000..63fe071 --- /dev/null +++ b/domain/cache/groupPower_c/group_power_star.go @@ -0,0 +1,83 @@ +package groupPower_c + +import ( + "fmt" + "git.hilo.cn/hilo-common/domain" + "git.hilo.cn/hilo-common/resource/mysql" + "github.com/go-redis/redis/v8" + "github.com/spf13/cast" + "hilo-group/_const/enum/groupPower_e" + "hilo-group/_const/redis_key/groupPower_k" + "time" +) + +// 家族之星-排名 +type GroupPowerStarRank struct { + Period string + GroupPowerId mysql.ID + UserId mysql.ID + Type groupPower_e.GroupPowerStarType + Score mysql.Num +} + +// 增加家族之星分数 +func IncrGroupPowerDayStarScore(model *domain.Model, groupPowerId, userId mysql.ID, _type groupPower_e.GroupPowerStarType, score mysql.Num) error { + ttl := map[string]time.Duration{ + "day": time.Hour * 24 * 7, + "week": time.Hour * 24 * 7 * 30, + "month": time.Hour * 24 * 7 * 30 * 2, + } + for _, period := range []string{"day", "week", "month"} { + key := groupPower_k.GetGroupPowerStarRankKey(_type, period, groupPowerId) + model.RedisCluster.ZIncrBy(model, key, float64(score), fmt.Sprintf("%d", userId)) + model.RedisCluster.Expire(model, key, ttl[period]) + } + return nil +} + +// 获取家族之星排行 +func GetGroupPowerStarRankPeriod(model *domain.Model, period string, groupPowerId mysql.ID, _type groupPower_e.GroupPowerStarType, offset, limit int) ([]*GroupPowerStarRank, error) { + var res []*GroupPowerStarRank + key := groupPower_k.GetGroupPowerStarRankKey(_type, period, groupPowerId) + rows, err := model.RedisCluster.ZRevRangeByScoreWithScores(model, key, &redis.ZRangeBy{ + Min: "-inf", + Max: "+inf", + Offset: int64(offset), + Count: int64(limit), + }).Result() + if err != nil { + model.Log.Errorf("GetGroupPowerStarRankPeriod fail:%v", err) + return res, err + } + for _, v := range rows { + res = append(res, &GroupPowerStarRank{ + Period: period, + GroupPowerId: groupPowerId, + UserId: cast.ToUint64(v.Member), + Type: _type, + Score: mysql.Num(v.Score), + }) + } + return res, nil +} + +// 获取家族之星三个排行榜的各自第一名 +func GetGroupPowerMonthStartTop1(model *domain.Model, groupPowerId mysql.ID) ([]*GroupPowerStarRank, error) { + var res []*GroupPowerStarRank + r1, err := GetGroupPowerStarRankPeriod(model, "month", groupPowerId, groupPower_e.GroupPowerStarTypeFamous, 0, 1) + if err != nil { + return res, err + } + r2, err := GetGroupPowerStarRankPeriod(model, "month", groupPowerId, groupPower_e.GroupPowerStarTypeActive, 0, 1) + if err != nil { + return res, err + } + r3, err := GetGroupPowerStarRankPeriod(model, "month", groupPowerId, groupPower_e.GroupPowerStarTypeCharm, 0, 1) + if err != nil { + return res, err + } + res = append(res, r1...) + res = append(res, r2...) + res = append(res, r3...) + return res, nil +} diff --git a/domain/model/groupPower_m/group_star.go b/domain/model/groupPower_m/group_star.go index ee52a2b..bc653ed 100644 --- a/domain/model/groupPower_m/group_star.go +++ b/domain/model/groupPower_m/group_star.go @@ -59,6 +59,7 @@ func IncrGroupPowerMonthStarScore(model *domain.Model, groupPowerId, userId mysq } // 增加家族之星分数 +// Deprecated: 用redis代替 func IncrGroupPowerDayStarScore(model *domain.Model, groupPowerId, userId mysql.ID, _type groupPower_e.GroupPowerStarType, score mysql.Num, lastCalTs int64) error { star := &GroupPowerDayStar{ Date: time.Now(), @@ -81,28 +82,6 @@ func IncrGroupPowerDayStarScore(model *domain.Model, groupPowerId, userId mysql. return nil } -// 获取家族之星分数 -// 允许返回gorm.ErrRecordNotFound -func GetGroupPowerDayStar(model *domain.Model, groupPowerId, userId mysql.ID, _type groupPower_e.GroupPowerStarType) (*GroupPowerDayStar, error) { - res := new(GroupPowerDayStar) - date := time.Now() - if err := model.DB().Where("date = ? AND group_power_id = ? AND user_id = ? AND `type` = ?", date.Format("2006-01-02"), groupPowerId, userId, _type).First(res).Error; err != nil { - return nil, err - } - return res, nil -} - -// 获取家族之星分数 -// 允许返回gorm.ErrRecordNotFound -func GetGroupPowerMonthStar(model *domain.Model, groupPowerId, userId mysql.ID, _type groupPower_e.GroupPowerStarType) (*GroupPowerMonthStar, error) { - res := new(GroupPowerMonthStar) - month := time.Now().Format("200601") - if err := model.DB().Where("month = ? AND group_power_id = ? AND user_id = ? AND `type` = ?", month, groupPowerId, userId, _type).First(res).Error; err != nil { - return nil, err - } - return res, nil -} - // 获取家族之星排行 func GetGroupPowerMonthStarRank(model *domain.Model, groupPowerId mysql.ID, _type groupPower_e.GroupPowerStarType, offset, limit int, month string) ([]*GroupPowerMonthStar, error) { var res []*GroupPowerMonthStar diff --git a/domain/service/group_mic_s/group_mic.go b/domain/service/group_mic_s/group_mic.go index eb159bc..2fdc2b4 100644 --- a/domain/service/group_mic_s/group_mic.go +++ b/domain/service/group_mic_s/group_mic.go @@ -8,8 +8,10 @@ import ( "git.hilo.cn/hilo-common/resource/redisCli" "git.hilo.cn/hilo-common/rpc" uuid "github.com/satori/go.uuid" + "hilo-group/_const/enum/groupPower_e" "hilo-group/_const/enum/group_e" "hilo-group/_const/redis_key" + "hilo-group/domain/cache/groupPower_c" "hilo-group/domain/event/group_ev" "hilo-group/domain/model/groupPower_m" "hilo-group/domain/model/group_m" @@ -325,7 +327,9 @@ func (s *GroupMicService) IncrGroupPowerOnMicExpAndTime(groupId string, userId u model.Log.Errorf("IncrGroupPowerStarOnMicMonth fail:%v", err) } // 增加势力上麦时长-天 - if err := groupPower_m.IncrGroupPowerStarOnMicDay(model, groupPowerId, userId, joinMicTimestamp); err != nil { + //if err := groupPower_m.IncrGroupPowerStarOnMicDay(model, groupPowerId, userId, joinMicTimestamp); err != nil { + if err := groupPower_c.IncrGroupPowerDayStarScore(model, groupPowerId, userId, + groupPower_e.GroupPowerStarTypeActive, 60); err != nil { model.Log.Errorf("IncrGroupPowerStarOnMicDay fail:%v", err) } return nil diff --git a/route/group_power_r/group_power.go b/route/group_power_r/group_power.go index 3ec8e3c..da591a2 100644 --- a/route/group_power_r/group_power.go +++ b/route/group_power_r/group_power.go @@ -23,6 +23,7 @@ import ( "hilo-group/cv/group_power_cv" "hilo-group/cv/medal_cv" "hilo-group/cv/user_cv" + "hilo-group/domain/cache/groupPower_c" "hilo-group/domain/cache/res_c" "hilo-group/domain/cache/user_c" "hilo-group/domain/model/game_m" @@ -820,7 +821,7 @@ func GroupPowerInfo(c *gin.Context) (*mycontext.MyContext, error) { } } // 补上家族之星三个榜一 - stars, err := groupPower_m.GetGroupPowerMonthStartTop1(model, gp.ID) + stars, err := groupPower_c.GetGroupPowerMonthStartTop1(model, gp.ID) if err != nil { return myContext, myerr.WrapErr(err) } diff --git a/route/group_power_r/group_rank.go b/route/group_power_r/group_rank.go index d431b3e..ba3b895 100644 --- a/route/group_power_r/group_rank.go +++ b/route/group_power_r/group_rank.go @@ -10,6 +10,7 @@ import ( "hilo-group/_const/enum/groupPower_e" "hilo-group/cv/group_power_cv" "hilo-group/cv/user_cv" + "hilo-group/domain/cache/groupPower_c" "hilo-group/domain/model/groupPower_m" "hilo-group/domain/model/user_m" "hilo-group/domain/service/group_power_s" @@ -264,7 +265,7 @@ func GroupPowerStarPeriod(c *gin.Context) (*mycontext.MyContext, error) { } var model = domain.CreateModelContext(myContext) offset, limit := (param.PageIndex-1)*param.PageSize, param.PageSize - rank, err := groupPower_m.GetGroupPowerStarRankPeriod(model, period, param.GroupPowerId, param.Type, offset, limit) + rank, err := groupPower_c.GetGroupPowerStarRankPeriod(model, period, param.GroupPowerId, param.Type, offset, limit) if err != nil { return myContext, err } -- 2.22.0