From 2aee2a04149efb6e26c547749b5c47baef3b5861 Mon Sep 17 00:00:00 2001 From: shenjiachi Date: Tue, 24 Dec 2024 19:53:18 +0800 Subject: [PATCH] feat: add caching top100 and auto update user_virtual_coin_flow_aggregation --- app/task/init.go | 1 + app/task/md/cron_key.go | 1 + app/task/svc/svc_auto_caching_top100.go | 78 +++++++++++++++++++ app/task/svc/svc_auto_caching_top100_test.go | 26 +++++++ .../svc/svc_egg_energy_virtual_coin_flow.go | 66 ++++++++++++++++ app/task/task_egg_energy_virtual_coin_flow.go | 22 ++++++ 6 files changed, 194 insertions(+) create mode 100644 app/task/svc/svc_auto_caching_top100.go create mode 100644 app/task/svc/svc_auto_caching_top100_test.go create mode 100644 app/task/svc/svc_egg_energy_virtual_coin_flow.go create mode 100644 app/task/task_egg_energy_virtual_coin_flow.go diff --git a/app/task/init.go b/app/task/init.go index ebc4517..7bacc70 100644 --- a/app/task/init.go +++ b/app/task/init.go @@ -100,4 +100,5 @@ func initTasks() { jobs[taskMd.CornEggEnergyAutoCachingEggPointStatistics] = taskAutoCachingEggPointStatistics // 蛋蛋分统计落地页-缓存 jobs[taskMd.CornEggEnergyAutoUpdateUserAccess] = taskEggEnergyAutoUpdateUserAccess // es蛋蛋分记录-自动更新访问次数 jobs[taskMd.CornEggEnergyAutoDeleteTableAdvertisingCallback] = taskAutoDeleteTableAdvertisingCallback // 定时清除七天以前的广告回调数据 + jobs[taskMd.CornEggEnergyCoinFlow] = taskEggEnergyCoinFlow // 定时重置用户流水聚合信息 } diff --git a/app/task/md/cron_key.go b/app/task/md/cron_key.go index 8f670b1..eabbe93 100644 --- a/app/task/md/cron_key.go +++ b/app/task/md/cron_key.go @@ -14,4 +14,5 @@ const ( CornEggEnergyAutoCachingEggPointStatistics = "cron_egg_energy_auto_caching_egg_point_statistics" // 缓存蛋蛋分统计落地页 CornEggEnergyAutoUpdateUserAccess = "cron_egg_energy_auto_update_user_access" // 同步用户访问次数到es CornEggEnergyAutoDeleteTableAdvertisingCallback = "cron_egg_energy_auto_delete_table_advertising_callback" // 同步用户访问次数到es + CornEggEnergyCoinFlow = "cron_egg_energy_coin_flow" // 定时重置用户流水聚合信息 ) diff --git a/app/task/svc/svc_auto_caching_top100.go b/app/task/svc/svc_auto_caching_top100.go new file mode 100644 index 0000000..0591fd8 --- /dev/null +++ b/app/task/svc/svc_auto_caching_top100.go @@ -0,0 +1,78 @@ +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-------------------------------") + } + +} diff --git a/app/task/svc/svc_auto_caching_top100_test.go b/app/task/svc/svc_auto_caching_top100_test.go new file mode 100644 index 0000000..947f504 --- /dev/null +++ b/app/task/svc/svc_auto_caching_top100_test.go @@ -0,0 +1,26 @@ +package svc + +import ( + "applet/app/cfg" + "applet/app/db" + "applet/app/utils/cache" + "testing" +) + +func TestAutoCachingTop100(t *testing.T) { + dbcfg := cfg.DBCfg{ + Host: "119.23.182.117:3306", + Name: "egg", + User: "root", + Psw: "Fnuo123com@", + ShowLog: true, + MaxLifetime: 30, + MaxOpenConns: 100, + MaxIdleConns: 100, + Path: "tmp/%s.log", + } + db.InitDB(&dbcfg) + cache.NewRedis("127.0.0.1:6379") + + AutoCachingTop100(db.Db) +} diff --git a/app/task/svc/svc_egg_energy_virtual_coin_flow.go b/app/task/svc/svc_egg_energy_virtual_coin_flow.go new file mode 100644 index 0000000..f815161 --- /dev/null +++ b/app/task/svc/svc_egg_energy_virtual_coin_flow.go @@ -0,0 +1,66 @@ +package svc + +import ( + "applet/app/db" + "applet/app/utils/cache" + "fmt" + "time" + "xorm.io/xorm" +) + +func EggEnergyCoinFlow(engine *xorm.Engine) { + fmt.Println(">>>>>>>>>>>>>>>>>>EggEnergyCoinFlow-----<<<<<<<<<<<<<<<<<<<") + var now = time.Now() + year, month, day := now.Date() + today := time.Date(year, month, day, 0, 0, 0, 0, time.Local) + thisWeek := today.AddDate(0, 0, -int(today.Weekday())+1).Format("2006-01-02") + thisMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02") + + //判断今天是否清空 + getString1, _ := cache.GetString("user_virtual_coin_flow_aggregation_today_data") + if getString1 != today.Format("2006-01-02") { + //go func() { + sql := "UPDATE `user_virtual_coin_flow_aggregation` SET today_data= '0.00'" + _, err := db.ExecuteOriginalSql(engine.Where(""), sql) + if err != nil { + fmt.Println("err:>>>>>>>>", err.Error()) + return + } + fmt.Println("success:update:day>>>>>>>>") + cache.Set("user_virtual_coin_flow_aggregation_today_data", today.Format("2006-01-02")) + //}() + } + + //判断本周是否清空 + getString2, _ := cache.GetString("user_virtual_coin_flow_aggregation_this_week_data") + if getString2 != today.Format("2006-01-02") && thisWeek == today.Format("2006-01-02") { + //go func() { + sql := "UPDATE `user_virtual_coin_flow_aggregation` SET this_week_data= '0.00'" + _, err := db.ExecuteOriginalSql(engine.Where(""), sql) + if err != nil { + fmt.Println("err:>>>>>>>>", err.Error()) + return + } + fmt.Println("success:update:week>>>>>>>>") + cache.Set("user_virtual_coin_flow_aggregation_this_week_data", today.Format("2006-01-02")) + //}() + } + + //判断本月是否清空 + getString3, _ := cache.GetString("user_virtual_coin_flow_aggregation_this_month_data") + if getString3 != today.Format("2006-01-02") && thisMonth == today.Format("2006-01-02") { + //go func() { + sql := "UPDATE `user_virtual_coin_flow_aggregation` SET this_month_data= '0.00'" + _, err := db.ExecuteOriginalSql(engine.Where(""), sql) + if err != nil { + fmt.Println("err:>>>>>>>>", err.Error()) + return + } + fmt.Println("success:update:month>>>>>>>>") + cache.Set("user_virtual_coin_flow_aggregation_this_month_data", today.Format("2006-01-02")) + //}() + } + + fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>EggEnergyCoinFlow----end<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") + return +} diff --git a/app/task/task_egg_energy_virtual_coin_flow.go b/app/task/task_egg_energy_virtual_coin_flow.go new file mode 100644 index 0000000..20ab539 --- /dev/null +++ b/app/task/task_egg_energy_virtual_coin_flow.go @@ -0,0 +1,22 @@ +package task + +import ( + "applet/app/task/svc" + "math/rand" + "time" + "xorm.io/xorm" +) + +func taskEggEnergyCoinFlow(eg *xorm.Engine) { + for { + if len(ch) > workerNum { + time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) + } else { + goto START + } + } +START: + ch <- 1 + svc.EggEnergyCoinFlow(eg) + <-ch +}