feat: add frame caching demo documentation

This commit is contained in:
zhangheng
2026-05-19 16:19:55 +08:00
commit 3ffeb1ae62
18 changed files with 4002 additions and 0 deletions

45
web/lib/video-cache.ts Normal file
View File

@@ -0,0 +1,45 @@
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.
}
}