feat(redis): add TTL metadata support

This commit is contained in:
dadong authored and GitHub committed 2026-08-20 01:51:11 +08:00
1 parent 680358aff8
commit 39ce50b681
15 files changed
+551 -140

No files matched your search

+17 -8
View File
@@ -1,7 +1,9 @@
/**
* Formats Redis TTL (seconds) as a compact string:
* < 1 day → HH:mm:ss
* >= 1 day → {n}d HH:mm:ss (day unit localized)
* Formats Redis TTL (seconds) as a compact human-readable remaining time:
* 从大到小取前两个非零单位,例如:
* 45 → 45s / 45秒
* 3661 → 1h 1m / 1小时 1分钟
* 93784 → 1d 2h / 1天 2小时
*
* Returns null for ttl === -1 (no expiry) or ttl <= 0.
*/
@@ -17,10 +19,17 @@ export function formatTtl(ttl: number, t: TranslateFn): string | null {
const minutes = Math.floor((ttl % 3_600) / 60);
const seconds = ttl % 60;
const time = [String(hours).padStart(2, "0"), String(minutes).padStart(2, "0"), String(seconds).padStart(2, "0")].join(":");
// 按从大到小排列四个单位,只保留前两个非零单位,避免展示过长
const units: Array<[number, string]> = [
[days, "redis.ttlDay"],
[hours, "redis.ttlHour"],
[minutes, "redis.ttlMinute"],
[seconds, "redis.ttlSecond"],
];
if (days > 0) {
return `${t("redis.ttlDay", { count: days })} ${time}`;
}
return time;
return units
.filter(([value]) => value > 0)
.slice(0, 2)
.map(([value, key]) => t(key, { count: value }))
.join(" ");
}