Cache-Control simple question
Is it possible to have a setup where user from public internet hitting my public API (running as worker) would get a cached response from CF edge (before hitting my API), thus saving me requests count for my free plan?
I made my responses return
Cache-Control header like this:
And haven't done anything else, I assume the s-maxage would mean that edge would save it, but that doesn't work (I can't see CF-Cache-Status header in my API response or any cache status header).
I know if I set it up to use Cache API, it would work, but that would mean that all requests would still hit my API and it would run some minimal compute to check if cache contains data, and return from it.9 Replies
not possible no
?workers-cache
Workers run in front of the Cloudflare Cache layer. This means it's not possible to avoid invoking a Worker by using the Cache.
If you want to cache things between Worker invocations, you can use the Cache API to store and retrieve data from the Cloudflare Cache (which is local per colocation).
For global data/cache storage, you may be interested in Workers KV instead.
That sucks. What about domain cache rules?
Don't the cache rules cache my domain origin response at edge?
Cache Rules can be used to configure caching origin responses sure
here's the problem though:
Workers literally run before the cache layer, so no setting of cache rules/page rules/headers/spaghetti/fancy dancing will make Workers not run before the cache layer
okay so for example if my worker calls 3rd party API, the cache rules would cache that only?
user -> my worker -> cache -> 3rd party APIyup, that's part of the reason why they run before cache, so you can change/run stuff before cache hits. Workers themselves are also faster/cheaper to run then a cache lookup
That's interesting, I would not expect it to work like that.
Is there cache layer between D1 and workers? Would it be enough to set cache rule, or do I need to use Cache API?
cache api
also using cache api allows you to avoid the d1 read and its cost, and cache api is free
I don't think there's a single binding your cache rules matter for. Some have internal cache but charge you for cache hits anyway, ex:
KV has fast internal cache but still charges you. So does Workers Static Asset binding minus the charging, since it's just KV.
R2 has internal cache, but it still has to go to the bucket region for it
Containers, no cache or anything
alright thanks for all the answers 👍