Caching Static and Dynamic Pages in Rails 4.2.5

Steps

Step 1

In production.rb:

config.action_controller.perform_caching = true

Step 2

module ArticlesHelper
  def cache_key_for_articles
    count          = Article.count
    max_updated_at = Article.maximum(:updated_at).utc.to_s(:number)
    "articles/all-#{count}-#{max_updated_at}"
  end
end

Step 3

<% cache(cache_key_for_articles) do %>
  Article list
<% end %>

If you have a static page, for instance the home page, you can use:

<% cache do %>
  Static Home Page content
<% end %>

Summary

In this article, we saw how to cache static and dynamic pages using Rails fragment caching.


Related Articles


Create your own user feedback survey