diff --git a/_const/redis_key/groupPower_k/group_power_star.go b/_const/redis_key/groupPower_k/group_power_star.go index f54f7593cf366241b1ba9f30ce3f003fe1bcb499..317b9558f68b1c118fa151357cc64b21e7ca25aa 100644 --- a/_const/redis_key/groupPower_k/group_power_star.go +++ b/_const/redis_key/groupPower_k/group_power_star.go @@ -8,9 +8,10 @@ import ( // 家族之星榜单 // type: 1:送礼 2:活跃 3:收礼 -const GroupPowerStarPrefix = "groupPowerStar:${type}:${period}:${groupPowerId}" // zset member:userId score:分数 +// date:天/周/月的开始时间 +const GroupPowerStarPrefix = "groupPowerStar:${type}:${period}:${groupPowerId}:${date}" // zset member:userId score:分数 -func GetGroupPowerStarRankKey(_type groupPower_e.GroupPowerStarType, period string, groupPowerId uint64) string { +func GetGroupPowerStarRankKey(_type groupPower_e.GroupPowerStarType, period string, groupPowerId uint64, date string) string { return redis_key.ReplaceKey(GroupPowerStarPrefix, - fmt.Sprintf("%d", _type), period, fmt.Sprintf("%d", groupPowerId)) + fmt.Sprintf("%d", _type), period, fmt.Sprintf("%d", groupPowerId), date) } diff --git a/domain/cache/groupPower_c/group_power_star.go b/domain/cache/groupPower_c/group_power_star.go index 63fe071f2343ae6b05bd9233dda77066165f6d3b..018f7c3968145fce2ac35e1bb664d6dc5ea2cbef 100644 --- a/domain/cache/groupPower_c/group_power_star.go +++ b/domain/cache/groupPower_c/group_power_star.go @@ -5,6 +5,8 @@ import ( "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/resource/mysql" "github.com/go-redis/redis/v8" + "github.com/jinzhu/now" + "github.com/pkg/errors" "github.com/spf13/cast" "hilo-group/_const/enum/groupPower_e" "hilo-group/_const/redis_key/groupPower_k" @@ -28,7 +30,16 @@ func IncrGroupPowerDayStarScore(model *domain.Model, groupPowerId, userId mysql. "month": time.Hour * 24 * 7 * 30 * 2, } for _, period := range []string{"day", "week", "month"} { - key := groupPower_k.GetGroupPowerStarRankKey(_type, period, groupPowerId) + date := "" + switch period { + case "day": + date = time.Now().Format("2006-01-02") + case "week": + date = now.BeginningOfWeek().Format("2006-01-02") + case "month": + date = now.BeginningOfMonth().Format("2006-01-02") + } + key := groupPower_k.GetGroupPowerStarRankKey(_type, period, groupPowerId, date) model.RedisCluster.ZIncrBy(model, key, float64(score), fmt.Sprintf("%d", userId)) model.RedisCluster.Expire(model, key, ttl[period]) } @@ -38,7 +49,19 @@ func IncrGroupPowerDayStarScore(model *domain.Model, groupPowerId, userId mysql. // 获取家族之星排行 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) + date := "" + switch period { + case "day": + date = time.Now().Format("2006-01-02") + case "week": + date = now.BeginningOfWeek().Format("2006-01-02") + case "month": + date = now.BeginningOfMonth().Format("2006-01-02") + } + if len(date) <= 0 { + return res, errors.New("illegal date") + } + key := groupPower_k.GetGroupPowerStarRankKey(_type, period, groupPowerId, date) rows, err := model.RedisCluster.ZRevRangeByScoreWithScores(model, key, &redis.ZRangeBy{ Min: "-inf", Max: "+inf",