Browse Source

feat: add caching top100 and auto update user_virtual_coin_flow_aggregation

master
shenjiachi 1 day ago
parent
commit
2aee2a0414
6 changed files with 194 additions and 0 deletions
  1. +1
    -0
      app/task/init.go
  2. +1
    -0
      app/task/md/cron_key.go
  3. +78
    -0
      app/task/svc/svc_auto_caching_top100.go
  4. +26
    -0
      app/task/svc/svc_auto_caching_top100_test.go
  5. +66
    -0
      app/task/svc/svc_egg_energy_virtual_coin_flow.go
  6. +22
    -0
      app/task/task_egg_energy_virtual_coin_flow.go

+ 1
- 0
app/task/init.go View File

@@ -100,4 +100,5 @@ func initTasks() {
jobs[taskMd.CornEggEnergyAutoCachingEggPointStatistics] = taskAutoCachingEggPointStatistics // 蛋蛋分统计落地页-缓存
jobs[taskMd.CornEggEnergyAutoUpdateUserAccess] = taskEggEnergyAutoUpdateUserAccess // es蛋蛋分记录-自动更新访问次数
jobs[taskMd.CornEggEnergyAutoDeleteTableAdvertisingCallback] = taskAutoDeleteTableAdvertisingCallback // 定时清除七天以前的广告回调数据
jobs[taskMd.CornEggEnergyCoinFlow] = taskEggEnergyCoinFlow // 定时重置用户流水聚合信息
}

+ 1
- 0
app/task/md/cron_key.go View File

@@ -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" // 定时重置用户流水聚合信息
)

+ 78
- 0
app/task/svc/svc_auto_caching_top100.go View File

@@ -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-------------------------------")
}

}

+ 26
- 0
app/task/svc/svc_auto_caching_top100_test.go View File

@@ -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)
}

+ 66
- 0
app/task/svc/svc_egg_energy_virtual_coin_flow.go View File

@@ -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
}

+ 22
- 0
app/task/task_egg_energy_virtual_coin_flow.go View File

@@ -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
}

Loading…
Cancel
Save