From aa1dc0896dfe93a0579bd1e43f2c8997865db8a4 Mon Sep 17 00:00:00 2001 From: ayflying Date: Tue, 2 Sep 2025 12:25:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AE=A1=E5=88=92=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=89=93=E6=96=AD=E9=80=A0=E6=88=90=E5=8D=8F=E7=A8=8B?= =?UTF-8?q?=E5=86=85=E4=B8=8A=E4=B8=8B=E6=96=87=E6=89=A7=E8=A1=8C=E5=81=9C?= =?UTF-8?q?=E6=AD=A2=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/boot/boot.go | 8 ++++---- internal/logic/gameAct/gameAct.go | 31 +++++++++++++++---------------- internal/logic/gameKv/gameKv.go | 8 ++++---- service/game_act.go | 4 +++- service/game_kv.go | 4 +++- tools/redis.go | 2 -- 6 files changed, 29 insertions(+), 28 deletions(-) diff --git a/internal/boot/boot.go b/internal/boot/boot.go index 25a4c32..db64015 100644 --- a/internal/boot/boot.go +++ b/internal/boot/boot.go @@ -19,12 +19,12 @@ func Boot() (err error) { //err = service.SystemCron().StartCron() //用户活动持久化每小时执行一次 - service.SystemCron().AddCronV2(v1.CronType_HOUR, func(ctx context.Context) error { + service.SystemCron().AddCronV2(v1.CronType_HOUR, func(context.Context) error { go func() { - err = service.GameKv().SavesV1(ctx) - err = service.GameAct().Saves(ctx) + err = service.GameKv().SavesV1() + err = service.GameAct().Saves() if err != nil { - g.Log().Error(ctx, err) + g.Log().Error(gctx.New(), err) } }() return nil diff --git a/internal/logic/gameAct/gameAct.go b/internal/logic/gameAct/gameAct.go index 3d7b2db..1b59956 100644 --- a/internal/logic/gameAct/gameAct.go +++ b/internal/logic/gameAct/gameAct.go @@ -21,7 +21,6 @@ import ( ) var ( - ctx = gctx.New() Name = "game_act" ActList = gset.New(true) RunTimeMax *gtime.Time @@ -47,6 +46,7 @@ func init() { // @return data *v1.Act: 返回活动信息结构体指针 // @return err error: 返回错误信息 func (s *sGameAct) Info(uid int64, actId int) (data *g.Var, err error) { + var ctx = gctx.New() if uid == 0 || actId == 0 { g.Log().Error(ctx, "当前参数为空") return @@ -89,6 +89,7 @@ func (s *sGameAct) Info(uid int64, actId int) (data *g.Var, err error) { // @param data interface{}: 要存储的活动信息数据。 // @return err error: 返回错误信息,如果操作成功,则返回nil。 func (s *sGameAct) Set(uid int64, actId int, data interface{}) (err error) { + var ctx = gctx.New() if uid == 0 || actId == 0 { g.Log().Error(ctx, "当前参数为空") return @@ -109,7 +110,8 @@ func (s *sGameAct) Set(uid int64, actId int, data interface{}) (err error) { return } -func (s *sGameAct) Saves(ctx context.Context) (err error) { +func (s *sGameAct) Saves() (err error) { + var ctx = gctx.New() g.Log().Debug(ctx, "开始执行游戏act数据保存了") //如果没有执行过,设置时间戳 // 最大允许执行时间 @@ -118,10 +120,10 @@ func (s *sGameAct) Saves(ctx context.Context) (err error) { ActList.Iterator(func(i interface{}) bool { //在时间内允许执行 if gtime.Now().Before(RunTimeMax) { - g.Log().Debug(ctx, "开始执行游戏act数据保存: act%v", i) + g.Log().Debugf(ctx, "开始执行游戏act数据保存:act=%v", i) err = s.Save(ctx, i.(int)) } else { - g.Log().Errorf(ctx, "游戏act数据保存超时: act=%v", i) + g.Log().Errorf(ctx, "游戏act数据保存超时:act=%v", i) } return true }) @@ -131,12 +133,6 @@ func (s *sGameAct) Saves(ctx context.Context) (err error) { func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { cacheKey := fmt.Sprintf("act:%v:*", actId) - //获取当前用户的key值 - //keys, err := utils.RedisScan(cacheKey) - //if len(keys) > 10000 { - // keys = keys[:10000] - //} - var add = make([]*entity.GameAct, 0) var update = make([]*entity.GameAct, 0) @@ -156,6 +152,7 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { uid = gconv.Int64(result[2]) //uid, err = strconv.ParseInt(result[2], 10, 64) if err != nil { + g.Log().Error(ctx, err) continue } @@ -184,7 +181,7 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { ActId: actId, }).Fields("uid,act_id").Scan(&data) if err != nil { - g.Log().Debugf(ctx, "当前数据错误: %v", cacheKey) + g.Log().Errorf(ctx, "当前数据错误: %v", cacheKey) continue } actionData := cacheGet.String() @@ -206,6 +203,7 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { //批量写入数据库 updateCount := 0 + g.Log().Debugf(ctx, "当前 %v 要更新的数据: %v 条", actId, len(update)) if len(update) > 100 { for _, v := range update { v.UpdatedAt = gtime.Now() @@ -223,7 +221,7 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { } //删除缓存 - go s.DelCacheKey(v.ActId, v.Uid) + go s.DelCacheKey(ctx, v.ActId, v.Uid) updateCount++ update = make([]*entity.GameAct, 0) @@ -233,7 +231,7 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { } var count int64 - + g.Log().Debugf(ctx, "当前 %v 要添加的数据: %v 条", actId, len(add)) if len(add) > 100 { dbRes, err2 := g.Model(Name).Data(add).Save() @@ -253,7 +251,7 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { for _, v2 := range add { //删除缓存 - go s.DelCacheKey(v2.ActId, v2.Uid) + go s.DelCacheKey(ctx, v2.ActId, v2.Uid) } //g.Log().Debugf(ctx, "当前 %v 写入数据库: %v 条", actId, count) @@ -271,7 +269,7 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { } // 删除缓存key -func (s *sGameAct) DelCacheKey(aid int, uid int64) { +func (s *sGameAct) DelCacheKey(ctx context.Context, aid int, uid int64) { cacheKey := fmt.Sprintf("act:%v:%v", aid, uid) _, err := g.Redis().Del(ctx, cacheKey) if err != nil { @@ -284,12 +282,13 @@ func (s *sGameAct) RefreshGetRedDotCache(uid int64) { cacheKey := fmt.Sprintf("gameAct:GetRedDot:%s:%d", gtime.Now().Format("d"), uid) _, err := pkg.Cache("redis").Remove(gctx.New(), cacheKey) if err != nil { - g.Log().Error(ctx, err) + g.Log().Error(gctx.New(), err) g.Dump(err) } } func (s *sGameAct) Del(uid int64, actId int) { + var ctx = gctx.New() if uid == 0 || actId == 0 { g.Log().Error(ctx, "当前参数为空") return diff --git a/internal/logic/gameKv/gameKv.go b/internal/logic/gameKv/gameKv.go index 64be563..0011487 100644 --- a/internal/logic/gameKv/gameKv.go +++ b/internal/logic/gameKv/gameKv.go @@ -18,7 +18,6 @@ import ( ) var ( - ctx = gctx.New() Name = "game_kv" RunTimeMax *gtime.Time ) @@ -40,7 +39,8 @@ func init() { // @Description: 保存用户KV数据列表。 // @receiver s: sGameKv的实例。 // @return err: 错误信息,如果操作成功,则为nil。 -func (s *sGameKv) SavesV1(ctx context.Context) (err error) { +func (s *sGameKv) SavesV1() (err error) { + var ctx = gctx.New() // 最大允许执行时间 RunTimeMax = gtime.Now().Add(time.Minute * 30) g.Log().Debug(ctx, "开始执行游戏kv数据保存") @@ -110,7 +110,7 @@ func (s *sGameKv) SavesV1(ctx context.Context) (err error) { } //删除当前key for _, v := range list { - go s.DelCacheKey(v.Uid) + go s.DelCacheKey(ctx, v.Uid) } list = make([]*ListData, 0) } @@ -125,7 +125,7 @@ func (s *sGameKv) SavesV1(ctx context.Context) (err error) { } // 删除缓存key -func (s *sGameKv) DelCacheKey(uid int64) { +func (s *sGameKv) DelCacheKey(ctx context.Context, uid int64) { cacheKey := fmt.Sprintf("user:kv:%v", uid) _, err := g.Redis().Del(ctx, cacheKey) if err != nil { diff --git a/service/game_act.go b/service/game_act.go index ec787c6..1544d47 100644 --- a/service/game_act.go +++ b/service/game_act.go @@ -31,8 +31,10 @@ type ( // @param data interface{}: 要存储的活动信息数据。 // @return err error: 返回错误信息,如果操作成功,则返回nil。 Set(uid int64, actId int, data interface{}) (err error) - Saves(ctx context.Context) (err error) + Saves() (err error) Save(ctx context.Context, actId int) (err error) + // 删除缓存key + DelCacheKey(ctx context.Context, aid int, uid int64) // 清空GetRedDot缓存 RefreshGetRedDotCache(uid int64) Del(uid int64, actId int) diff --git a/service/game_kv.go b/service/game_kv.go index cac5a78..45b8bc0 100644 --- a/service/game_kv.go +++ b/service/game_kv.go @@ -16,7 +16,9 @@ type ( // @Description: 保存用户KV数据列表。 // @receiver s: sGameKv的实例。 // @return err: 错误信息,如果操作成功,则为nil。 - SavesV1(ctx context.Context) (err error) + SavesV1() (err error) + // 删除缓存key + DelCacheKey(ctx context.Context, uid int64) } ) diff --git a/tools/redis.go b/tools/redis.go index d048fca..f533273 100644 --- a/tools/redis.go +++ b/tools/redis.go @@ -48,7 +48,6 @@ func (r *redis) RedisScan(cacheKey string, _key ...string) (keys []string, err e // redis 批量获取大量数据 func (r *redis) RedisScanV2(cacheKey string, _func func([]string) error, _key ...string) error { - //var keys []string var err error @@ -67,7 +66,6 @@ func (r *redis) RedisScanV2(cacheKey string, _func func([]string) error, _key .. g.Log().Errorf(ctx, "Scan failed: %v", err) break } - if len(newKeys) > 0 { err = _func(newKeys) if err != nil {