蛋蛋星球 后台端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_auto_caching_top100.go 2.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/utils"
  5. "applet/app/utils/cache"
  6. "fmt"
  7. "xorm.io/xorm"
  8. )
  9. const AutoCachingTop100LockKey = "egg_energy_auto_caching_top100_lock_key"
  10. const HeroListRedisRankKey = "EggEnergy:HomePage:HeroList:Rank:%d" // 1.kind
  11. func AutoCachingTop100(engine *xorm.Engine) {
  12. fmt.Println("egg_energy_auto_caching_top100...")
  13. defer func() {
  14. if err := recover(); err != nil {
  15. fmt.Println(err)
  16. return
  17. }
  18. }()
  19. fmt.Println("----------------------------AutoCachingTop100_Begin-------------------------------")
  20. getString, _ := cache.GetString(AutoCachingTop100LockKey)
  21. if getString != "" {
  22. fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次缓存前一百用户未执行完")
  23. return
  24. }
  25. cache.SetEx(AutoCachingTop100LockKey, "running", 10*60) // 10 min
  26. defer cache.Del(AutoCachingTop100LockKey)
  27. for i := 1; i <= 3; i++ {
  28. sql := "SELECT user_virtual_coin_flow_aggregation.*" +
  29. "FROM `user_virtual_coin_flow_aggregation` " +
  30. "WHERE 1=1"
  31. switch i {
  32. case 1:
  33. sql += " ORDER BY user_virtual_coin_flow_aggregation.today_data"
  34. case 2:
  35. sql += " ORDER BY user_virtual_coin_flow_aggregation.this_week_data"
  36. case 3:
  37. sql += " ORDER BY user_virtual_coin_flow_aggregation.this_month_data"
  38. }
  39. sql += " Desc LIMIT %d, %d;"
  40. sql = fmt.Sprintf(sql, 0, 100)
  41. res, err := db.QueryNativeString(engine, sql)
  42. if err != nil {
  43. fmt.Println("AutoCachingTop100_Err ===================>" + err.Error())
  44. return
  45. }
  46. rankCacheKey := fmt.Sprintf(HeroListRedisRankKey, i)
  47. for _, item := range res {
  48. switch i {
  49. case 1:
  50. _, err = cache.ZAdd(rankCacheKey, utils.StrToFloat64(item["today_data"]), item["uid"])
  51. if err != nil {
  52. fmt.Println("AutoCachingTop100_FailedToCacheData_Err ==================>" + err.Error())
  53. return
  54. }
  55. case 2:
  56. _, err := cache.ZAdd(rankCacheKey, utils.StrToFloat64(item["this_week_data"]), item["uid"])
  57. if err != nil {
  58. fmt.Println("AutoCachingTop100_FailedToCacheData_Err ==================>" + err.Error())
  59. return
  60. }
  61. case 3:
  62. _, err := cache.ZAdd(rankCacheKey, utils.StrToFloat64(item["this_month_data"]), item["uid"])
  63. if err != nil {
  64. fmt.Println("AutoCachingTop100_FailedToCacheData_Err ==================>" + err.Error())
  65. return
  66. }
  67. }
  68. }
  69. fmt.Println("----------------------------AutoCachingTop100_End-------------------------------")
  70. }
  71. }