Example worker code requested - for using the new Worker Cache API to run a primitive, poll-based cache for a REST API


#1

This refers to https://blog.cloudflare.com/introducing-the-workers-cache-api-giving-you-control-over-how-your-content-is-cached/

We have a REST API, and certain items for a specific org/token do not change very often. When GET requests arrive for those specific items, with a specific token, on a specific URL path - we’d like to implement a 5-minute cache for them. We’re okay with such items being potentially stale for 5 minutes, but they must be refreshed if requested after 5 minutes have elapsed.

Questions:

  1. How long does it take to invalidate cache via the API, everywhere?
  2. Similar to (1) - how long before a newly cached item shows up everywhere?
  3. How do you calculate time elapsed e.g. 5 minutes - to know when to refresh the cache again?
  4. Can anyone propose any sample worker code that would implement such a timestamp based caching schema based on a primitive “keep-cached” time of 5 minutes since the object was last cached?

Help appreciated!