/users response cached in one region will not exist in another until a request for that resource is made from there.
An origin can have multiple, named Cache objects, and it is up to your script to decide how cache updates happen. Items in a Cache respect the Cache-Control header on the response you pass to put(), which is how you control the life cycle of your caches. Entries are purged automatically a short time after they expire. Version your caches by name, and only use a cache from a version of the script that can safely operate on it.
Cache instances are shared across all domains associated with your PullZone, but they must be accessed via the currently requesting host name. Use the current Request object as the key, or build the key from the current request URL, for example const key = new URL(req.url).origin + "/img/example.png";. Either way your caches will work reliably across every domain on the PullZone.
Note: There is a hard limit of
100MB per cache file.Limitations
API surface limits:CacheStorage(the globalcachesobject)- Supported:
caches.default,caches.open(name) - Not supported:
caches.has,caches.delete,caches.keys,caches.match
- Supported:
Cache(an instance returned bycaches.defaultorcaches.open)- Supported:
match,put,delete - Not supported:
matchAll,add,addAll,keys
- Supported:
cache:v1, cache:v2) so that a cache is completely purged when updating scripts.
Pull Zone cache settings
The Cache API is not independent of the Pull Zone. The Cache expiration time setting of the connected Pull Zone also applies to entries your script writes. If Cache expiration time is set to Override: do not cache,cache.put() resolves without an error, but it stores no entry. A later cache.match() for the same key returns undefined.
To cache entries in your script, but not the responses it returns:
- In the Pull Zone, click Caching and set Cache expiration time to Respect origin Cache-Control.
- Set a
Cache-Controlheader with a TTL, such asmax-age=60, on each response you pass tocache.put(). - Set
Cache-Control: no-cacheon the responses your script returns to the client.
Quickstart
A minimal cache-aside pattern: look up by URL, generate on miss, write back in the background.Troubleshooting
cache.put() succeeds, but cache.match() returns undefined
cache.put() succeeds, but cache.match() returns undefined
- Check the Cache expiration time setting of the connected Pull Zone. If it is Override: do not cache, set it to Respect origin Cache-Control. See Pull Zone cache settings.
- Make sure the response you store has a
Cache-Controlheader with a TTL, such asmax-age=60. - Use the same key for
put()andmatch(). Build the key from the hostname of the current request, and normalize the method toGET. - Test the write and the read in the same request, and
awaittheput(). Cache contents do not replicate between regions, so two separate requests can reach different regions.
MISS, the problem is in the Pull Zone configuration, not in your script.References
- MSDN CacheStorage - Mozilla’s CacheStorage interface documentation
- MSDN Cache API - Mozilla’s Cache interface documentation