Cache seems to not be storing anything

In my calls TTL is set to 1800 and checking with postman the cache control is being set in my response, so the header is set just fine. I just can't wrap my head around why the console.log is logging undefined for this code that feels like it should be caching properly, but somehow literally just after caching it, its undefined. Any help? Other info: Basically, trying to make a sort of proxy for a online API, but that API has cache headers set to private and 0 cache time, which wasn't allowing the response to be cached. I am creating the new response with all the headers and the body copied but just the Cache-Control modified as I assumed this would work, like the article I linked talks about at the bottom, but the cache is telling me nothing is stored.
head.set('Cache-Control', 'public, max-age='+ttl+', s-maxage='+ttl);
let result = new Response(resp.body, {status: 200, headers: head})
await caches.default.put(req, result.clone());
console.log(await caches.default.match(req));
head.set('Cache-Control', 'public, max-age='+ttl+', s-maxage='+ttl);
let result = new Response(resp.body, {status: 200, headers: head})
await caches.default.put(req, result.clone());
console.log(await caches.default.match(req));
https://developers.cloudflare.com/workers/learning/how-the-cache-works/
How the Cache works · Cloudflare Workers docs
Workers was designed and built on top of Cloudflare’s global network to allow developers to interact directly with the Cloudflare cache. The cache can …
15 Replies
Cooleg
CoolegOP2y ago
await caches.default.put(req, result.clone()).then(async () => {
console.log(await caches.default.match(req));
});
await caches.default.put(req, result.clone()).then(async () => {
console.log(await caches.default.match(req));
});
The console.log in this tidbit is also saying null/undefined.
broswen
broswen2y ago
How are you triggering your worker? The docs here https://developers.cloudflare.com/workers/runtime-apis/cache say that For Workers fronted by Cloudflare Access Cache API is not currently available. Only Workers deployed to custom domains have access to functional cache operations.
Cache · Cloudflare Workers docs
The Cache API allows fine grained control of reading and writing from the Cloudflare global network cache.
Cooleg
CoolegOP2y ago
Do I need to trigger it with the custom domain too?
broswen
broswen2y ago
From those docs it seems that only workers deployed to custom domains have access to functional cache operations.
Cooleg
CoolegOP2y ago
Ill check that out It is deployed to a custom domain but I was using the workers.dev anyways Triggering it with custom domain isn't working either All four console.logs i have are null/undefined too
Cooleg
CoolegOP2y ago
broswen
broswen2y ago
Is the req something you create or the raw request from the worker invocation? I believe there are certain headers that prevent caching from happening.
Cooleg
CoolegOP2y ago
console.log(await caches.default.match(generated));
let req = generated;
req.headers.set("API-Key", apikey);
req.headers.set("User-Agent", "insertmyuseragenthere");
console.log(await caches.default.match(req));
let resp: Response = await fetch(req);
if (resp.status == 200) {
let head = new Headers();
resp.headers.forEach((value, key) => {
head.set(key, value)
})
head.set('Cache-Control', 'public, max-age='+ttl+', s-maxage='+ttl);
let result = new Response(resp.body, {status: 200, headers: head})
await caches.default.put(req, result.clone()).then(async () => {
console.log(await caches.default.match(req));
});
console.log(await caches.default.match(req));
return result;
}
console.log(await caches.default.match(generated));
let req = generated;
req.headers.set("API-Key", apikey);
req.headers.set("User-Agent", "insertmyuseragenthere");
console.log(await caches.default.match(req));
let resp: Response = await fetch(req);
if (resp.status == 200) {
let head = new Headers();
resp.headers.forEach((value, key) => {
head.set(key, value)
})
head.set('Cache-Control', 'public, max-age='+ttl+', s-maxage='+ttl);
let result = new Response(resp.body, {status: 200, headers: head})
await caches.default.put(req, result.clone()).then(async () => {
console.log(await caches.default.match(req));
});
console.log(await caches.default.match(req));
return result;
}
req is created by me thats the code in the function thats the function call to that above function makeRequest(new Request("url here"), 1800); in this specific case that url is literally hard coded so it shouldnt be changing
broswen
broswen2y ago
You can try creating a different req to use as the cache key without the API key/user-agent headers. It might be those. I'm not sure otherwise
Cooleg
CoolegOP2y ago
ill use generated then, that literally just has the url see if that works Still didn't work i added another console.log just to make sure the url wasnt somehow changing and it wasnt generated in this case is just the new request in that call so its literally just the url thats it
await caches.default.put(generated, result.clone()).then(async () => {
console.log(await caches.default.match(generated));
});
await caches.default.put(generated, result.clone()).then(async () => {
console.log(await caches.default.match(generated));
});
this somehow nulling has me really stumped though
broswen
broswen2y ago
Yea that makes me think it's not actually caching.
Cooleg
CoolegOP2y ago
Cooleg
CoolegOP2y ago
Both with a subdomain and a domain set it wont cache so I'm assuming that that's not the problem
export default async function makeRequest(generated: Request, ttl: number): Promise<Response> {
console.log(generated.url);
console.log(await caches.default.match(generated));
let req = generated;
req.headers.set("API-Key", apikey);
req.headers.set("User-Agent", "useragent");
console.log(await caches.default.match(req));
let resp: Response = await fetch(req);
if (resp.status == 200) {
let head = new Headers();
resp.headers.forEach((value, key) => {
head.set(key, value)
})
head.set('Cache-Control', 'public, max-age='+ttl+', s-maxage='+ttl);
let result = new Response(resp.body, {status: 200, headers: head})
await caches.default.put(generated, result.clone()).then(async () => {
console.log(await caches.default.match(generated));
});
console.log(await caches.default.match(generated));
return result;
}
else {
console.log(await resp.json());
return new Response("Information request seems to have failed, try again later.", {status:500});
}
}
export default async function makeRequest(generated: Request, ttl: number): Promise<Response> {
console.log(generated.url);
console.log(await caches.default.match(generated));
let req = generated;
req.headers.set("API-Key", apikey);
req.headers.set("User-Agent", "useragent");
console.log(await caches.default.match(req));
let resp: Response = await fetch(req);
if (resp.status == 200) {
let head = new Headers();
resp.headers.forEach((value, key) => {
head.set(key, value)
})
head.set('Cache-Control', 'public, max-age='+ttl+', s-maxage='+ttl);
let result = new Response(resp.body, {status: 200, headers: head})
await caches.default.put(generated, result.clone()).then(async () => {
console.log(await caches.default.match(generated));
});
console.log(await caches.default.match(generated));
return result;
}
else {
console.log(await resp.json());
return new Response("Information request seems to have failed, try again later.", {status:500});
}
}
This is the entire function although not sure if its the most helpful Making the same request to the server multiple times and that first url console.log is the exact same every time but all the other console.logs are consistently nulling. (problem still not solved in case anyone knows what may be going on) get get is default iirc and either way its not even storing response cache either when told to in cache api
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Cooleg
CoolegOP2y ago
nope
Want results from more Discord servers?
Add your server