按照执行中的状态进行删除

This commit is contained in:
ayflying
2025-09-01 18:32:02 +08:00
parent 50cfc23ad2
commit ce8ae4d26a

View File

@@ -143,6 +143,9 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) {
// keys = keys[:10000] // 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) { 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执行超时了,停止执行!") err = errors.New("act执行超时了,停止执行!")
return return
} }
var add = make([]*entity.GameAct, 0)
var update = make([]*entity.GameAct, 0)
var delKey []string
for _, cacheKey = range keys { for _, cacheKey = range keys {
result := strings.Split(cacheKey, ":") result := strings.Split(cacheKey, ":")
actId, err = strconv.Atoi(result[1]) actId, err = strconv.Atoi(result[1])
@@ -206,13 +207,12 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) {
data.Action = actionData data.Action = actionData
update = append(update, data) update = append(update, data)
} }
//最后删除key
delKey = append(delKey, cacheKey)
} }
//批量写入数据库 //批量写入数据库
updateCount := 0 updateCount := 0
if len(delKey) > 200 {
if len(update) > 100 {
for _, v := range update { for _, v := range update {
v.UpdatedAt = gtime.Now() v.UpdatedAt = gtime.Now()
updateRes, err2 := g.Model(Name).Where(do.GameAct{ updateRes, err2 := g.Model(Name).Where(do.GameAct{
@@ -227,16 +227,22 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) {
g.Log().Error(ctx, "本次更新为0更新数据失败: %v", v) g.Log().Error(ctx, "本次更新为0更新数据失败: %v", v)
return return
} }
//删除缓存
go s.DelCacheKey(v.ActId, v.Uid)
updateCount++ updateCount++
update = make([]*entity.GameAct, 0)
} }
g.Log().Debugf(ctx, "当前 %v 更新数据库: %v 条", actId, updateCount) g.Log().Debugf(ctx, "当前 %v 更新数据库: %v 条", actId, updateCount)
update = make([]*entity.GameAct, 0) update = make([]*entity.GameAct, 0)
}
var count int64 var count int64
if len(add) > 0 { if len(add) > 100 {
dbRes, err2 := g.Model(Name).Data(add).Save() dbRes, err2 := g.Model(Name).Data(add).Save()
add = make([]*entity.GameAct, 0)
err = err2 err = err2
if err != nil { if err != nil {
g.Log().Error(ctx, err2) g.Log().Error(ctx, err2)
@@ -251,17 +257,14 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) {
return return
} }
for _, v2 := range add {
//删除缓存
go s.DelCacheKey(v2.ActId, v2.Uid)
}
//g.Log().Debugf(ctx, "当前 %v 写入数据库: %v 条", actId, count) //g.Log().Debugf(ctx, "当前 %v 写入数据库: %v 条", actId, count)
} add = make([]*entity.GameAct, 0)
for _, v := range delKey {
_, err = g.Redis().Del(ctx, v)
if err != nil {
g.Log().Error(ctx, err)
}
}
delKey = make([]string, 0)
} }
if err != nil { if err != nil {
@@ -274,6 +277,15 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) {
return 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缓存 // 清空GetRedDot缓存
func (s *sGameAct) RefreshGetRedDotCache(uid int64) { func (s *sGameAct) RefreshGetRedDotCache(uid int64) {
cacheKey := fmt.Sprintf("gameAct:GetRedDot:%s:%d", gtime.Now().Format("d"), uid) cacheKey := fmt.Sprintf("gameAct:GetRedDot:%s:%d", gtime.Now().Format("d"), uid)