|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- package svc
-
- import (
- "applet/app/db"
- "applet/app/utils"
- "applet/app/utils/cache"
- "fmt"
- "xorm.io/xorm"
- )
-
- const AutoCachingTop100LockKey = "egg_energy_auto_caching_top100_lock_key"
- const HeroListRedisRankKey = "EggEnergy:HomePage:HeroList:Rank:%d" // 1.kind
-
- func AutoCachingTop100(engine *xorm.Engine) {
- fmt.Println("egg_energy_auto_caching_top100...")
- defer func() {
- if err := recover(); err != nil {
- fmt.Println(err)
- return
- }
- }()
- fmt.Println("----------------------------AutoCachingTop100_Begin-------------------------------")
-
- getString, _ := cache.GetString(AutoCachingTop100LockKey)
- if getString != "" {
- fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次缓存前一百用户未执行完")
- return
- }
- cache.SetEx(AutoCachingTop100LockKey, "running", 10*60) // 10 min
- defer cache.Del(AutoCachingTop100LockKey)
-
- for i := 1; i <= 3; i++ {
- sql := "SELECT user_virtual_coin_flow_aggregation.*" +
- "FROM `user_virtual_coin_flow_aggregation` " +
- "WHERE 1=1"
- switch i {
- case 1:
- sql += " ORDER BY user_virtual_coin_flow_aggregation.today_data"
- case 2:
- sql += " ORDER BY user_virtual_coin_flow_aggregation.this_week_data"
- case 3:
- sql += " ORDER BY user_virtual_coin_flow_aggregation.this_month_data"
- }
- sql += " Desc LIMIT %d, %d;"
- sql = fmt.Sprintf(sql, 0, 100)
- res, err := db.QueryNativeString(engine, sql)
- if err != nil {
- fmt.Println("AutoCachingTop100_Err ===================>" + err.Error())
- return
- }
- rankCacheKey := fmt.Sprintf(HeroListRedisRankKey, i)
- for _, item := range res {
- switch i {
- case 1:
- _, err = cache.ZAdd(rankCacheKey, utils.StrToFloat64(item["today_data"]), item["uid"])
- if err != nil {
- fmt.Println("AutoCachingTop100_FailedToCacheData_Err ==================>" + err.Error())
- return
- }
- case 2:
- _, err := cache.ZAdd(rankCacheKey, utils.StrToFloat64(item["this_week_data"]), item["uid"])
- if err != nil {
- fmt.Println("AutoCachingTop100_FailedToCacheData_Err ==================>" + err.Error())
- return
- }
- case 3:
- _, err := cache.ZAdd(rankCacheKey, utils.StrToFloat64(item["this_month_data"]), item["uid"])
- if err != nil {
- fmt.Println("AutoCachingTop100_FailedToCacheData_Err ==================>" + err.Error())
- return
- }
- }
- }
-
- fmt.Println("----------------------------AutoCachingTop100_End-------------------------------")
- }
-
- }
|