Cache
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.
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);
Our implementation of the Cache API respects the following HTTP headers on the response passed to put()
:
Cache-Control
- Controls caching directives. This is consistent with Cloudflare Cache-Control Directives. Refer to Edge TTL for a list of HTTP response codes and their TTL when
Cache-Control
directives are not present.
- Controls caching directives. This is consistent with Cloudflare Cache-Control Directives. Refer to Edge TTL for a list of HTTP response codes and their TTL when
Cache-Tag
- Allows resource purging by tag(s) later.
ETag
- Allows
cache.match()
to evaluate conditional requests withIf-None-Match
.
- Allows
Expires
string- A string that specifies when the resource becomes invalid.
Last-Modified
- Allows
cache.match()
to evaluate conditional requests withIf-Modified-Since
.
- Allows
This differs from the web browser Cache API as they do not honor any headers on the request or response.
cache.put(request, response);
-
put(request, response)
: Promise- Attempts to add a response to the cache, using the given request as the key. Returns a promise that resolves to
undefined
regardless of whether the cache successfully stored the response.
- Attempts to add a response to the cache, using the given request as the key. Returns a promise that resolves to
-
request
string | Request- The string or
Request
object used as the lookup key. Strings are interpreted as the URL for a newRequest
object.
- The string or
-
options
object- Can contain one possible property:
ignoreMethod
(Boolean). Consider the request method a GET regardless of its actual value.
- Can contain one possible property:
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Products
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark