边缘开发者平台
  • 边缘函数
    • 概述
    • 快速指引
    • 操作指引
      • 函数管理
      • Web调试
      • 触发配置
      • 环境变量
      • 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
    • 示例函数
      • 示例概述
      • 获取客户端URL信息
      • 基于客户端地理位置的自定义
      • 获取客户端地理位置信息
      • 批量重定向
      • URL rewrite based on regular expressions
      • 返回 HTML 页面
      • 返回 JSON
      • Fetch 远程资源
      • 请求头鉴权
      • 修改响应头
      • AB 测试
      • 设置 Cookie
      • 基于请求区域重定向
      • Cache API 使用
      • 缓存 POST 请求
      • 流式响应
      • 合并资源流式响应
      • 防篡改校验
      • m3u8 改写与鉴权
      • 图片自适应缩放
      • 图片自适应 WebP
      • 自定义 Referer 限制规则
      • 远程鉴权
      • HMAC 数字签名
      • 自定义下载文件名
      • 获取客户端 IP
      • Complex origin-pull URL rewriting
      • Web Bot Auth
    • 最佳实践
      • 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
      • 通过边缘函数实现自适应图片格式转换
      • 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

Cache

Cache 基于 Web APIs 标准 Cache API 进行设计。边缘函数运行时会在全局注入 caches 对象,该对象提供了一组缓存操作接口。
说明:
缓存的内容仅在当前数据节点有效,不会自动复制到其他数据节点。

构造函数

使用 caches.default 可以获取默认的 cache 实例。
// 获取默认 cache 实例
const cache = caches.default;

// 效果等同于 caches.default
await caches.open('default');
使用 caches.open 创建指定命名空间的 cache 实例。
// 创建指定命名空间的 cache 实例
const cache = await caches.open(namespace);

参数

caches.open(namespace) 方法参数说明如下。
参数名称
类型
必填
说明
namespace
string
是
缓存命名空间。
如果该值为 "default" 则表示默认实例,也可直接使用 caches.default 获取默认实例。

实例方法

match

cache.match(request: string | Request, options?: MatchOptions): Promise<Response | undefined>
获取 request 关联的缓存 Response。返回一个 Promise 对象。如果缓存存在,则包含 Response 对象,反之包含 undefined。
注意:
cache.match 内部不会主动回源,缓存过期则会抛出 504 错误。

参数

参数名称
类型
必填
说明
request
string | Request
是
请求对象,headers 说明如下。
GET
request 只支持 GET 方法,当类型为 string 时,将被作为 URL 构造 Request 对象。
Range
request 包含 Range 头部时,如果缓存的 Response 能够支持 Range 范围处理,返回 206 响应。
If-Modified-Since
request 包含 If-Modified-Since 头部时,如果缓存的 Response 存在 Last-Modified 头部,且 Last-Modified 与 If-Modified-Since 相等,返回 304 响应。
If-None-Match
request 包含 If-None-Match 头部时,如果缓存的 Response 存在 ETag 头部,且 ETag 与 If-None-Match 相等, 返回 304 响应。
options
否
选项。

MatchOptions

属性名
类型
示例值
说明
ignoreMethod
boolean
true
是否忽略 Request 的 method。为 true 时,会忽略 Request 原来的 method,作为 GET 处理。

put

cache.put(request: string | Request, response: Response): Promise<undefined>
尝试使用给定的 request 作为缓存 key,将 response 添加到缓存。无论缓存是否成功,均返回 Promise<undefined> 对象。
注意:
当参数 response 对象的 Cache-Control 头部表示不缓存时,抛出 413 错误。

参数

参数名称
类型
必填
说明
request
string | Request
是
缓存 key,说明如下。
GET
参数 request 仅支持 GET 方法,其他方法,将抛出参数错误。
string
当参数 request 类型为 string 时,将被作为 URL 构造 Request 对象。
response
是
缓存内容,说明如下。
Cache-Control
支持 s-maxage、max-age、no-store、no-cache、private;其中 no-store、no-cache、private 均表示不缓存,cache.put 将返回 413 错误。
Pragma
当 Cache-Control 未设置,并且 Pragma 为 no-cache。此时表示不缓存。
ETag
当 cache.match 参数 request 包含 If-None-Match 头部时,可关联 ETag 使用。
Last-Modified
当 cache.match 参数 request 包含 If-Modified-Since 头部时,可关联 Last-Modified 使用。
416 Range Not Satisfiable
当参数 response 对象为 416 Range Not Satisfiable 时,暂不缓存。

参数限制

cache.put 使用以下的参数值,将抛出参数错误:
参数 request 为 GET 方法之外的其他方法.
参数 response 状态码为 206 Partial Content。
参数 response 包含 Vary: *头部。

delete

cache.delete(request: string | Request, options?: DeleteOptions): Promise<boolean>
删除 request 关联的缓存 response。未发生网络错误时, 总返回 Promise,并包含 true,反之包含 false。

参数

参数名称
类型
必填
说明
request
string | Request
是
缓存 key,说明如下。
GET
参数 request 仅支持 GET 方法
string
当参数 request 类型为 string 时,将被作为 URL 构造 Request 对象。
options
否
配置选项。

DeleteOptions

属性名
类型
示例值
说明
ignoreMethod
boolean
true
是否忽略 request 的方法名。为 true 时,会忽略 Request 原来的方法,作为 GET 处理

相关参考