From 02b3e275d0c73e18e4fd6dc4e5b1386d737db10a Mon Sep 17 00:00:00 2001 From: ayflying Date: Tue, 2 Dec 2025 10:39:53 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=9A=82=E6=97=B6=E5=B1=8F=E8=94=BD?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E7=9A=84=E5=88=A0=E9=99=A4=EF=BC=8C=E6=9D=A5?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=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/logic/gameAct/gameAct.go | 40 ++++++++++++++++--------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/internal/logic/gameAct/gameAct.go b/internal/logic/gameAct/gameAct.go index da3759d..20706a7 100644 --- a/internal/logic/gameAct/gameAct.go +++ b/internal/logic/gameAct/gameAct.go @@ -237,7 +237,7 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { } //删除缓存 - go s.DelCacheKey(ctx, v.ActId, v.Uid) + //s.DelCacheKey(ctx, v.ActId, v.Uid) updateCount++ update = make([]*entity.GameAct, 0) @@ -265,10 +265,10 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { return } - for _, v2 := range add { - //删除缓存 - go s.DelCacheKey(ctx, v2.ActId, v2.Uid) - } + //for _, v2 := range add { + // //删除缓存 + // s.DelCacheKey(ctx, v2.ActId, v2.Uid) + //} //g.Log().Debugf(ctx, "当前 %v 写入数据库: %v 条", actId, count) add = make([]*entity.GameAct, 0) @@ -458,7 +458,7 @@ func (s *sGameAct) Cache2Sql(ctx context.Context, add, update []*entity.GameAct) tx, err = g.DB().Begin(ctx) } //删除缓存 - s.DelCacheKey(ctx, v.ActId, v.Uid) + //s.DelCacheKey(ctx, v.ActId, v.Uid) } //循环结束了,最后写入一波 g.Log().Debugf(ctx, "act当前更新数据库: %v 条", updateCount) @@ -489,7 +489,7 @@ func (s *sGameAct) Cache2Sql(ctx context.Context, add, update []*entity.GameAct) tx, err = g.DB().Begin(ctx) } //删除缓存 - s.DelCacheKey(ctx, v.ActId, v.Uid) + //s.DelCacheKey(ctx, v.ActId, v.Uid) } //循环结束了,最后写入一波 @@ -556,7 +556,7 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan * } //删除缓存 - s.DelCacheKey(ctx, v.ActId, v.Uid) + //s.DelCacheKey(ctx, v.ActId, v.Uid) case v, ok := <-updateChan: if !ok { @@ -590,7 +590,7 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan * } //删除缓存 - s.DelCacheKey(ctx, v.ActId, v.Uid) + //s.DelCacheKey(ctx, v.ActId, v.Uid) case <-ctx.Done(): g.Log().Debug(ctx, "act协程被上下文取消") @@ -607,17 +607,19 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan * // 删除缓存key func (s *sGameAct) DelCacheKey(ctx context.Context, aid int, uid int64) { - //如果有活跃,跳过删除 - if getBool, _ := pkg.Cache("redis"). - Contains(ctx, fmt.Sprintf("act:update:%d", uid)); getBool { - return - } + go func() { + //如果有活跃,跳过删除 + if getBool, _ := pkg.Cache("redis"). + Contains(ctx, fmt.Sprintf("act:update:%d", uid)); getBool { + return + } - cacheKey := fmt.Sprintf("act:%v:%v", aid, uid) - _, err := g.Redis().Del(ctx, cacheKey) - if err != nil { - g.Log().Error(ctx, err) - } + cacheKey := fmt.Sprintf("act:%v:%v", aid, uid) + _, err := g.Redis().Del(ctx, cacheKey) + if err != nil { + g.Log().Error(ctx, err) + } + }() } // 清空GetRedDot缓存 From 4fd262beae564dece2711f9cd8754d013135a224 Mon Sep 17 00:00:00 2001 From: ayflying Date: Tue, 2 Dec 2025 12:21:15 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E4=B8=8E=E5=86=99=E5=85=A5=E7=9A=84=E6=8C=81=E8=AE=A1=E5=88=92?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/logic/gameAct/gameAct.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/internal/logic/gameAct/gameAct.go b/internal/logic/gameAct/gameAct.go index 20706a7..581b62f 100644 --- a/internal/logic/gameAct/gameAct.go +++ b/internal/logic/gameAct/gameAct.go @@ -237,7 +237,7 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { } //删除缓存 - //s.DelCacheKey(ctx, v.ActId, v.Uid) + s.DelCacheKey(ctx, v.ActId, v.Uid) updateCount++ update = make([]*entity.GameAct, 0) @@ -458,10 +458,10 @@ func (s *sGameAct) Cache2Sql(ctx context.Context, add, update []*entity.GameAct) tx, err = g.DB().Begin(ctx) } //删除缓存 - //s.DelCacheKey(ctx, v.ActId, v.Uid) + s.DelCacheKey(ctx, v.ActId, v.Uid) } //循环结束了,最后写入一波 - g.Log().Debugf(ctx, "act当前更新数据库: %v 条", updateCount) + g.Log().Debugf(ctx, "Cache2Sql运行结束,act当前更新数据库: %v 条", updateCount) update = (update)[:0] } @@ -479,7 +479,7 @@ func (s *sGameAct) Cache2Sql(ctx context.Context, add, update []*entity.GameAct) } addCount++ if addCount > TaskMax { - g.Log().Debugf(ctx, "act当前写入数据库: %v 条", addCount) + g.Log().Debugf(ctx, "超过%v条,act当前写入数据库: %v 条", TaskMax, addCount) err = tx.Commit() if err != nil { g.Log().Debugf(ctx, "act当前写入数据库失败:%v", err) @@ -489,11 +489,11 @@ func (s *sGameAct) Cache2Sql(ctx context.Context, add, update []*entity.GameAct) tx, err = g.DB().Begin(ctx) } //删除缓存 - //s.DelCacheKey(ctx, v.ActId, v.Uid) + s.DelCacheKey(ctx, v.ActId, v.Uid) } //循环结束了,最后写入一波 - g.Log().Debugf(ctx, "act当前写入数据库: %v 条", addCount) + g.Log().Debugf(ctx, "Cache2Sql运行结束,act当前写入数据库: %v 条", addCount) add = (add)[:0] } @@ -545,7 +545,7 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan * addCount += row if addCount > TaskMax { - g.Log().Debugf(ctx, "act当前写入数据库: %v 条", addCount) + g.Log().Debugf(ctx, "超过%v条,act当前写入数据库: %v 条", TaskMax, addCount) err = tx.Commit() if err != nil { g.Log().Debugf(ctx, "act当前写入数据库失败:%v", err) @@ -556,7 +556,7 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan * } //删除缓存 - //s.DelCacheKey(ctx, v.ActId, v.Uid) + s.DelCacheKey(ctx, v.ActId, v.Uid) case v, ok := <-updateChan: if !ok { @@ -579,10 +579,10 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan * updateCount++ if updateCount > TaskMax { - g.Log().Debugf(ctx, "act当前写入数据库: %v 条", addCount) + g.Log().Debugf(ctx, "超过%v条,act当前更新数据库: %v 条", TaskMax, updateCount) err = tx.Commit() if err != nil { - g.Log().Debugf(ctx, "act当前写入数据库失败:%v", err) + g.Log().Debugf(ctx, "act当前更新数据库失败:%v", err) return } updateCount = 0 @@ -590,7 +590,7 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan * } //删除缓存 - //s.DelCacheKey(ctx, v.ActId, v.Uid) + s.DelCacheKey(ctx, v.ActId, v.Uid) case <-ctx.Done(): g.Log().Debug(ctx, "act协程被上下文取消") @@ -600,8 +600,8 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan * err = tx.Commit() // 仅在所有通道处理完毕后打印最终计数(移除中间冗余日志) - g.Log().Debugf(ctx, "act当前写入数据库: %v 条", addCount) - g.Log().Debugf(ctx, "act当前更新数据库: %v 条", updateCount) + g.Log().Debugf(ctx, "运行结束act当前写入数据库: %v 条", addCount) + g.Log().Debugf(ctx, "运行结束act当前更新数据库: %v 条", updateCount) return } From 01c97c37f7c928f63f112e0d4408511d932daed4 Mon Sep 17 00:00:00 2001 From: liaoyulong Date: Wed, 3 Dec 2025 16:38:38 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B3=A8=E9=87=8Aact=E6=8C=81=E4=B9=85?= =?UTF-8?q?=E5=8C=96=E7=9A=84=E5=86=97=E4=BD=99=E6=89=93=E5=8D=B0=20?= =?UTF-8?q?=E6=9C=80=E5=90=8E=E6=89=A7=E8=A1=8C=E7=BB=93=E6=9D=9F=E5=90=8E?= =?UTF-8?q?=E6=89=93=E5=8D=B0=E6=9C=80=E7=BB=88=E8=AE=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/logic/gameAct/gameAct.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/internal/logic/gameAct/gameAct.go b/internal/logic/gameAct/gameAct.go index 581b62f..6faef75 100644 --- a/internal/logic/gameAct/gameAct.go +++ b/internal/logic/gameAct/gameAct.go @@ -510,6 +510,8 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan * //通道关闭标志 addClosed := false updateClosed := false + //写入总数 + var addAllCount, updateAllCount int64 tx, err := g.DB().Begin(ctx) if err != nil { @@ -545,12 +547,14 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan * addCount += row if addCount > TaskMax { - g.Log().Debugf(ctx, "超过%v条,act当前写入数据库: %v 条", TaskMax, addCount) + // g.Log().Debugf(ctx, "超过%v条,act当前写入数据库: %v 条", TaskMax, addCount) err = tx.Commit() if err != nil { g.Log().Debugf(ctx, "act当前写入数据库失败:%v", err) return } + //清空数量前累加一下 + addAllCount += addCount addCount = 0 tx, err = g.DB().Begin(ctx) } @@ -579,12 +583,14 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan * updateCount++ if updateCount > TaskMax { - g.Log().Debugf(ctx, "超过%v条,act当前更新数据库: %v 条", TaskMax, updateCount) + // g.Log().Debugf(ctx, "超过%v条,act当前更新数据库: %v 条", TaskMax, updateCount) err = tx.Commit() if err != nil { g.Log().Debugf(ctx, "act当前更新数据库失败:%v", err) return } + //清空数量前累加一下 + updateAllCount += updateCount updateCount = 0 tx, err = g.DB().Begin(ctx) } @@ -600,8 +606,8 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan * err = tx.Commit() // 仅在所有通道处理完毕后打印最终计数(移除中间冗余日志) - g.Log().Debugf(ctx, "运行结束act当前写入数据库: %v 条", addCount) - g.Log().Debugf(ctx, "运行结束act当前更新数据库: %v 条", updateCount) + g.Log().Debugf(ctx, "运行结束act当前写入数据库: %v 条", addAllCount) + g.Log().Debugf(ctx, "运行结束act当前更新数据库: %v 条", updateAllCount) return }