Russian doll caching in Rails

AppSignal
AppSignal
Apr 3, 2018 · 4 min read

Product variants

# app/views/products/index.html.erb
<h1>Products</h1>

<% @products.each do |product| %>
<% cache product do %>
<%= render product %>
<% end %>
<% end %>
# app/views/products/_product.html.erb
<article>
<h1><%= product.title %></h1>

<ul>
<% product.variants.each do |variant| %>
<%= render variant %>
<% end %>
</ul>
</article>

Cache invalidation

class Variant < ApplicationRecord
belongs_to :product, touch: true
end

Nested fragments

<article> 
<h1><%= product.title %></h1>
<ul>
<% product.variants.each do |variant| %>
<% cache(variant) do %>
<%= render variant %>
<% end %>
<% end %>
</ul>
</article>
Started GET "/products" for 127.0.0.1 at 2018-03-30 14:51:38 +0200
Processing by ProductsController#index as HTML
Rendering products/index.html.erb within layouts/application
Product Load (0.2ms) SELECT "products".* FROM "products"
Variant Load (0.9ms) SELECT "variants".* FROM "variants" WHERE "variants"."product_id" IN (27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51)
Rendered variants/_variant.html.erb (0.5ms)
Rendered variants/_variant.html.erb (0.1ms)
Rendered variants/_variant.html.erb (0.1ms)
Rendered variants/_variant.html.erb (0.0ms)
Rendered variants/_variant.html.erb (0.1ms)
Rendered variants/_variant.html.erb (0.1ms)
Rendered variants/_variant.html.erb (0.1ms)
Rendered variants/_variant.html.erb (0.1ms)
Rendered variants/_variant.html.erb (0.1ms)
Rendered variants/_variant.html.erb (0.1ms)
Rendered products/_product.html.erb (44.8ms) [cache miss]
...
Rendered variants/_variant.html.erb (0.1ms)
Rendered variants/_variant.html.erb (0.1ms)
Rendered variants/_variant.html.erb (0.1ms)
Rendered variants/_variant.html.erb (0.1ms)
Rendered variants/_variant.html.erb (0.1ms)
Rendered variants/_variant.html.erb (0.1ms)
Rendered variants/_variant.html.erb (0.1ms)
Rendered variants/_variant.html.erb (0.1ms)
Rendered variants/_variant.html.erb (0.1ms)
Rendered variants/_variant.html.erb (0.1ms)
Rendered products/_product.html.erb (46.2ms) [cache miss]
Rendered products/index.html.erb within layouts/application (1378.6ms)
Completed 200 OK in 1414ms (Views: 1410.5ms | ActiveRecord: 1.1ms)
Started GET "/products" for 127.0.0.1 at 2018-03-30 14:51:41 +0200
Processing by ProductsController#index as HTML
Rendering products/index.html.erb within layouts/application
Product Load (0.3ms) SELECT "products".* FROM "products"
Variant Load (12.7ms) SELECT "variants".* FROM "variants" WHERE "variants"."product_id" IN (27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51)
Rendered products/index.html.erb within layouts/application (48.1ms)
Completed 200 OK in 76ms (Views: 59.0ms | ActiveRecord: 13.0ms)
Started GET "/products" for 127.0.0.1 at 2018-03-30 14:52:04 +0200
Processing by ProductsController#index as HTML
Rendering products/index.html.erb within layouts/application
Product Load (0.3ms) SELECT "products".* FROM "products"
Variant Load (1.2ms) SELECT "variants".* FROM "variants" WHERE "variants"."product_id" IN (27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51)
Rendered variants/_variant.html.erb (0.5ms)
Rendered products/_product.html.erb (13.3ms) [cache miss]
Rendered products/index.html.erb within layouts/application (45.9ms)
Completed 200 OK in 78ms (Views: 73.5ms | ActiveRecord: 1.5ms)ered in its entirety, unless the cache is completely empty. Even when the data changes, most of the rendered pages are served straight from the cache.

AppSignal

Written by

AppSignal

Error tracking and performance insights for Ruby and Elixir without the silly per-host pricing. From Amsterdam with love.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade