Overview
The Cache API is an implementation of the MSDN Cache Interface to provide a persistent storage mechanism for Request / Response object pairs that are cached in long lived memory. The Cache API is available globally but the contents of the cache do not replicate outside of the originating region. A GET/users response can be cached in the originating region, but will not exist in another region until a request for the resources is made from there.
An origin can have multiple, named Cache objects. You are responsible for implementing how your script handles Cache updates. Items in a Cache respect the Cache-Control request header, it is expected that you will use this to control the life cycle of your caches. Cache entries will automatically be purged a short time after they expire. Make sure to version caches by name and use the caches only from the version of the script that they can safely operate on.
The cache instances are shared across all domains associated with your PullZone, however they must be accessed via the currently requesting host name. For this reason we either recommend using the current Request object as they key or constructing the key using the current request url e.g. const key = new URL(req.url).origin + "/img/example.png";. This will ensure your caches work reliably across all domains associated with your PullZone.
Note: There is a hard limit of
100MB per cache file.Limitations
Currently the Edge Scripting runners happen after the HTTP cache layer. If you want to have Edge Scripting being able to run before Cache, we are currently working on the feature and testing it at scale. 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.
Quickstart
A minimal cache-aside pattern: look up by URL, generate on miss, write back in the background.References
- MSDN CacheStorage - Mozilla’s CacheStorage interface documentation
- MSDN Cache API - Mozilla’s Cache interface documentation