Skip to content

Cache API

caches.defaultcaches.open()put / match / deletecaches.default 与该 Worker 的默认 HTTP response cache 共享逻辑存储。

ts
export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
    const cached = await caches.default.match(request);
    if (cached) return cached;
    const response = new Response("hello", {
      headers: { "Cache-Control": "public, s-maxage=60" },
    });
    ctx.waitUntil(caches.default.put(request, response.clone()));
    return response;
  },
} satisfies ExportedHandler<Env>;

部署侧 cache.enabledWorkers Cache

兼容性

主题Cloudflareopen-compute
caches.default / caches.open / put / match / delete是,见 Cache API
条件请求、Vary、Range按 pinned workerd 与本机 cache
缓存范围全球 / colo CDN单节点
自动缓存 TTL可含启发式 TTL需要显式 s-maxagemax-age;无启发式 TTL
全球 purge / Cache Tags不提供