INNER CODE UNIT · TypeScript
cachedOrCompute
harvard-edge/cs249r_book · interviews/staffml-vault-worker/src/index.ts:199
async function cachedOrCompute(
req: Request,
env: Env,
ctx: ExecutionContext,
ttlSec: number,
compute: () => Promise<Response>,
opts: { useCache: boolean } = { useCache: true },
): Promise<Response> {
// Graceful degradation when the CF Cache API isn't available (Node test
// env, vault-api local shim, or a runtime regression) — always compute
// and skip caching. Prevents crashing the handler on missing globals.
if (!opts.useCache || typeof caches === "undefined" || !caches?.default) {
return compute();
}
const manifest = await getManifest(env);
const key = cacheKey(req, manifest.release_id);
const cached = await caches.default.match(key);
if (cached) {