diff --git a/cron/cron.go b/cron/cron.go index eeef2b4a420c0a1b6d4b82eb61d2dfb1bcd99752..c2c4b5ece3b2f079a9fc86a7de85e675621cc390 100644 --- a/cron/cron.go +++ b/cron/cron.go @@ -4,9 +4,11 @@ import ( "git.hilo.cn/hilo-common/resource/config" "hilo-user/cron/cp_cron" "hilo-user/cron/gift_cron" + "hilo-user/cron/user_cron" ) func Init() { + user_cron.SyncGiftRecommendUsers() // 同步送礼推荐用户,多进程都需要执行 if !config.IsMaster() { return } diff --git a/cron/user_cron/gift_user.go b/cron/user_cron/gift_user.go new file mode 100644 index 0000000000000000000000000000000000000000..3a4c72665f03fdc8e3edf97a5276d63a037c1cc5 --- /dev/null +++ b/cron/user_cron/gift_user.go @@ -0,0 +1,20 @@ +package user_cron + +import ( + "git.hilo.cn/hilo-common/domain" + "github.com/robfig/cron" + "hilo-user/domain/model/recommend_m" +) + +// 定期同步礼物推荐用户 +func SyncGiftRecommendUsers() { + go recommend_m.SyncPastTop50SendGiftUsers(domain.CreateModelNil()) // 启动先同步一次 + + c := cron.New() + spec := "0 */5 * * * ?" + _ = c.AddFunc(spec, func() { + var model = domain.CreateModelNil() + recommend_m.SyncPastTop50SendGiftUsers(model) + }) + c.Start() +} diff --git a/domain/model/recommend_m/gift.go b/domain/model/recommend_m/gift.go index 430691d0543bf86830687032abb7931997af341b..fae95a8b29acac6ebe1906ded1a35f86d596b211 100644 --- a/domain/model/recommend_m/gift.go +++ b/domain/model/recommend_m/gift.go @@ -17,12 +17,7 @@ var recommendUserGiftKey = "recommendUserGiftKey" var recommendUserGiftCache = gcache.New(1).LRU().Build() // 推荐最近送礼的50人,最近12小时赠送礼物大于100k的用户 -// 先lru cache,后db -// ttl: 5min -func GetPastTop50SendGiftUsers(model *domain.Model) []recommendUserGift { - if data, err := recommendUserGiftCache.Get(recommendUserGiftKey); err == nil { - return data.([]recommendUserGift) - } +func SyncPastTop50SendGiftUsers(model *domain.Model) { limitUserIds, _ := GetBillboardLimitUserList(model) var res []recommendUserGift if err := model.DB().Table("gift_operate g").Joins("INNER JOIN user u ON u.id = g.send_user_id").Select("send_user_id,SUM(send_user_diamond) send_user_diamond"). @@ -33,8 +28,15 @@ func GetPastTop50SendGiftUsers(model *domain.Model) []recommendUserGift { Having("send_user_diamond > 100000").Order("send_user_diamond DESC").Limit(34).Find(&res).Error; err != nil { model.Log.Errorf("GetPastTop50SendGiftUsers fail:%v", err) } - recommendUserGiftCache.SetWithExpire(recommendUserGiftKey, res, time.Minute*5) - return res + _ = recommendUserGiftCache.Set(recommendUserGiftKey, res) +} + +// 只从缓存中拿 +func GetPastTop50SendGiftUsers(model *domain.Model) []recommendUserGift { + if data, err := recommendUserGiftCache.Get(recommendUserGiftKey); err == nil { + return data.([]recommendUserGift) + } + return []recommendUserGift{} } // 榜单黑名单