All Products
Search
Document Center

Edge Security Acceleration:Cache API

Last Updated:Mar 26, 2024

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

Working mechanism

You can use the built-in Cache API of EdgeRoutine to cache the results that are processed by EdgeRoutine or the data that is retrieved from the origin server on Dynamic Content Delivery Network (DCDN) 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 EdgeRoutine and DCDN cache.工作原理

API standards

The Cache API tries to follow the standards of Cache. EdgeRoutine reuses the existing Cache engine of DCDN. 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 is successful, the returned Promise object is resolved to undefined.

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

    • If the quota of the Cache engine is exhausted, the Response 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 on the Cache engine of DCDN.

  • 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 TTL for the cache. The configuration of the Cache-Control header must meet the standards 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 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, a Promise object 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, a Promise object is resolved to true.

    • If the Response object fails to be deleted, a Promise object 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 EdgeRoutine subrequests. By default, the number of fetch subrequests of an EdgeRoutine request cannot exceed 32. When EdgeRoutine is put into commercial use, this quota may change. 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 have concurrency control. If one request performs get and the other performs delete for the same URL, the pending state may be returned for the get or delete request. The request cannot be performed because concurrent requests are sent to modify resources that belong to the same key at the same time. We recommend that you try again later.

    Note

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

Cache refresh

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