Rendering a page in Rails isn't a simple process. It usually consists of several database queries that render various partials and convert HAML to HTML. Repeating this entire process for each page load is wasteful. In this recipe, you will learn how to cache your views so that you only have to render them once. You'll also learn how to use key-based cache expiration to ensure that you're always serving the latest content.
In this recipe, we'll use the Blogger application as an example to implement view caching. This time, we will optimize posts#show
using caching to make viewing a post as fast as possible.
Note
Make sure that caching is enabled in config/environments/development.rb
and config.action_controller.perform_caching
is set to true
.
Currently, if we try to view a post, our application does the following to render the page:
Started GET "/posts/2" for 127.0.0.1 at 2014-08-09 14:29:06 -0400 Processing by PostsController#show as HTML Parameters: {"id...