INTEGRITY Cloudflare Docs

Cache

Background

The Cache API allows fine grained control of reading and writing from the Cloudflare global network cache.

The Cache API is available globally but the contents of the cache do not replicate outside of the originating data center. A GET /users response can be cached in the originating data center, but will not exist in another data center unless it has been explicitly created.

Workers deployed to custom domains have access to functional cache operations. So do Pages functions, whether attached to custom domains or *.pages.dev domains.

However, any Cache API operations in the Cloudflare Workers dashboard editor and Playground previews will have no impact. For Workers fronted by Cloudflare Access, the Cache API is not currently available.


Accessing Cache

The caches.default API is strongly influenced by the web browsers’ Cache API, but there are some important differences. For instance, Cloudflare Workers runtime exposes a single global cache object.

let cache = caches.default;
await cache.match(request);

You may create and manage additional Cache instances via the caches.open method.

let myCache = await caches.open('custom:cache');
await myCache.match(request);

Headers

Our implementation of the Cache API respects the following HTTP headers on the response passed to put():

This differs from the web browser Cache API as they do not honor any headers on the request or response.


Methods

Put

cache.put(request, response);

Parameters

Invalid parameters

cache.put will throw an error if:

Errors

cache.put returns a 413 error if Cache-Control instructs not to cache or if the response is too large.

Match

cache.match(request, options);

Parameters

Unlike the browser Cache API, Cloudflare Workers do not support the ignoreSearch or ignoreVary options on match(). You can accomplish this behavior by removing query strings or HTTP headers at put() time.

Our implementation of the Cache API respects the following HTTP headers on the request passed to match():

Errors

cache.match generates a 504 error response when the requested content is missing or expired. The Cache API does not expose this 504 directly to the Worker script, instead returning undefined. Nevertheless, the underlying 504 is still visible in Cloudflare Logs.

If you use Cloudflare Logs, you may see these 504 responses with the RequestSource of edgeWorkerCacheAPI. Again, these are expected if the cached asset was missing or expired. Note that edgeWorkerCacheAPI requests are already filtered out in other views, such as Cache Analytics. To filter out these requests or to filter requests by end users of your website only, refer to Filter end users.

Delete

cache.delete(request, options);

Deletes the Response object from the cache and returns a Promise for a Boolean response:

Parameters