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


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.