feat: add frame caching demo documentation
This commit is contained in:
45
web/lib/video-cache.ts
Normal file
45
web/lib/video-cache.ts
Normal 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.
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user