add article

This commit is contained in:
2025-09-01 18:29:59 +08:00
parent 34ff6ce916
commit 5d5372e359
59 changed files with 45435 additions and 1535 deletions

View File

@@ -171,9 +171,9 @@ func (r *RedisCache) GetMultiple(ctx context.Context, keys []string) (map[string
var data interface{}
// 修复改进JSON反序列化错误处理
if err := json.Unmarshal([]byte(val.(string)), &data); err != nil {
r.logger.Warn("反序列化缓存数据失败",
zap.String("key", keys[i]),
zap.String("value", val.(string)),
r.logger.Warn("反序列化缓存数据失败",
zap.String("key", keys[i]),
zap.String("value", val.(string)),
zap.Error(err))
continue
}
@@ -227,17 +227,18 @@ func (r *RedisCache) DeletePattern(ctx context.Context, pattern string) error {
} else {
fullPattern = r.getFullKey(pattern)
}
// 检查上下文是否已取消
if ctx.Err() != nil {
return ctx.Err()
}
var cursor uint64
var totalDeleted int64
maxIterations := 100 // 防止无限循环
iteration := 0
for {
// 检查迭代次数限制
iteration++
@@ -249,7 +250,7 @@ func (r *RedisCache) DeletePattern(ctx context.Context, pattern string) error {
)
break
}
// 检查上下文是否已取消
if ctx.Err() != nil {
r.logger.Warn("缓存删除操作被取消",
@@ -259,7 +260,7 @@ func (r *RedisCache) DeletePattern(ctx context.Context, pattern string) error {
)
return ctx.Err()
}
// 执行SCAN操作
keys, next, err := r.client.Scan(ctx, cursor, fullPattern, 1000).Result()
if err != nil {
@@ -272,27 +273,27 @@ func (r *RedisCache) DeletePattern(ctx context.Context, pattern string) error {
)
return err
}
r.logger.Error("扫描缓存键失败",
zap.String("pattern", fullPattern),
r.logger.Error("扫描缓存键失败",
zap.String("pattern", fullPattern),
zap.Error(err))
return err
}
// 批量删除找到的键
if len(keys) > 0 {
// 使用pipeline批量删除提高性能
pipe := r.client.Pipeline()
pipe.Del(ctx, keys...)
cmds, err := pipe.Exec(ctx)
if err != nil {
r.logger.Error("批量删除缓存键失败",
zap.Strings("keys", keys),
r.logger.Error("批量删除缓存键失败",
zap.Strings("keys", keys),
zap.Error(err))
return err
}
// 统计删除的键数量
for _, cmd := range cmds {
if delCmd, ok := cmd.(*redis.IntCmd); ok {
@@ -301,26 +302,26 @@ func (r *RedisCache) DeletePattern(ctx context.Context, pattern string) error {
}
}
}
r.logger.Debug("批量删除缓存键",
r.logger.Debug("批量删除缓存键",
zap.Strings("keys", keys),
zap.Int("batch_size", len(keys)),
zap.Int64("total_deleted", totalDeleted),
)
}
cursor = next
if cursor == 0 {
break
}
}
r.logger.Debug("缓存模式删除完成",
zap.String("pattern", fullPattern),
zap.Int64("total_deleted", totalDeleted),
zap.Int("iterations", iteration),
)
return nil
}