fix(cache): share:mac ZSET 添加 1h 兜底 TTL,防止永不调用 shareList 时 key 泄漏
- 新增 shareMacIndexTTL = 1h 常量 - Lua 脚本中原子执行 EXPIRE KEYS[3] - 每次创建分享码续期 ZSET,活跃用户不受影响 - 无人调用 shareList 时 ZSET 1h 后自动过期清理
This commit is contained in:
Vendored
+4
-2
@@ -9,7 +9,7 @@
|
||||
// expire_at 过期时间 (RFC3339)
|
||||
// persisted 是否已刷入 DB ("0"/"1")
|
||||
//
|
||||
// share:mac:{mac} ZSET — MAC 二级索引 (无 TTL, 查询时自动清理过期成员)
|
||||
// share:mac:{mac} ZSET, TTL 1h (兜底) — MAC 二级索引 (查询时主动清理过期成员)
|
||||
// member = share_code
|
||||
// score = expire_at unix timestamp
|
||||
//
|
||||
@@ -52,6 +52,7 @@ const (
|
||||
shareMacIndexPrefix = "share:mac:"
|
||||
shareCodeLength = 5
|
||||
shareCodeTTL = 30 * time.Minute
|
||||
shareMacIndexTTL = 1 * time.Hour
|
||||
shareImportPendingTTL = 12 * time.Hour
|
||||
shareCodeCharset = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"
|
||||
shareCodeMaxRetries = 20
|
||||
@@ -78,6 +79,7 @@ redis.call('EXPIRE', KEYS[1], tonumber(ARGV[5]))
|
||||
redis.call('SADD', KEYS[2], ARGV[6])
|
||||
-- ZSET: score = expire_at unix timestamp, member = share code
|
||||
redis.call('ZADD', KEYS[3], tonumber(ARGV[7]), ARGV[6])
|
||||
redis.call('EXPIRE', KEYS[3], tonumber(ARGV[8]))
|
||||
return 1
|
||||
`)
|
||||
|
||||
@@ -123,7 +125,7 @@ func (c *ShareCodeCache) Create(ctx context.Context, macAddr, ipAddr string, eqD
|
||||
key := shareCodeKey(code)
|
||||
macIdxKey := shareMacIndexKey(macAddr)
|
||||
ok, err := shareCreateScript.Run(ctx, c.rdb, []string{key, sharePendingSet, macIdxKey},
|
||||
macAddr, ipAddr, eqJSON, expireAt.Format(time.RFC3339), int(shareCodeTTL.Seconds()), code, expireAt.Unix(),
|
||||
macAddr, ipAddr, eqJSON, expireAt.Format(time.RFC3339), int(shareCodeTTL.Seconds()), code, expireAt.Unix(), int(shareMacIndexTTL.Seconds()),
|
||||
).Int()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create share code in redis: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user