Recaching Pages As Needed

We are planning to move resources from AWS to Cloudflare to reduce costs and simplify our infrastructure. We currently do full page caching. We have a system to recache pages on an as needed basis (content changes, etc.) by having background workers fetch the uncached version, cache it, and then replace the existing cache. We store the caches in Redis currently. This ensures that a user never requests a page from the origin that has to be fully processed. The request is just to fetch the HTML in Redis. I’m having trouble seeing how we can have this sort of fine grained control over the cache in Cloudflare and would appreciate any guidance. We want to store the HTML in the Cloudflare global network permanently and then only replace what is there through a background process

Hi there,

One option is to create a cache all rule and then do an API call to purge the specific file you are updating:

Purging by URL allows you to specify exactly the asset you want deleted from cache. Depending on your system, this could potentially be the easiest solution, as long as you implement a way for an api call to be made each time a new HTML is uploaded.

Another option would be to create a worker (or several) to interact specifically with cache:

Take care.

Thanks for the response. To make sure I fully understand, does a cache purge trigger a recache in the background automatically while continuing to serve end-users the older cached version until it has been updated?

Hi there,

It does not trigger a recache.
A cache purge is merely a purge, the assets will then be gradually cached as they’re requested.
When you perform a request for an asset that is not on cache, Cloudflare will serve you that asset from the origin and the cache header will show as MISS, but Cloudflare will take that same asset and cache it, so subsequent requests for the same asset hitting the same Cloudflare datacenter will result in a cache HIT (until Edge Cache TTL is reached or LRU policy happens to evict it).

Take care.