perf(console): Gitea 等leo清單讀取加 CF Cache 層(TTL 90s,#36 審查①)

- cachedGiteaWaiting:caches.default + Cache-Control max-age=90;前端 60s
  刷新下 Gitea 從每分鐘 3-4 call 降到 ≤1 輪/90s
- hit 回放用 reviveWaitingAges 隨牆鐘補算「維護於 N 分鐘前」(不停走)
- 失敗不快取(不把網路抖動放大成 90 秒盲區);cache.put 走 waitUntil 不阻塞
- waiting_meta.cache 吐 hit/miss,部署後 curl 兩次即得快取生效證據
- 測試:tests/console-dashboard-cache.test.ts 4 案例(真 workerd Cache API,
  注入 fetcher 計數證明 hit 不再打 Gitea);全套 69/70(1 fail=main 既有)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015d5jDbuqT5Htwv3Q88XXKk
This commit is contained in:
2026-07-07 11:19:19 +00:00
parent 99da35a885
commit 0d9a6e8eed
3 changed files with 193 additions and 3 deletions
@@ -257,3 +257,26 @@ export function humanAge(minutes: number): string {
if (minutes < 48 * 60) return `${Math.round(minutes / 60)} 小時前`;
return `${Math.round(minutes / (24 * 60))} 天前`;
}
// ── Gitea 讀取快取(總管 #36 審查要求:查詢面加快取——別讓前端 60 秒刷新變成
// 每分鐘 3-4 個 API call 打自家 Gitea;快取是讀,不違「不輪詢」紅線)──────────
/** 快取 TTL(秒)。90s 前端 60s 刷新下,Gitea 實際被打 ≤1 輪/90s
* 等leo清單由 progress-guard 每日維護,90 秒新鮮度綽綽有餘。 */
export const GITEA_WAITING_CACHE_TTL_SECONDS = 90;
/** Cache API 裡存的封套:模型 + fetch 當下的牆鐘(回放時補算 age 用)。 */
export interface CachedWaitingEnvelope {
model: WaitingModel;
fetched_at_ms: number;
}
/** 快取回放時把「維護於 N 分鐘前」隨牆鐘推進:存的是 fetch 當下算好的 ago,
* 直接回放會讓時間停走(最多差一個 TTL,雖小仍是假時間);補回經過的分鐘數、
* stale 判定同步重算,才符合「不擺 stale 殘骸」的原則。 */
export function reviveWaitingAges(model: WaitingModel, fetchedAtMs: number, nowMs: number): WaitingModel {
if (model.updated_ago_minutes < 0) return model;
const drift = Math.max(0, Math.round((nowMs - fetchedAtMs) / 60000));
const ago = model.updated_ago_minutes + drift;
return { ...model, updated_ago_minutes: ago, stale: ago > 48 * 60 };
}