Rails caching notes

General Notes

Very nice tutorial for rails 2.1 caching.

API references: Fragments, Sweepers, the Store

Post how to move the page cache in a folder with some server examples

Observing Controllers with Sweepers

Here is described how you can use the Sweepers to observe controller actions. I totaly agree that the documenation is very bad on the sweepers and it is like a rule book of MTG than usefull rdoc.

Here is what I have found on the naming of the callbacks.

Checking the source code in sweeping.rb shows that the callbacks that are used for the controller are constructed in this way:

controller_callback_method_name = "#{timing}_#{controller.controller_name.underscore}"
action_callback_method_name = "#{controller_callback_method_name}_#{controller.action_name}"

This means that the method which will be called in the sweeper will be named

  • after/before_user – for the controller UserController
  • after/before_user_login – for the action login in the controller UserController

Here is an example.

Skipping callbacks from an observer

I know that it is not good idea, but some times there is no time/way to do skip it. I have found a plugin and this post here. I have choose to use the second one in my project. Here is the example

And later in the controller….model…

timed_fragment_cache

Some patches on the timed_fragment_cache plugin.

Note how Jolyon Says suggest to expire the cache when you have time zone : when_fragment_expired ‘tags_home_page’, Time.now + 10.minutes do

I have notice that timed_fragment_cache (or the patch) have a problem with expiring the cache from the controller. In the documenation it is stated that it is ok to expire the cache only in the controller with

when_fragment_expired  "_last_update",  Time.now + 1.minutes  do

and there is no need to expire it in the view, but you MUST expire it in the view.

cache "_last_update" do

must be

cache "_last_update", Time.now + 1.minutes  do