Workers fetch cache not working?

I'm using an external api, which is rate limited. But I need more requests than that the limit allows, therefore I want to use cloudflare workers to cache the request and work as a middle man. I'm using the hono framework to handle my requests (but I don't think that should affect this issue). I'm using this function to cache my requests.
export const cachedFetch = async (url: string, time = 60 * 60) =>
await fetch(url, {
cf: {
cacheTtl: time,
cacheEverything: true,
cacheKey: url,
},
});
export const cachedFetch = async (url: string, time = 60 * 60) =>
await fetch(url, {
cf: {
cacheTtl: time,
cacheEverything: true,
cacheKey: url,
},
});
But when runnin this, I'm still getting rate-limited by the external api? Even tough 1 hour is plenty below the rate limit?
4 Replies
zegevlier
zegevlier17mo ago
Cloudflare's cache doesn't guarantee that the data will stay there for 1 hour. The one hour will just be the maximum time. Are the requests actually coming from cache (they should have a CF-Cache-Status header, and if it was in cache it will return HIT)? If you want to guarantee that the data will be still be there in an hour, you need to use something like KV.
Arthur
Arthur17mo ago
allow GET, HEAD, OPTIONS
cf-cache-status DYNAMIC
cf-ray 7bfff7c627262e3e-BRU
connection keep-alive
content-length 236
content-type application/json
cross-origin-opener-policy same-origin
date Sun, 30 Apr 2023 13:00:28 GMT
referrer-policy same-origin
server cloudflare
vary Cookie, Origin
via 1.1 vegur
x-content-type-options nosniff
x-frame-options DENY
allow GET, HEAD, OPTIONS
cf-cache-status DYNAMIC
cf-ray 7bfff7c627262e3e-BRU
connection keep-alive
content-length 236
content-type application/json
cross-origin-opener-policy same-origin
date Sun, 30 Apr 2023 13:00:28 GMT
referrer-policy same-origin
server cloudflare
vary Cookie, Origin
via 1.1 vegur
x-content-type-options nosniff
x-frame-options DENY
These are the headers that are present on the response, so the cf-cache-status is present but set to dynamic. Also what do you mean with HIT?
zegevlier
zegevlier17mo ago
If it was getting it from the cache that header would be set to "HIT". You might want to use the cache API here, or use KV.
Arthur
Arthur17mo ago
Alright ^^
Want results from more Discord servers?
Add your server