perf: Sprint 19 P0/P1 性能优化落地

P0(高优先级):
- P0-1: 确认数据库复合索引已存在(GORM tag),composite_index_test 验证通过
- P0-2: 连接池调优 MaxIdleConns 5→10, ConnMaxLifetime 30min→5min
- P0-3: Redis 智能探测(ProbeRedis),无 Redis 自动降级到纯内存模式

P1(中优先级):
- P1-1: GZIP 压缩中间件(compress/gzip 标准库,零新依赖)
- P1-2: 权限缓存 TTL 30min→5min
- P1-3: Argon2id 启动自适应校准(CalibrateArgon2id)

历史优化(含本次提交):
- L1Cache O(n)→O(1) LRU 重构
- Auth 中间件 DB 查询合并 + 5s L1 缓存
- Logger 异步化(4096 缓冲通道)

验证: go build/vet/test 41/41 PASS, govulncheck 无漏洞
This commit is contained in:
2026-04-18 22:57:44 +08:00
parent 85285c16d1
commit 7b047e2f11
11 changed files with 1231 additions and 154 deletions

27
internal/cache/l2.go vendored
View File

@@ -4,12 +4,39 @@ import (
"context"
"encoding/json"
"errors"
"log"
"strings"
"time"
redis "github.com/redis/go-redis/v9"
)
// ProbeRedis 探测 Redis 是否可达。
//
// 使用 2 秒超时发起 PING成功返回 true任何错误连接拒绝、超时、DNS 解析失败)
// 均返回 false 并打印 warn 日志,调用方可据此决定是否启用 Redis。
//
// 此函数是幂等的,可在启动阶段安全调用多次。
func ProbeRedis(addr, password string, db int) bool {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
client := redis.NewClient(&redis.Options{
Addr: addr,
Password: password,
DB: db,
DialTimeout: 2 * time.Second,
})
defer client.Close()
if err := client.Ping(ctx).Err(); err != nil {
log.Printf("redis probe: unreachable at %s — falling back to in-memory only (%v)", addr, err)
return false
}
log.Printf("redis probe: reachable at %s — Redis L2 cache will be enabled", addr)
return true
}
// L2Cache defines the distributed cache contract.
type L2Cache interface {
Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error