caches.default.match() always returns undefined. even after caches.default.put() is executed.
4 Replies
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:
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.
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?
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
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