Preheat

IN DEVELOPMENT DISCLAMER: This gem is still under development and hasn’t been put through intensive real-world testing yet. Any comments/concerns/recommendations would be greatly appreciated.

Keep your Rails.cache up to date without:

  • code bloat in your Rails.cache.fetch invocations

  • tedious dependency setup in a cron task to mimic what your controller does, just so you can call your model/library methods which have the Rails.cache.fetch invocations

Anything executed in a Preheat.it block will change all fetch calls into a fetch calls with :force => true. (:force => true will force a cache-miss and a subsequent cache-write)

Example

This will “preheat” all your Rails.cache.fetch calls on your homepage. It is as simple as that!

Preheat.it do
  app.get("/")
end

Note: If you have not seen “app.get” used before, the “app” object is not related to my preheat gem. It ships with rails: more detail here. I use app.get because ActiveSupport’s fetch method is being modified only in the ruby process which is using Preheat, so something like mechanize/wget/curl would call the page through your frontend webserver and would not be effected by Preheat.it, while app.get will directly call your controller in that same ruby process.

A more detailed example

Let’s say we have a list of product pages where we want the cached to be updated every hour.

#app/models/product.rb
def slow_method
  Rails.cache.fetch("product-slow-method-#{self.id}") do
    sleep 15
    Time.now
  end
end

#lib/tasks/preheat.rake
namespace :preheat do
  desc "Preheat product caches"
  task (:products => :environment) do
    Preheat.it do
      Product.all.each do |product|
        app.get(app.products_path(product)) #or you could just call product.slow_method directly, whatever makes more sense
      end
    end
  end
end

#crontab -e
0 * * * * /path/to/rake preheat:products RAILS_ENV=production 2>&1 >> #{Rails.root}/log/preheat.log &

Installation

  1. gem install preheat

  2. Add the gem to environment.rb or your Gemfile

Thanks

John Hume for helping me cleanup part of my code. Brian Guthrie for getting me all learned up on gem dependencies and the like.