From 62b0e429b3d03fd3430bf9fc741d2f0a729aab0b Mon Sep 17 00:00:00 2001 From: ayflying Date: Wed, 3 Sep 2025 10:02:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=A3=80=E6=B5=8B=E5=88=B0?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E6=AD=A3=E5=9C=A8=E6=B4=BB=E8=B7=83=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E5=88=A0=E9=99=A4=E7=BC=93=E5=AD=98key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/logic/gameAct/gameAct.go | 6 ++++++ internal/logic/gameKv/gameKv.go | 33 +++++++++++++++++-------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/internal/logic/gameAct/gameAct.go b/internal/logic/gameAct/gameAct.go index 02cdc62..83242db 100644 --- a/internal/logic/gameAct/gameAct.go +++ b/internal/logic/gameAct/gameAct.go @@ -498,6 +498,12 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context) { // 删除缓存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 + } + cacheKey := fmt.Sprintf("act:%v:%v", aid, uid) _, err := g.Redis().Del(ctx, cacheKey) if err != nil { diff --git a/internal/logic/gameKv/gameKv.go b/internal/logic/gameKv/gameKv.go index 0011487..d1943a9 100644 --- a/internal/logic/gameKv/gameKv.go +++ b/internal/logic/gameKv/gameKv.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "strconv" "strings" "sync" "time" @@ -15,6 +14,7 @@ import ( "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gtime" + "github.com/gogf/gf/v2/util/gconv" ) var ( @@ -72,19 +72,17 @@ func (s *sGameKv) SavesV1() (err error) { //uid := v.Int64() //cacheKey = "user:kv:" + strconv.FormatInt(uid, 10) result := strings.Split(cacheKey, ":") - var uid int64 - uid, err = strconv.ParseInt(result[2], 10, 64) + var uid = gconv.Int64(result[2]) + if uid == 0 { + continue + } + //uid, err = strconv.ParseInt(result[2], 10, 64) if err != nil { g.Log().Error(ctx, err) g.Redis().Del(ctx, cacheKey) continue } - ////如果1天没有活跃,跳过 - //user, _ := service.MemberUser().Info(uid) - //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 { @@ -94,6 +92,9 @@ func (s *sGameKv) SavesV1() (err error) { get, _ := g.Redis().Get(ctx, cacheKey) var data interface{} get.Scan(&data) + if data == nil { + continue + } list = append(list, &ListData{ Uid: uid, Kv: data, @@ -103,21 +104,17 @@ func (s *sGameKv) SavesV1() (err error) { // 将列表数据保存到数据库 if len(list) > 100 { _, err2 := g.Model("game_kv").Data(list).Save() - if err2 != nil { - g.Log().Error(ctx, err2) + g.Log().Error(ctx, "当前kv数据入库失败: %v", err2) + err = err2 return } //删除当前key for _, v := range list { - go s.DelCacheKey(ctx, v.Uid) + s.DelCacheKey(ctx, v.Uid) } list = make([]*ListData, 0) } - if err != nil { - g.Log().Error(ctx, "当前kv数据入库失败: %v", err) - } - return }) @@ -126,6 +123,12 @@ func (s *sGameKv) SavesV1() (err error) { // 删除缓存key func (s *sGameKv) DelCacheKey(ctx context.Context, uid int64) { + //如果有活跃,跳过删除 + if getBool, _ := pkg.Cache("redis"). + Contains(ctx, fmt.Sprintf("act:update:%d", uid)); getBool { + return + } + cacheKey := fmt.Sprintf("user:kv:%v", uid) _, err := g.Redis().Del(ctx, cacheKey) if err != nil {