Yesterday, while working on a soon-to-be planet for my LUG, I came up with this ingenious way of eliminating the caching gap that the usual rails ActionController caches_page introduces when used with cron to expire a page at a specified time interval. It’s amazingly simple. This works ideally for one-page systems (such as this planet), but can probably be extended for many pages.
While you would normally develop code like this to put in a cron job:
rm planet.html curl http://planet.example.com > planet.html
the caching gap is obvious here. Anyone who visits the site while curl (and the aggregating rails app in the back) is doing it’s job will have to wait for that process to be completed(which on my dev-server can take up to 17secs).
In order to eliminate the gap and acquire the new page, this is what must be done:
mv planet.html oldplanet curl http://planet.example.com > planet.html & mv oldplanet planet.html
Notice how the old page is saved and deleted, so rails produces a brand new one for curl and immediately after curl has started, the old page will be put back in the cache so no visitors will notice. After curl finishes it’s background job, we’ll have an updated planet and everyone will be happy.
I searched for days upon days to find the perfect (simplicity was my most important criteria here) caching method for this planet and finally here it is. Hope it helps.
This, together with something like rails’s feed-normalizer gem can make writing a rails planet aggregator a breeze.