Static Pages on Rails

When I said that favcol is built on Rails, I wasn't being entirely truthful.

I'm not serving anything on the fly to the public, as it only changes once every 5 minutes, my server is a little overloaded and the algorithms I'm using take a little too long.

Instead, I'm (ab)using Rails page caching with a little twist. In short, instead of deleting the cached page whenever it's out of date, it's replaced with a new version.

This only makes sense if your site has frequently requested pages that update rarely and can take little while to render, and you don't mind waiting a few extra seconds for a non-public-facing request to finish. In the case of favcol, this isn't a problem as it's a computer making the request as part of the 5 minute cronjob:

*/5 * * * * /usr/bin/curl -s http://localhost/cron"

This url is mapped to an action looking something like

def cron
  if local_request?
    # update the data from flickr
    Photo.refresh()

    # first grab the data needed for the template
    index

    # then cache the page
    cache_page render_to_string(:action => :index), 
               :controller => "public", 
               :action => "index"

    # return empty string so cron won't send an email
    render :text => ""
  else
    render :text => "not local request"
  end
end

If you use this technique, be aware that immediately after a deployment or code update the cached pages may not exist. Adding a caches_page :index to the controller fixes that problem too.

< previous next >