The Cache API is designed based on the standard Web API Cache. During the runtime of edge functions, a global caches
object that provides a group of methods for cache-related operations is injected.
Note:The cached content takes effect only on the current data node and is not automatically copied to other data nodes.
caches.default
to fetch the default cache instance.// Fetch the default cache instance.
const cache = caches.default;
// This method provides the same effect as `caches.default`.
await caches.open('default');
caches.open
to create a cache instance with the specified namespace.// Create a cache instance with the specified namespace.
const cache = await caches.open(namespace);
The following table describes the parameters of the caches.open(namespace)
method.
Parameter | Type | Required | Description |
---|---|---|---|
namespace | string | Yes |
The namespace of the cache.
caches.default to fetch a default instance. |
cache.match(request: string | Request, options?: MatchOptions): Promise<Response | undefined>
The match() method fetches the Response cache associated with the request and returns a Promise object. If the cache exists, the object contains the Response object. If not, the object contains undefined.
Note:The cache.match() method does not forward requests to the origin. If the cache expires, the method throws a 504 error.
Parameter | Type | Required | Description |
---|---|---|---|
request | string | Request | Yes |
The request object. The following method and headers are supported: Only the
GET method is supported. If this parameter is set to a string, the string is used as a URL to construct a Request object.If the request contains a
Range header and the cached response contains the Accept-Ranges header, the 206 response is returned.If the request contains a If-Modified-Since header and the cached response contains a Last-Modified header with the same value as that of the If-Modified-Since header, the 304 response is returned.
If the request contains a If-None-Match header and the cached response contains an ETag header with the same value as that of the If-Modified-Since header, the 304 response is returned.
|
options | MatchOptions | No | The options. |
Parameter | Type | Example | Description |
---|---|---|---|
ignoreMethod | boolean | true | Specifies whether to ignore the request method. If you set this parameter to true, the request is considered to be a GET request regardless of its actual method. |
cache.put(request: string | Request, response: Response): Promise<undefined>
The put() method tries to add a response to the cache by using the given request as the cache key. A Promise
Note:The put() method returns a 413 error if the Cache-Control header of the object specified in the response parameter instructs not to cache.
Parameter | Type | Required | Description |
---|---|---|---|
request | string | Request | Yes |
The cache key.
The
request parameter supports only the GET method. If other methods are specified, an error is thrown.
If the
request parameter is set to a string, the string is used as a URL to construct a Request object.
|
response | Response | Yes |
The cache content.
Valid values: s-maxage, max-age, no-store, no-cache, and private. The values no-store, no-cache, and private indicate no caching. If you use these values, the
cache.put method returns a 413 error.
If Cache-Control is not specified and Pragma is set to no-cache, no caching is performed.
If the
request parameter of the cache.match method contains a If-None-Match header, you can associate it with ETag.
If the
request parameter of the cache.match method contains a If-Modified-Since header, you can associate it with Last-Modified.
If the object specified in the
response parameter is 416 Range Not Satisfiable, no caching is performed.
|
The cache.put
method throws an error in the following scenarios:
request
parameter specifies a method other than the GET method.response
parameter is 206 Partial Content. response
parameter contains a Vary: * header. cache.delete(request: string | Request, options?: DeleteOptions): Promise<boolean>
The delete() method deletes the response associated with the request from the cache. If no network exception occurs, a Promise containing true
is returned. Otherwise, a Promise containing false
is returned.
Parameter | Type | Required | Description |
---|---|---|---|
request | string | Request | Yes |
The cache key.
The
request parameter supports only the GET method.
If the
request parameter is set to a string, the string is used as a URL to construct a Request object.
|
options | DeleteOptions | No | The options. |
Parameter | Type | Example | Description |
---|---|---|---|
ignoreMethod | boolean | true | Specifies whether to ignore the request method. If you set this parameter to true, the request is considered to be a GET request regardless of its actual method. |
Was this page helpful?