Why we required caching?
– Caching is one of the technique to speed up your application.
What different types of caching available in rails?
There are 3 different types of caching available in ruby on rails.
- Page Caching
- Action Caching
- Fragement Caching
There are 4 different types of storage for cached data.
- Memory Store – store cached data in form of simple hashes.
- File Store – store cached data in files.
- DRb Store – use DRb server to store cached date in form of simple hash.
- Memcached Store – store cached data in shared memory cache (memcache).
How to implement caching with rails and importance of different storage.
First you have to enable caching for application by placing this on your config/environments/development.rb
config.action_controller.perform_caching = true
Default cache page is store in public directory of application. If you need to change directory for caching process than use
config.action_controller.page_cache_directory = “/tmp”
Define the storage for cached page/action/fragement.
config.cache_store = :memory_store # for memory storage
config.cache_store = :file_store, ‘/tmp’ # for file storage and specify directory
config.cache_store = :mem_cache_store, ‘localhost’ # for mem_cache storage
Lets, we start with simple caching.
1. Page Caching
From the 3 different caching facility Page caching is one of the most simplest and fastest technique.
Page caching is an approach to caching where the entire action output of is stored as a HTML file that the web server can serve without going through rails.
Two methods:
caches_page : for caching the page.
expire_page : for expiring cached page.
Below is the code to implement page caching.
class SectionsController < ApplicationController
caches_page :index
def index
………..
end
def create/update
expire_page :action => :index
end
end
Now cache page is store in your public and with action name eg. index.html
As it’s simple than always have some limitations:
– Filtering problem like authentication on page
– Query string like ?page=2
– If there are multiple place where you required cached page than it’s turn into complex sweeping
2. Action Caching
Action cache is used when you need to cache response of the action unlike the page caching. When action cache is invoke it access rails controller.
Two methods:
caches_action : for caching the action.
expire_action : for expiring the cached action.
Below is the code to implement action caching.
class SectionsController < ApplicationController
caches_action :show, :expires_in => 1.hour
def show
end
end
Above code is cache the show action and expire every 1 hour duration.
Limitation of action cache:
– Slower than page caching.
– Query string problem.
3. Fragement Caching
Last and more useful caching is fragement caching. It’s use when we need to cache particular fragement/block of page.
There are several methods:
expire_fragement : for expiring the cached fragement.
fragement_exist? : check the existance of fragement.
read_fragement(key) : read content from specified fragement.
write_fragement(key, content) : write the fragement by specifing key and the content which you need to write.
Below is the code to implement fragement caching.
sections/index.html.erb
<% cache :key => “set_section” do
unless @sections.blank?
@sections.each do |s| %>
<%= s.name %>
<%= s.description %>
<% end
end
end
%>
Above code, i just cache the sections listing fragement and give the key as “set_section”.
Now need to expire this fragemented cache by
expire_fragement :key => “set_section”
Once you cache any page or action or fragment, need to expire for that we are using sweeper of rails.
There are pre-built method of the rails available for this. here is the example:
class TestSweeper < ActionController::Caching::Sweeper
#observe model
observe Section
def after_save
expire_page :controller => “sections”, :action => “index” # expire cached page
expire_action :controller => “sections”, :action => “show” # expire cached action
expire_fragement(“set_section”) # expire cached fragement
end
end
Invoke the sweeper from the controller by
class SectionsController < ApplicationController
caches_page :index
caches_action :show
cache_sweeper :test_sweeper
def index
end
def show
end
end
Hope this article will help. If have any query or suggestion than post comment.