I can't use Rails caching because...

November 10, 2006 Link to post  Permalink

One of the reasons a site can’t use page or action caching in Rails is because different types of users see different page contents for the same URL. For instance, logged in users may see user specific info on the page, whilst other users will see a generic page. Until now, that would mean that the page can’t be cached for anyone.

I’ve just updated my action_cache plugin to help work around some of these cases and allow caching for at least some of the requests.

You can now implement a method on your controller to tell the action cache whether the current request should be cached or not. With this, you can decide, for instance, to cache requests when the user is not logged in and not cache requests for logged in users.

Here’s what that code looks like:

1lass ApplicationController < ActionController::Base
2  ...
3  def cache_action?(action_name)
4    !user_logged_in?
5  end
6  ...

Install the update using the Rails plugin script: script/plugin install action_cache, take a look at the source code