Edge Developer Platform
  • Edge Functions
    • Overview
    • Getting Started
    • Operation Guide
      • Function Management
      • Function Trigger
    • Runtime APIs
      • addEventListener
      • Cache
      • Cookies
      • Encoding
      • Fetch
      • FetchEvent
      • Headers
      • Request
      • Response
      • Streams
        • ReadableStream
        • ReadableStreamBYOBReader
        • ReadableStreamDefaultReader
        • TransformStream
        • WritableStream
        • WritableStreamDefaultWriter
      • Web Crypto
      • Web standards
      • Images
        • ImageProperties
    • Sample Functions
      • Returning an HTML Page
      • Returning a JSON Object
      • Fetch Remote Resources
      • Authenticating a Request Header
      • Modifying a Response Header
      • Performing an A/B Test
      • Setting Cookies
      • Performing Redirect Based on the Request Location
      • Using the Cache API
      • Caching POST Requests
      • Responding in Streaming Mode
      • Merging Resources and Responding in Streaming Mode
      • Protecting Data from Tampering
      • Rewriting a m3u8 File and Configuring Authentication
      • Adaptive Image Resize
      • Image Adaptive WebP
      • Customize Referer restriction rules
      • Remote Authentication
      • HMAC Digital Signature
      • Naming a Downloaded File
      • Obtaining Client IP Address
    • Best Practices
      • Adaptive Image Format Conversion via Edge Functions

Cache

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.

Constructor API

Use 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');
Use 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);

Parameters

The following table describes the parameters of the caches.open(namespace) method.
Parameter
Type
Required
Description
namespace
string
Yes
The namespace of the cache.
The value "default" specifies the default instance. You can also use caches.default to fetch a default instance.

Methods

match

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.

Parameters

Parameter
Type
Required
Description
request
string | Request
Yes
The request object. The following method and headers are supported:
GET
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.
Range
If the request contains a Range header and the cached response contains the Accept-Ranges header, the 206 response is returned.
If-Modified-Since
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-None-Match
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
No
The options.

MatchOptions

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.

put

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<undefined> object is returned regardless of whether caching is successful.
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.

Parameters

Parameter
Type
Required
Description
request
string | Request
Yes
The cache key.
GET
The request parameter supports only the GET method. If other methods are specified, an error is thrown.
string
If the request parameter is set to a string, the string is used as a URL to construct a Request object.
response
Yes
The cache content.
Cache-Control
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.
Pragma
If Cache-Control is not specified and Pragma is set to no-cache, no caching is performed.
ETag
If the request parameter of the cache.match method contains a If-None-Match header, you can associate it with ETag.
Last-Modified
if the request parameter of the cache.match method contains a If-Modified-Since header, you can associate it with Last-Modified.
416 Range Not Satisfiable
If the object specified in the response parameter is 416 Range Not Satisfiable, no caching is performed.

Parameter limits

The cache.put method throws an error in the following scenarios:
The request parameter specifies a method other than the GET method.
The status code of the response parameter is 206 Partial Content.
The response parameter contains a Vary: * header.

delete

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.

Parameters

Parameter
Type
Required
Description
request
string | Request
Yes
The cache key.
GET
The request parameter supports only the GET method.
string
If the request parameter is set to a string, the string is used as a URL to construct a Request object.
options
No
The options.

DeleteOptions

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.

References