Is it possible to set cache items to all zones with an API call?

Hi :),
saying i got a heavy async fetch to do very often and save it to cache, and the result is the same for all zones,
one way i can think of is to purge the key for all the instances (i noticed there is an API for that),
then, in the worker to check the key availability, and if it isn’t there - fetch it.
i prefer to save the fetch time, and do this calculation once, even in a separate service, and set this cache key to all instances/zones,
is this kind of distributed “set” is possible? maybe using cloudflare API?
thanks

It’s currently not possible to have something immediately cached on all zones.
You can however use Cloudflare’s distributed KV store to store anything in it, such as data that is heavy or takes a long time to produce, and then you’ll be able to grab it from any zone with the built-in KV api, which you can read more about here.


thanks, looks great and answer my requirement.
few questions on that, sorry if i’m exceeding the context of the thread,
i couldn’t find the answers in the docs,
are the kv variables deployed to the different worker instances at the moment they are getting updated ?
is it faster than reading from the cache ? not sure if it is a good idea to read from kv, save to cache, and when needed, purge the cache. or just use the kv

You will be able to access the same KV store from any of your workers, so you should have the same values for all workers you may have using it.

It’s not faster than cache, as cache is stored per-zone, whereas KV is stored in specific (core) zones. So you will have a bit more latency when using KV, but the hit rate should be higher if you’re using it as a cache.
I would generally recommend using KV for anything that is expensive or takes long to produce. You could however try to do a combination of both KV and cache: First check if you have a response in the cache for the zone you’re in, and else grab the response from KV.