Cloudflare worker Cache not work
Hi, I tried to put the cache but not work, not sure why.
here is my code
app.get('/get_all_order_status', async (ctx) => {
const cacheUrl = ctx.req.url;
const cacheKey = new Request(cacheUrl.toString(), ctx.req);
const cache = caches.default;
let fromCache = true;
let response = await cache.match(cacheKey);
if (!response) {
const data = await Model.getAllOrderStatus();
response = new Response(JSON.stringify(data), {
headers: { 'content-type': 'application/json' },
});
// Add a custom header to the response before putting it in the cache
fromCache = false;
// Cache the response indefinitely
try {
console.log(cacheKey.method, cacheKey.url);
console.log(response.status, response.statusText);
response.headers.append("Cache-Control", "s-maxage=10000000");
ctx.executionCtx.waitUntil(cache.put(cacheKey, response.clone()));
} catch (error) {
console.error('Error putting data in cache:', error);
}
}
response.headers.set('X-From-Cache', fromCache.toString());
return response;
});
The cacheKey and response is OK but cache.put not work.
here is my code
app.get('/get_all_order_status', async (ctx) => {
const cacheUrl = ctx.req.url;
const cacheKey = new Request(cacheUrl.toString(), ctx.req);
const cache = caches.default;
let fromCache = true;
let response = await cache.match(cacheKey);
if (!response) {
const data = await Model.getAllOrderStatus();
response = new Response(JSON.stringify(data), {
headers: { 'content-type': 'application/json' },
});
// Add a custom header to the response before putting it in the cache
fromCache = false;
// Cache the response indefinitely
try {
console.log(cacheKey.method, cacheKey.url);
console.log(response.status, response.statusText);
response.headers.append("Cache-Control", "s-maxage=10000000");
ctx.executionCtx.waitUntil(cache.put(cacheKey, response.clone()));
} catch (error) {
console.error('Error putting data in cache:', error);
}
}
response.headers.set('X-From-Cache', fromCache.toString());
return response;
});
The cacheKey and response is OK but cache.put not work.