Skip to content

cache

Source: src/Cloudflare/Workers/Cache.ts

Enable Workers Cache on the surrounding Worker and get the runtime cache client.

Yielding cache() in an Effect-native Worker’s init phase turns the cache on at deploy time (the equivalent of setting cache: { enabled: true } on the Worker’s props) and returns a client whose purge drives the runtime ctx.cache API from your handlers.

What gets cached is controlled by standard response headers — Cache-Control (including stale-while-revalidate), Cache-Tag for tag-based purging, and Vary for content negotiation.

For async (non-Effect) Workers, set the cache prop on the Worker instead.

Effect.gen(function* () {
// init: enable Workers Cache on this Worker
const { purge } = yield* Cloudflare.cache();
return {
fetch: Effect.gen(function* () {
const request = yield* HttpServerRequest;
if (request.url.startsWith("/invalidate")) {
yield* purge({ tags: ["products"] });
return HttpServerResponse.text("purged");
}
return HttpServerResponse.text("hello", {
headers: {
"Cache-Control": "public, max-age=300",
"Cache-Tag": "products",
},
});
}),
};
})