From 28fb7989268eed52b835ee4622d0594a914cfdfb Mon Sep 17 00:00:00 2001 From: eafonyang Date: Fri, 12 Jun 2026 17:30:18 +0800 Subject: [PATCH] =?UTF-8?q?fix(cache):=20share:mac=20ZSET=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=201h=20=E5=85=9C=E5=BA=95=20TTL=EF=BC=8C=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E6=B0=B8=E4=B8=8D=E8=B0=83=E7=94=A8=20shareList=20?= =?UTF-8?q?=E6=97=B6=20key=20=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 shareMacIndexTTL = 1h 常量 - Lua 脚本中原子执行 EXPIRE KEYS[3] - 每次创建分享码续期 ZSET,活跃用户不受影响 - 无人调用 shareList 时 ZSET 1h 后自动过期清理 --- internal/cache/share_code_cache.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/cache/share_code_cache.go b/internal/cache/share_code_cache.go index 27bad8b..bfcde88 100644 --- a/internal/cache/share_code_cache.go +++ b/internal/cache/share_code_cache.go @@ -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)