From 115331338476778fd09e1f8ee1f539824e2fd655 Mon Sep 17 00:00:00 2001 From: ayflying Date: Thu, 24 Apr 2025 17:44:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AE=A1=E5=88=92=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E7=9A=84=E6=8C=81=E4=B9=85=E5=8C=96=EF=BC=8Cact?= =?UTF-8?q?=E4=B8=8Ekv=E7=9A=84=E7=BC=93=E5=AD=98=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E9=A2=84=E9=98=B2=E5=A4=9A=E6=9C=8D=E5=8A=A1=E5=99=A8?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E6=8C=81=E4=B9=85=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/boot/boot.go | 4 +++- internal/logic/gameAct/gameAct.go | 8 ++++++++ internal/logic/gameKv/gameKv.go | 23 ++++++++++++++++++----- utility.go | 1 + 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/internal/boot/boot.go b/internal/boot/boot.go index 70ede1c..5509e92 100644 --- a/internal/boot/boot.go +++ b/internal/boot/boot.go @@ -17,7 +17,9 @@ func Boot() (err error) { //用户活动持久化 service.SystemCron().AddCronV2(v1.CronType_DAILY, func(ctx context.Context) error { - return service.GameAct().Saves(ctx) + err = service.GameKv().SavesV1() + err = service.GameAct().Saves(ctx) + return err }) //初始化自启动方法 diff --git a/internal/logic/gameAct/gameAct.go b/internal/logic/gameAct/gameAct.go index 6272ddb..d7c35b5 100644 --- a/internal/logic/gameAct/gameAct.go +++ b/internal/logic/gameAct/gameAct.go @@ -107,6 +107,14 @@ func (s *sGameAct) Set(uid int64, actId int, data interface{}) (err error) { } func (s *sGameAct) Saves(ctx context.Context) (err error) { + getCache, _ := pkg.Cache("redis").Get(nil, "cron:game_act") + //如果没有执行过,设置时间戳 + if getCache.Int64() > 0 { + return + } else { + pkg.Cache("redis").Set(nil, "cron:game_act", gtime.Now().Unix(), time.Hour) + } + //遍历执行 ActList.Iterator(func(i interface{}) bool { err = s.Save(ctx, i.(int)) diff --git a/internal/logic/gameKv/gameKv.go b/internal/logic/gameKv/gameKv.go index be9c4eb..f544df8 100644 --- a/internal/logic/gameKv/gameKv.go +++ b/internal/logic/gameKv/gameKv.go @@ -1,13 +1,17 @@ package gameKv import ( + "fmt" + "github.com/ayflying/utility_go/pkg" "github.com/ayflying/utility_go/service" "github.com/ayflying/utility_go/tools" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" + "github.com/gogf/gf/v2/os/gtime" "strconv" "strings" "sync" + "time" ) var ( @@ -25,10 +29,6 @@ func New() *sGameKv { func init() { service.RegisterGameKv(New()) - - //支付钩子 - //task.Task.Trigger(tasks.TaskType_PAY, service.GameKv().HookPay) - //task.Task.Trigger(tasks.TaskType_WARDROBE_LEVEL, service.GameKv().HookLevelRwd) } // SavesV1 方法 @@ -37,6 +37,14 @@ func init() { // @receiver s: sGameKv的实例。 // @return err: 错误信息,如果操作成功,则为nil。 func (s *sGameKv) SavesV1() (err error) { + getCache, err := pkg.Cache("redis").Get(nil, "cron:game_kv") + //如果没有执行过,设置时间戳 + if getCache.Int64() > 0 { + return + } else { + pkg.Cache("redis").Set(nil, "cron:game_kv", gtime.Now().Unix(), time.Hour) + } + // 从Redis列表中获取所有用户KV索引的键 //keys, err := utils.RedisScan("user:kv:*") err = tools.Redis.RedisScanV2("user:kv:*", func(keys []string) (err error) { @@ -69,12 +77,17 @@ func (s *sGameKv) SavesV1() (err error) { //if user.UpdatedAt.Seconds < gtime.Now().Add(consts.ActSaveTime).Unix() { // continue //} + //如果有活跃,跳过持久化 + if getBool, _ := pkg.Cache("redis"). + Contains(ctx, fmt.Sprintf("act:update:%d", uid)); getBool { + continue + } get, _ := g.Redis().Get(ctx, cacheKey) var data interface{} get.Scan(&data) list = append(list, &ListData{ - Uid: int64(uid), + Uid: uid, Kv: data, }) diff --git a/utility.go b/utility.go index ef1cbfc..29ebfa2 100644 --- a/utility.go +++ b/utility.go @@ -17,6 +17,7 @@ func init() { g.Log().Debug(ctx, "utility_go init启动完成") // 初始化配置 var err = boot.Boot() + if err != nil { panic(err) }