Edge Developer Platform
  • Edge Functions
    • Overview
    • Getting Started
    • Operation Guide
      • Function Management
      • Web Debugging
      • Function Trigger
      • Environment Variable
      • Code Replica
    • 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
      • Example Overview
      • Obtaining Client URL Information
      • Customization Based on Client Geo Location
      • Obtaining Client Geo Location Information
      • Batch Redirect
      • URL rewrite based on regular expressions
      • 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
      • Complex origin-pull URL rewriting
      • Web Bot Auth
    • Practical Tutorial
      • Overview
      • Origin retrieval based on user IP/geographic location
        • EdgeOne Implementation of Session Persistence Based on Client IP Addresses
        • EdgeOne Implementation of Origin-Pull Based on Client's Geo Location
      • APK dynamic packaging
        • EdgeOne enables dynamic packaging of Android APKs.
          • Feature Overview
          • Step 1: Preprocess the Android APK Parent Package
          • Step 2: Write the Channel Information into the APK Package with EdgeOne Edge Functions
      • Canary Release and Region-specific Execution
      • Adaptive Image Format Conversion via Edge Functions
      • Two Ways to Implement CDN Origin-pull Via Edge Function: Fetch and Passthrough
  • KV Storage
    • Overview
    • Operation Guide
  • Edge reasoning
    • Edge Inference Overview
    • Quick Guide

Using the Cache API

Use the Cache API in edge functions to read from and write to the edge functions independent cache: use a Request carrying eo.cacheKey as the cache key. On a cache miss, pull the remote resource from the origin and write it to the cache (with a cache duration of 10 seconds). On a cache hit, return the cached content directly. You can determine whether the cache is hit by checking the x-edgefunctions-cache response header.
Note:
For read, write, and delete operations, use the same Request object that specifies eo.cacheKey. If eo.cacheKey is not carried, the write operation will be rejected and a forbidden cdn cache error will be thrown. For details, see Cache API.
To determine whether a resource is cached by an EdgeOne site, use a Request created with the target resource URL for matching. It is recommended not to carry any client request headers. For details, see the match section of Cache API.

Sample Code

const RESOURCE_URL = 'https://static.cloudcachetci.com/qcloud/main/scripts/release/common/vendors/jquery-3.2.1.min.js';

async function fetchResource(event, request, cache) {
// Cache miss: Pull the resource from the origin and write it to the edge function's dedicated cache.
const response = await fetch(RESOURCE_URL);
response.headers.set('Cache-Control', 's-maxage=10');
event.waitUntil(cache.put(request, response.clone()));
response.headers.append('x-edgefunctions-cache', 'miss');
return response;
}

async function handleEvent(event) {
const cache = caches.default;

// Read, write, and delete use the same Request object with eo.cacheKey specified
const request = new Request(RESOURCE_URL, {
eo: { cacheKey: 'jquery-v1' },
});

try {
let response = await cache.match(request);
if (!response) {
return fetchResource(event, request, cache);
}
response.headers.append('x-edgefunctions-cache', 'hit');
return response;
} catch (e) {
// Cache expiration (504) or exception: Delete the cache, then perform origin-pull again and write the result
try {
await cache.delete(request);
} catch {}
return fetchResource(event, request, cache);
}
}

addEventListener('fetch', (event) => {
event.respondWith(handleEvent(event));
});

Sample Preview

Enter a URL that matches an edge function trigger rule in the browser address bar to preview the example effect.
The cache is not hit.



The cache is hit.




References