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)