Why do it?

There are three types of caching in Rails. In this tutorial we are going to concentrate on Page Caching. Page caching is the simplest and fastest form of caching in Rails. If you want to reduce the amount of times your Rails app hits the database then why not add some page caching.

When to do it?

Page caching should be done when you want to cache the entire page. So it's best to do this on pages which don't have lots of sections which are updating their content independently of each other (this is better suited to fragment caching, which will be covered in the next article).

How to do it?

First things first! Enable caching for your development environment by editing the environments/development.rb file and changing the perform_caching property to true. Like so:-

config.action_controller.perform_caching = true

Now, edit the controller that relates to the page you want to cache (in my case, the People controller) and add the caches_page method to the top of the class. As you can see from the example below, you need to pass in a symbol, representing the controller action relating to the page you want to cache.

http://gist.github.com/200207.js">

With this in place, you can browse to the page as you normally would. The page will load, as expected, and a database query will be executed to retrieve the relevant data. However, it will also create a cached file called "people.html" in the /public folder. On any subsequent requests, this cached HTML file will be loaded. Happy days!

Expiring the cache

Caching the page is little use to anyone if you can't expire it. When someone creates and new record, updates a record or destroys a record, you need some mechanism to expire the existing cached file and replace it with new update-to-date cache.

You can expire the cache by calling the expire_page method and passing in a hash with the name of the action. As you would expect, the place to do this is in the methods which update the state of your data i.e. create, update and destroy.

http://gist.github.com/200208.js">

Sweepers

While the above code works, it isn't very flexible plus it clutters your controller with code which is far from DRY. As always, Rails provides a solution to this and they are called Sweepers. Sweepers use an observer to observe the controller to see when given actions occur. When these actions occur, the sweeper kicks in and expires the cache.

Let's do it! Add the cache_sweeper method to the top of your controller. This tells the sweeper which actions it should observe. In this case, when create, update or destroy is called, the sweeper will jump into action.

http://gist.github.com/200209.js">

Now, create a new sub folder under your "app" folder, called "sweepers". This will hold all your sweeper files. In this folder, create a file called person_sweeper.rb and add the following code.

http://gist.github.com/200213.js">

Finally, since we have created a new folder called "sweepers", we need to tell Rails that it should load file in this folder. To do that, edit your config/environment.rb file and add this code:-

config.load_paths += %W( #{RAILS_ROOT}/app/sweepers )

Now that Rails knows where to locate sweeper files, you can restart your web server and check that everything is working.