caches.default.match() always returns undefined. even after caches.default.put() is executed.

export async function FITThetaTauEvents(ctx: ExecutionContext) {
const events_list = await (async () => {
const events_discord_url = `${RouteBases.api}${Routes.guildScheduledEvents("12345678901234")}?with_user_count=true`;

//check for cache
const cache_res = await caches.default.match(events_discord_url);
if (exists(cache_res)) { //always returns udefined here some how <<<<<<<<<<<<<<<-----
const now = Date.now();
const cached_time = (new Date(cache_res.headers.get('X-Cached-At')!)).getTime();

if ((now - cached_time) <= 300000)
return await cache_res.clone().json() as RESTGetAPIGuildScheduledEventsResult;
}

//else fetch
const proto_res = await fetch(
events_discord_url,
{
body: null,
method: 'GET',
headers: {
'Authorization': `Bot ${FITThetaTauRESTBot_credentials.token}`,
'Content-Type': `application/json`
}
}
);

if (!proto_res.ok) return null;

if (exists(proto_res.clone().body)) {
const cache_header = new Headers(proto_res.headers);
cache_header.append('X-Cached-At', new Date().toUTCString());

ctx.waitUntil(caches.default.put(
events_discord_url,
new Response(proto_res.clone().body, {
headers: cache_header,
status: proto_res.status,
statusText: proto_res.statusText
})
))

return await proto_res.json() as RESTGetAPIGuildScheduledEventsResult|undefined;
} else {
return null;
}
})();
export async function FITThetaTauEvents(ctx: ExecutionContext) {
const events_list = await (async () => {
const events_discord_url = `${RouteBases.api}${Routes.guildScheduledEvents("12345678901234")}?with_user_count=true`;

//check for cache
const cache_res = await caches.default.match(events_discord_url);
if (exists(cache_res)) { //always returns udefined here some how <<<<<<<<<<<<<<<-----
const now = Date.now();
const cached_time = (new Date(cache_res.headers.get('X-Cached-At')!)).getTime();

if ((now - cached_time) <= 300000)
return await cache_res.clone().json() as RESTGetAPIGuildScheduledEventsResult;
}

//else fetch
const proto_res = await fetch(
events_discord_url,
{
body: null,
method: 'GET',
headers: {
'Authorization': `Bot ${FITThetaTauRESTBot_credentials.token}`,
'Content-Type': `application/json`
}
}
);

if (!proto_res.ok) return null;

if (exists(proto_res.clone().body)) {
const cache_header = new Headers(proto_res.headers);
cache_header.append('X-Cached-At', new Date().toUTCString());

ctx.waitUntil(caches.default.put(
events_discord_url,
new Response(proto_res.clone().body, {
headers: cache_header,
status: proto_res.status,
statusText: proto_res.statusText
})
))

return await proto_res.json() as RESTGetAPIGuildScheduledEventsResult|undefined;
} else {
return null;
}
})();
4 Replies
Chaika
Chaika15mo ago
Two things I see: You need to use normal Cache-headers with the Cache API, you can specify a max age there like normal without custom logic, see: https://developers.cloudflare.com/workers/examples/cache-api/ and https://developers.cloudflare.com/workers/runtime-apis/cache/ for more exact implementation details also, there are certain limitations of where you can use the cache api:
However, any Cache API operations in the Cloudflare Workers dashboard editor, Playground previews, and any *.workers.dev deployments will have no impact. For Workers fronted by Cloudflare Access
, the Cache API is not currently available. Only Workers deployed to custom domains have access to functional cache operations.
However, any Cache API operations in the Cloudflare Workers dashboard editor, Playground previews, and any *.workers.dev deployments will have no impact. For Workers fronted by Cloudflare Access
, the Cache API is not currently available. Only Workers deployed to custom domains have access to functional cache operations.
Using the Cache API · Cloudflare Workers docs
Documentation for Cloudflare Workers, a serverless execution environment that allows you to create entirely new applications or augment existing ones …
Cache · Cloudflare Workers docs
The Cache API allows fine grained control of reading and writing from the Cloudflare global network cache.
IdkWhatever69
IdkWhatever69OP15mo ago
the thing is discord has rate limits so im trying to save the fetched data for 5 mins is this the best way of doing this?
Chaika
Chaika15mo ago
is your worker a http interactions handler, or triggered by user requests? Cache is free - but only in that specific colo/cloudflare location, iirc all interactions are triggered by a specific Discord location so you might have pretty good hit rates from cache R2 is Cloudflare's generic storage solution, slower, single origin/location, but strongly consistent and still cheapish KV is Cloudflare's Key-value data store, two central stores and globally replicates, eventually consistent, more expensive then R2 but built in support for auto expiring keys You'd achieve higher hit rates with KV/R2, but at a cost of paying for it and additional latency
IdkWhatever69
IdkWhatever69OP15mo ago
its triggered by http get request (from a browser, etc) i do have custom domain but also have a .workers.dev for the same worker
No description
Want results from more Discord servers?
Add your server