Rails Page Caching

1 Basic Caching

This is an introduction to the three types of caching techniques that Rails provides by default without the use of any third party plugins.

To start playing with caching you’ll want to ensure that config.action_controller.perform_caching is set to true, if you’re running in development mode. This flag is normally set in the corresponding config/environments/*.rb and caching is disabled by default for development and test, and enabled for production.

config.action_controller.perform_caching is set to true

1.1 Page Caching

Page caching is a Rails mechanism which allows the request for a generated page to be fulfilled by the webserver (i.e. Apache or nginx), without ever having to go through the Rails stack at all. Obviously, this is super-fast. Unfortunately, it can’t be applied to every situation (such as pages that need authentication) and since the webserver is literally just serving a file from the filesystem, cache expiration is an issue that needs to be dealt with.

To enable page caching, you need to use the caches_page method.

class ProductsController < ActionController
 
  caches_page :index
 
  def index
    @products = Products.all
  end
end

Let’s say you have a controller called ProductsController and an index action that lists all the products. The first time anyone requests /products, Rails will generate a file called products.html and the webserver will then look for that file before it passes the next request for /products to your Rails application.

By default, the page cache directory is set to Rails.public_path (which is usually set to the public folder) and this can be configured by changing the configuration setting config.action_controller.page_cache_directory. Changing the default from public helps avoid naming conflicts, since you may want to put other static html in public, but changing this will require web server reconfiguration to let the web server know where to serve the cached files from.

The Page Caching mechanism will automatically add a .html extension to requests for pages that do not have an extension to make it easy for the webserver to find those pages and this can be configured by changing the configuration setting config.action_controller.page_cache_extension.

In order to expire this page when a new product is added we could extend our example controller like this:

class ProductsController < ActionController
 
  caches_page :index
 
  def index
    @products = Products.all
  end
 
  def create
    expire_page :action => :index
  end
 
end

If you want a more complicated expiration scheme, you can use cache sweepers to expire cached objects when things change. This is covered in the section on Sweepers.

Advertisements

About me
Interested about Ruby on Rails

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: