From ce8ae4d26ac873e71e02069f3471c4807809d2ee Mon Sep 17 00:00:00 2001 From: ayflying Date: Mon, 1 Sep 2025 18:32:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=89=E7=85=A7=E6=89=A7=E8=A1=8C=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E7=8A=B6=E6=80=81=E8=BF=9B=E8=A1=8C=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/logic/gameAct/gameAct.go | 72 ++++++++++++++++++------------- 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/internal/logic/gameAct/gameAct.go b/internal/logic/gameAct/gameAct.go index e72d384..a7b4e18 100644 --- a/internal/logic/gameAct/gameAct.go +++ b/internal/logic/gameAct/gameAct.go @@ -143,6 +143,9 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { // keys = keys[:10000] //} + var add = make([]*entity.GameAct, 0) + var update = make([]*entity.GameAct, 0) + //循环获取缓存数据 err = tools.Redis.RedisScanV2(cacheKey, func(keys []string) (err error) { //判断是否超时 @@ -151,9 +154,7 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { err = errors.New("act执行超时了,停止执行!") return } - var add = make([]*entity.GameAct, 0) - var update = make([]*entity.GameAct, 0) - var delKey []string + for _, cacheKey = range keys { result := strings.Split(cacheKey, ":") actId, err = strconv.Atoi(result[1]) @@ -206,13 +207,12 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { data.Action = actionData update = append(update, data) } - //最后删除key - delKey = append(delKey, cacheKey) } //批量写入数据库 updateCount := 0 - if len(delKey) > 200 { + + if len(update) > 100 { for _, v := range update { v.UpdatedAt = gtime.Now() updateRes, err2 := g.Model(Name).Where(do.GameAct{ @@ -227,41 +227,44 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { g.Log().Error(ctx, "本次更新为0,更新数据失败: %v", v) return } + + //删除缓存 + go s.DelCacheKey(v.ActId, v.Uid) + updateCount++ + update = make([]*entity.GameAct, 0) } g.Log().Debugf(ctx, "当前 %v 更新数据库: %v 条", actId, updateCount) - update = make([]*entity.GameAct, 0) - var count int64 + } - if len(add) > 0 { - dbRes, err2 := g.Model(Name).Data(add).Save() - add = make([]*entity.GameAct, 0) - err = err2 - if err != nil { - g.Log().Error(ctx, err2) - return - } - count, _ = dbRes.RowsAffected() - if count == 0 { - g.Log().Error(ctx, "当前 %v 写入数据库: %v 条", actId, count) - for _, vTemp := range add { - g.Log().Debugf(ctx, "当前act:%v,add写入数据: %v,内容:%v", vTemp.ActId, vTemp.Uid, vTemp.Action) - } - return + var count int64 + if len(add) > 100 { + dbRes, err2 := g.Model(Name).Data(add).Save() + + err = err2 + if err != nil { + g.Log().Error(ctx, err2) + return + } + count, _ = dbRes.RowsAffected() + if count == 0 { + g.Log().Error(ctx, "当前 %v 写入数据库: %v 条", actId, count) + for _, vTemp := range add { + g.Log().Debugf(ctx, "当前act:%v,add写入数据: %v,内容:%v", vTemp.ActId, vTemp.Uid, vTemp.Action) } - //g.Log().Debugf(ctx, "当前 %v 写入数据库: %v 条", actId, count) + return + } - for _, v := range delKey { - _, err = g.Redis().Del(ctx, v) - if err != nil { - g.Log().Error(ctx, err) - } + for _, v2 := range add { + //删除缓存 + go s.DelCacheKey(v2.ActId, v2.Uid) } - delKey = make([]string, 0) + //g.Log().Debugf(ctx, "当前 %v 写入数据库: %v 条", actId, count) + add = make([]*entity.GameAct, 0) } if err != nil { @@ -274,6 +277,15 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { return } +// 删除缓存key +func (s *sGameAct) DelCacheKey(aid int, uid int64) { + cacheKey := fmt.Sprintf("act:%v:%v", aid, uid) + _, err := g.Redis().Del(ctx, cacheKey) + if err != nil { + g.Log().Error(ctx, err) + } +} + // 清空GetRedDot缓存 func (s *sGameAct) RefreshGetRedDotCache(uid int64) { cacheKey := fmt.Sprintf("gameAct:GetRedDot:%s:%d", gtime.Now().Format("d"), uid)