【发布时间】:2021-09-03 14:16:06
【问题描述】:
我正在构建一个 cloudflare worker,我想缓存一个 fetch 请求至少 24 小时。这意味着如果我在 24 小时内两次发出相同的请求,则不应调用 fetch() 并使用缓存的响应。 我已经编写了这个脚本,但是每次都会调用远程网站 (unixtimestamp.com)。
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
});
async function handleRequest(request) {
const url = 'https://www.unixtimestamp.com/'
let response = await fetch(url, {
cf: {
cacheTtlByStatus: { "200-299": 60*60*24, 404: -1, "500-599": -1 }
}
})
return response
}
文档:https://developers.cloudflare.com/workers/examples/cache-using-fetch
【问题讨论】:
-
如何将
cacheEverything选项设置为true? -
只有状态为200时才需要缓存。无论如何,即使使用cacheEverything结果都是一样的
-
cacheEverything: true只告诉缓存所有 URL 都应该被认为是可缓存的。否则,它假定只能缓存以某些文件扩展名结尾的 URL。所以你确实想要cacheEverythig: true。同时,cacheTtlByStatus选项仅适用于企业客户。您是企业客户吗?如果是这样,您应该联系您在 Cloudflare 的 CSM,以获得有关完成这项工作的帮助。 -
它仍然没有缓存“cacheEverything: true”
-
您的工作人员是否托管在workers.dev 上?如果是,则没有缓存将有效。需要自定义域才能激活缓存。
标签: cloudflare cloudflare-workers