Files
video-process/web/lib/video-cache.ts
2026-05-19 16:19:55 +08:00

46 lines
1.1 KiB
TypeScript

const CACHE_NAME = "video-process-h264-v1";
export async function openCachedVideoResponse(
url: string,
signal: AbortSignal,
): Promise<{ response: Response; cacheState: "hit" | "miss" }> {
if (typeof caches !== "undefined") {
const cache = await caches.open(CACHE_NAME);
const cached = await cache.match(url);
if (cached) {
return { response: cached, cacheState: "hit" };
}
}
const response = await fetch(url, {
signal,
cache: "no-store",
});
if (!response.ok) {
throw new Error(`请求失败:${response.status} ${response.statusText}`);
}
if (typeof caches !== "undefined") {
const cache = await caches.open(CACHE_NAME);
cache.put(url, response.clone()).catch(() => {
// Cache Storage is best-effort.
});
}
return { response, cacheState: "miss" };
}
export async function clearCachedVideoResponse(url: string): Promise<void> {
if (typeof caches === "undefined") {
return;
}
try {
const cache = await caches.open(CACHE_NAME);
await cache.delete(url);
} catch {
// Cache Storage is best-effort.
}
}