Caching in ROR

When you start developing any ROR application you will not even care to think of things like caching, but as your application go live with more and more users starting to use the application, you will surely feel the need for 'Caching'.

 

Ruby is "Interpreted Programming Language" which means the code does not get translated into machine code (the language, computer understands) until someone actually runs it. Now imagine, your application handling more than 100 requests a second can take great deal of processor power. Here, need for speeding up the application is felt.

Caching is the process of temporarily storing the data likely to be used again and again. It is the art of taking a processed web page (or part of a webpage), and storing it in a temporary location. If the same webpage is requested again by any user, then we can serve up the cached version.

Caching is the cheap and best way of speeding up the slow applications.

 

The ActionController::Caching module is responsible for implementing caching in ROR applications.

It supports three variations called

Page Caching

Action Caching &

Fragment Caching

 

How to Configure??

If you are working on development environment, then you need to enable caching in environment/development.rb file.

Search for the following line and set it to true:

config.action_controller.perform_caching = true

Page Caching

Page caching is suitable for the pages -

  • Where no authentication is required
  • Where the page content remains same / static

Module

ActionController::Caching::Pages

Methods

  • cache_page
  • expire_page

Implementation

Action to be cached should be specified in the controller using the method cache_page like:

class UserController < ApplicationController
caches_page :list
def list
Post.find(:all, :order => "created_on desc", :limit => 10)
end
end

This will generate a cache files and will be used the next time, same page is called.

To expire the cached pages so that it will reflect the new changes, expire_page method is used like:

   expire_page(:controller => 'user', :action => 'list')


The above statement will clean up the cache for the specified controller and corresponding action, such that the next time the same page is called, page gets freshly cached.

A better way to expire the cached pages is to create sweepers, they are the piece of code that gets executed when a model gets changed and it accordingly deletes the old cached pages.

 

Action Caching

Action caching is similar to page caching as it caches the whole page as well but this time the request still passes through the controller running filters and checking authentication.

Module

ActionController::Caching::Actions

Methods

  • caches_action
  • expire_action

Implementation

Action caching is implemented exactly yhe way we do page caching, as shown below

class UserController < ApplicationController
before_filter :authenticate, :except => :public
caches_action :show, :feed
end

and to expire the cached action:

 expire_page(:controller => 'user', :action => 'list')

Note : Action caching internally uses the fragment caching

 

Fragment Caching

Caching whole page may not be feasible and even possible in conditions where some of the fragment of page is static while the other is highly dynamic.

To implement caching in such situation we cache just fragments or part of pages, called fragment caching.

Methods

ActionController::Caching::Fragments

Implementation

Part/fragment caching can be implemented as below:

<b>Hello <%= @login_name %></b>
<% cache do %>
Your details are:
<%= render :partial => "details", :collection => Detail.find(:all) %>
<% end %>

Similarly, to expire the cached fragment we will do:

  expire_fragment(:controller => "topics", :action => "list")


Through this write up, we have just introduced ourselves with the world of caching in ROR. But with this, I can surely conclude that caching plays a pretty vital role in the ROR framework.