feat(redis): optimize TTL display with human-readable format (#1689)

This commit is contained in:
lexmin0412 authored and GitHub committed 2026-06-24 12:53:59 +08:00
1 parent c411cc6707
commit 941dfd5f82
10 files changed
+198 -1

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
/**
* Formats Redis TTL (seconds) as a compact string:
* < 1 day → HH:mm:ss
* >= 1 day → {n}d HH:mm:ss (day unit localized)
*
* Returns null for ttl === -1 (no expiry) or ttl <= 0.
*/
type TranslateFn = (key: string, options?: Record<string, unknown>) => string;
export function formatTtl(ttl: number, t: TranslateFn): string | null {
if (ttl === -1) return null;
if (ttl <= 0) return null;
const days = Math.floor(ttl / 86_400);
const hours = Math.floor((ttl % 86_400) / 3_600);
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(":");
if (days > 0) {
return `${t("redis.ttlDay", { count: days })} ${time}`;
}
return time;
}