All Products
Search
Document Center

Edge Security Acceleration:Cache API

Last Updated:Nov 04, 2024

The Cache API is a method that is used to cache data. You can call the Cache API to cache data on points of presence (POPs) so that data can be served quickly for subsequent requests. In a Cache API operation, you can configure the time to live (TTL) and cache size based on your business requirements.

How it works

You can use the built-in Cache API of Edge Routine to cache the results that are processed by Edge Routine or the data that is retrieved from the origin server on Edge Security Acceleration (ESA) POPs. This way, other requests that access the same POPs can reuse the cached results, and the number of repetitive calculations or requests is reduced. The following figure shows the relationship between Edge Routine and ESA cache.工作原理

API specification

The Cache API basically follows the specification described in Cache, while Edge Routine reuses the existing cache engine of ESA. As a result, the final semantics for the Cache API and Cache may be different.

API definition

cache.put(request/string, response)

  • Adds a Response object to Cache.

    • If the PUT method succeeds, the returned Promise is resolved to undefined.

    • If the Cache engine fails, the Promise object is rejected and an error is triggered.

    • If the quota of the Cache engine is exhausted, the Promise object is rejected and an error is triggered.

  • Set the cache key to the URL of the Request object or use a string to specify the URL. Only HTTP URLs are supported due to the limits of the Cache engine of ESA.

  • This is an asynchronous function. You can call await to wait until the Response object is added to Cache.

  • You can configure the Cache-Control header for the Response object to define the TTL for the cache. The configuration of the Cache-Control header must meet the specification of Cache.

Examples

  • Add content to Cache

    async function doPut() {
      await cache.put("http://www.example.com", new Response("Hello World"));
      new Response("Hello World", {headers: [["cache-control", "max-age=10"]]})); 
    }
  • Add content to Cache and configure the TTL

    async function doPut() {
      await cache.put("http://www.example.com", new Response("Hello World"));
      await cache.put("http://www.example.com", 
      new Response("Hello World", {headers: [["cache-control", "max-age=10"]]})); 
    }

cache.get(request/string)

  • Obtains a Response object whose key is the input request or the input string. If the object does not exist, the returned Promise is resolved to undefined.

  • This is an asynchronous function. You can call await to wait until the Response object is obtained.

  • The GET method may fail to return the object that is added in the preceding PUT method. The Cache object may not be fetched due to the least recently used (LRU) caching algorithm.

Examples

  • Obtain a Response object

    async function doGet() {
        let resp = await cache.get("http://www.example.com");
    }
  • Parse the stored results into JSON

    async function doGet() {
        let resp = await cache.get("http://www.example.com");
        let j = await resp.json();
    }

cache.delete(request/string)

  • Deletes a Response object whose key is the input request.

    • If the Response object is deleted, the returned Promise is resolved to true.

    • If the Response object fails to be deleted, the returned Promise is resolved to false.

  • This is an asynchronous function. You can call await to wait until the Response object is deleted.

Example: Delete the resources of a key

async function doDelete() {
  let resp = await cache.delete("http://www.example.com");
  
  if (resp) {
        console.alert("done");
  } else {
        console.alert("failed");
  }
}

Limits

  • All preceding requests are subrequests that share the quota of Edge Routine subrequests. By default, an Edge Routine request can have a maximum of 32 fetch subrequests. This quota may change after Edge Routine is put into commercial use. Cache calls can be considered subrequests that share the quota of fetch subrequests. The total number of subrequests of cache.put, cache.get, cache.delete, and fetch cannot exceed 32 in a Request context.

  • cache.put, cache.get, and cache.delete are under concurrency control. If one request is performing the GET operation and another request is performing the DELETE operation for the same URL, the pending state may be returned for the GET or DELETE operation. The requests fail because the requests attempt to modify the resource that has the same key at the same time. In this case, you can try again later.

    Note

    If the requests of cache.put, cache.get, and cache.delete are rejected and the returned Promise is resolved to true, the operation is in concurrency control.

Purge cache

The content that is cached by calling the Cache API cannot be automatically purged. You must specify the TTL parameter when you use Cache.put().