action cache, , , " />

Conditional Page Cache

January 27, 2009

cashDo to some Obama day drama, I wrote a little plugin that allows you to conditionally page or action cache at runtime. This means that you might page cache depending on the user type, or the date or how you feel when you wake up.

script/plugin install http://arperftoolkit.rubyforge.org/svn/trunk/conditional_page_cache

At the top of the controller, indicate which actions you wish to cache with conditionally_cache.

class MyController < Application

  conditionally_cache :action_one, :action_two

  def cache_page?(current_action_name)
    @user.demo?
  end

  def cache_action?(action_name)
    !@user.admin?
  end

end

To use page caching, add a method cache_page?(current_action_name) on the controller. The page will be cached if this method returns true.

Similarly, return true from method cache_action?(current_action_name) when you want to action cache the page. This method is the same as the one used by the action caching plugin which works great along with this one.

Leave a Reply