Home » Ruby on Rails Caching

Ruby on Rails Caching

by Online Tutorials Library

Rails Caching

Caching is used in any web technologies. It speeds up the performance by storing previous results for subsequent requests. By default, cahcing is disabled in Rails.

Rails caching is available at three levels of granularity:

  • Page
  • Action
  • Fragment

Page Caching

Rails page caching is a technique where entire action output is stored as an HTML file. Due to this, web server can serve the output without going through Rails to call the action again. As a result, it improves the performance by 100 times by generating content dynamically. But this technique is only helpful for stateless pages that do not differentiate between application users.

Page caching can be turned on for any mehtod in controller classes by using caches_ page method call. Actions are passed that need to be cached as parameters to cache_page. There is no need to include all your controller’s action.

Example:

Step 1 Create an application MyCache -T

Step 2 Turn on the cache by running following command. This command creates an empty caching-dev.txt file inside tmp directory.

Step 3 Go to config/environments/development.rb file and write following code:

Step 4 Go to Gemfile, add following line.

Step 5 Run bundle install.

Step 6 Write following code in config/application.rb file. It will specify the location where you want to save your cached pages.

Step 7 We will introduce a controller here. Run following command:

Step 8 In app/page_controller.rb file, write following code.

Step 9 We will introduce a controller here. Run following command:

Step 10 Go to views/page/index.html.erb file.

Step 11 Page caching is enabled by caches_page method. Go to controller file and write following code:

Step 12 Start the server.

Step 13 Run it on localhost.

Ruby on rails Caching 1


Action Caching

Rails action caching saves entire output of an action reponse like page caching. There is only one difference that is with action caching, action calls are still routed to the controller so that any filter can still be applied. Action caching can be turned on for any mehtod in controller classes by using caches_ action method call.

Example:

We will continue with the example above.

Step 14 Go to Gemfile, write following code.

Step 15 Run bundle install.

Step 16 To use action caching, let’s create a new restricted page. Write following code, in controllers file.

Step 17 Create app/views/page/restricted.html.erb file.

Step 18 Go to config/routes.rb file and write following code.

Step 19 Start the server.

Step 20 Run it on localhost.

Ruby On rails Caching 2


Fragment Caching

Rails fragment caching is used to cache blocks within templates rather than caching entire output of an action method. It is useful where you need to change certain parts of an action frequently which can’t be cached while other parts need to be cached as they remain static.

It is done in view templates instead of controller classes. A fragment cache is designated with a cache_do block. The line inside the block enclosed in the cache_do statement will be cached.

Example:

We will continue with the example above.

Step 21 Create a new model called frag.

Step 22 Run migrate command.

Step 23 Go to app/controllers/frags_controller.rb file.

Step 24 Go to config/routes.rb file. Add following line.

Step 25 Go to app/views/frags/index.html.erb file.

Step 26 Go to db/seeds.rb file to populate fragment table.

Step 27 Run the rake command.

Step 28 If we want to cache each fragment listed on a page, it will be done using cache method.

Go to app/views/frags/index.html.erb file.

Step 29 When an object is passed to cache method, it automatically takes its id and generate a proper cache key. The cache will automatically expire if fragment was updated.

Use render method with cached option in app/views/frags/index.html.erb file.


Download

Download this example

You may also like