如果检测到当前正在活跃,不删除缓存key
This commit is contained in:
@@ -498,6 +498,12 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context) {
|
|||||||
|
|
||||||
// 删除缓存key
|
// 删除缓存key
|
||||||
func (s *sGameAct) DelCacheKey(ctx context.Context, aid int, uid int64) {
|
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)
|
cacheKey := fmt.Sprintf("act:%v:%v", aid, uid)
|
||||||
_, err := g.Redis().Del(ctx, cacheKey)
|
_, err := g.Redis().Del(ctx, cacheKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -15,6 +14,7 @@ import (
|
|||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/os/gctx"
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
"github.com/gogf/gf/v2/os/gtime"
|
"github.com/gogf/gf/v2/os/gtime"
|
||||||
|
"github.com/gogf/gf/v2/util/gconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -72,19 +72,17 @@ func (s *sGameKv) SavesV1() (err error) {
|
|||||||
//uid := v.Int64()
|
//uid := v.Int64()
|
||||||
//cacheKey = "user:kv:" + strconv.FormatInt(uid, 10)
|
//cacheKey = "user:kv:" + strconv.FormatInt(uid, 10)
|
||||||
result := strings.Split(cacheKey, ":")
|
result := strings.Split(cacheKey, ":")
|
||||||
var uid int64
|
var uid = gconv.Int64(result[2])
|
||||||
uid, err = strconv.ParseInt(result[2], 10, 64)
|
if uid == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
//uid, err = strconv.ParseInt(result[2], 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
g.Log().Error(ctx, err)
|
g.Log().Error(ctx, err)
|
||||||
g.Redis().Del(ctx, cacheKey)
|
g.Redis().Del(ctx, cacheKey)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
////如果1天没有活跃,跳过
|
|
||||||
//user, _ := service.MemberUser().Info(uid)
|
|
||||||
//if user.UpdatedAt.Seconds < gtime.Now().Add(consts.ActSaveTime).Unix() {
|
|
||||||
// continue
|
|
||||||
//}
|
|
||||||
//如果有活跃,跳过持久化
|
//如果有活跃,跳过持久化
|
||||||
if getBool, _ := pkg.Cache("redis").
|
if getBool, _ := pkg.Cache("redis").
|
||||||
Contains(ctx, fmt.Sprintf("act:update:%d", uid)); getBool {
|
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)
|
get, _ := g.Redis().Get(ctx, cacheKey)
|
||||||
var data interface{}
|
var data interface{}
|
||||||
get.Scan(&data)
|
get.Scan(&data)
|
||||||
|
if data == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
list = append(list, &ListData{
|
list = append(list, &ListData{
|
||||||
Uid: uid,
|
Uid: uid,
|
||||||
Kv: data,
|
Kv: data,
|
||||||
@@ -103,21 +104,17 @@ func (s *sGameKv) SavesV1() (err error) {
|
|||||||
// 将列表数据保存到数据库
|
// 将列表数据保存到数据库
|
||||||
if len(list) > 100 {
|
if len(list) > 100 {
|
||||||
_, err2 := g.Model("game_kv").Data(list).Save()
|
_, err2 := g.Model("game_kv").Data(list).Save()
|
||||||
|
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
g.Log().Error(ctx, err2)
|
g.Log().Error(ctx, "当前kv数据入库失败: %v", err2)
|
||||||
|
err = err2
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//删除当前key
|
//删除当前key
|
||||||
for _, v := range list {
|
for _, v := range list {
|
||||||
go s.DelCacheKey(ctx, v.Uid)
|
s.DelCacheKey(ctx, v.Uid)
|
||||||
}
|
}
|
||||||
list = make([]*ListData, 0)
|
list = make([]*ListData, 0)
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
g.Log().Error(ctx, "当前kv数据入库失败: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
return
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -126,6 +123,12 @@ func (s *sGameKv) SavesV1() (err error) {
|
|||||||
|
|
||||||
// 删除缓存key
|
// 删除缓存key
|
||||||
func (s *sGameKv) DelCacheKey(ctx context.Context, uid int64) {
|
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)
|
cacheKey := fmt.Sprintf("user:kv:%v", uid)
|
||||||
_, err := g.Redis().Del(ctx, cacheKey)
|
_, err := g.Redis().Del(ctx, cacheKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user