适合场景
网站加速、下载分发、SSR 缓存、API 缓存
Wrangler 配置
// 多数缓存策略在 Dashboard 或 Cache Rules 设置,无需 wrangler 配置 Worker 代码
// 在 Worker 里手动管理边缘缓存
export default {
async fetch(req, env, ctx) {
const cache = caches.default;
let res = await cache.match(req);
if (!res) {
res = await fetch(req);
ctx.waitUntil(cache.put(req, res.clone()));
}
return res;
},
}; 实现注意事项
- ● Cache-Control: private/no-store 默认不缓存;记得改源站 header
- ● 缓存 Key 默认是完整 URL;不同 query 算不同 key(要忽略用 Cache Rules)
- ● 刷新用 Purge 或 Cache Tag;不要靠 TTL 倒计时等过期
已复制