View Caching
A decade of "put Redis in front of everything" advice has left view caching with a reputation for complexity it no longer deserves. Rails 8 ships a database-backed cache store by default, Hotwire has moved rendering back to the server where fragments can be reused, and ViewComponent has finally learned how to participate. This guide covers the modern toolkit — fragment, collection, and HTTP caching, component caching, the Turbo interplay, and the stores — and, just as importantly, when to leave a view uncached.
1. Why Now
Two things changed. Solid Cache is the default cache store for new Rails 8 applications: it keeps entries in a database table, so there is no Redis or Memcached to provision, and because disk is cheap the cache can be enormous and long-lived — entries stay warm for days instead of being evicted by lunchtime. Hotwire moved rendering back to the server. A Turbo app answers every navigation, frame load, and stream with server-rendered HTML, which means the same fragments are generated over and over for every visitor. That is exactly the workload a fragment cache exists for.
The trade has not changed: a cache turns a rendering problem into an invalidation problem. Everything below is about keeping the second problem small.
2. Fragment and Russian-Doll Caching
The cache helper wraps a block of view logic. The first request
renders it and writes the HTML to the store; every later request reads
it back. The key combines the record's cache_key_with_version with a
digest of the template tree, so editing the template (or any partial
it renders) busts the entry without you doing anything:
-# app/views/products/index.html.haml
- @products.each do |product|
- cache product do
= render productRussian-doll caching is nesting: an outer cache for the list, inner
caches for each item. When one item changes, only its own fragment and
the outer fragment re-render — the other items are read straight out of
the cache and stitched in. For that to work the parent's version has to
change when a child does, which is what touch: true is for:
# app/models/review.rb
class Review < ApplicationRecord
belongs_to :product, touch: true # a new or edited review bumps the product's updated_at
end-# app/views/products/show.html.haml
- cache @product do
%h1= @product.name
- @product.reviews.each do |review|
- cache review do
= render reviewcache_if and cache_unless take a condition first, for fragments
that should only be cached for some visitors — the classic case is a
fragment that renders admin controls:
- cache_unless current_user&.admin?, product do
= render product3. Collection Caching
Wrapping each item of a list in its own cache block works, but it
reads the cache once per item. Rendering the collection with
cached: true fetches every cached entry in one multi-read, renders
only the misses, and writes those back for next time:
= render partial: "products/product", collection: @products, cached: trueTwo caveats. The whole collection must render through one partial (Rails needs a single template digest for the batch), so a list that switches partials per item cannot use it. And the cache key is the item alone by default: if the rendering depends on anything else — the locale, the viewer's role — fold it into the key with a lambda, or pass the options hash for an expiry:
= render partial: "products/product", collection: @products,
cached: ->(product) { [I18n.locale, product] }
= render partial: "products/product", collection: @products,
cached: { expires_in: 1.hour, key: ->(product) { [I18n.locale, product] } }4. Cache Keys and Versioning
Modern Rails uses recyclable cache keys. A record's cache_key is
stable (products/42), and its version — derived from updated_at —
is stored inside the entry. When the product is touched, the version no
longer matches and the entry is overwritten in place rather than
orphaned under a new key. That is why touch: true is the whole
invalidation strategy for most apps: bump updated_at and every
fragment that depends on the record is stale.
Anything else the fragment depends on has to be in the key explicitly. Pass an array — records, strings, whatever changes the output:
-# The fragment shows a price in the visitor's currency and differs for admins.
- cache [product, current_currency, current_user&.admin?] do
= render productThe template tree digest covers the other half: Rails scans each
template for render calls and hashes the whole tree, so editing a
nested partial invalidates every fragment that includes it. It can only
see literal render calls, though. When a render hides inside a helper
method, or the partial name is built at runtime, declare the dependency
with a comment so the digest stays honest:
-# Template Dependency: todolists/todolist
= render_sortable_todolists @project.todolists
-# Template Dependency: events/*
= render_categorizable_events @person.events5. HTTP and Conditional Caching
Fragment caching saves rendering; conditional GET saves the whole
response. fresh_when sets ETag and Last-Modified headers from a
record, and when the browser sends them back on the next request and
nothing has changed, Rails answers 304 Not Modified with an empty
body — no view is rendered at all:
# app/controllers/products_controller.rb
def show
@product = Product.find(params[:id])
fresh_when @product # ETag + Last-Modified from cache_key_with_version and updated_at
endstale? is the same check as a conditional, for actions that need to
do more than render when the record has changed. And expires_in sets
Cache-Control so a browser or CDN can serve the response without
asking at all — reserve it for genuinely public, slow-changing pages:
def show
@product = Product.find(params[:id])
if stale?(@product)
respond_to do |format|
format.html
format.json { render json: @product }
end
end
end
def pricing
expires_in 1.hour, public: true
endRails generates weak ETags by default and mixes in the template
digests, so an ETag changes when the layout does. Anything else that
varies the page — the current user, a flash message — must be folded in
with fresh_when @product, etag: [current_user, flash], or the browser
will happily show one user another's 304.
6. Caching ViewComponents
This stack renders through ViewComponent, and components have a
caching blind spot: the template tree digest is built from templates,
and a component is a Ruby class. Wrap a component render in a cache
block and editing the component's template, class, or sidecar files
does not invalidate the fragment — stale markup ships until the
cache is cleared by hand.
ViewComponent 4.14 fixes this with ViewComponent::ExperimentallyCacheable.
Include it in a component and the component joins Rails' digest tree:
the surrounding fragment is invalidated when the component's template,
Ruby class, sidecar files, superclasses, child components, or rendered
partials change.
# app/components/product_card_component.rb
class ProductCardComponent < ApplicationComponent
include ViewComponent::ExperimentallyCacheable
def initialize(product:)
@product = product
end
end- cache product do
= render ProductCardComponent.new(product: product)A component can also cache itself, so every call site is cached
without a cache block. cache_on names the methods whose values form
the key; the digest, format, variant, and locale are added for you, and
if: / unless: skip the cache for some renders:
class ProductCardComponent < ApplicationComponent
include ViewComponent::ExperimentallyCacheable
cache_on :product, unless: -> { product.draft? }
def initialize(product:)
@product = product
end
private
attr_reader :product
endA block, with_content, or a slot filled by the caller is not part of
the cache key, so passing one to a cache_on component raises
ContentPassedToCachedComponentError — in development and test too,
not just when caching is on. Move the content into the component and
derive it from cache_on values, and declare cache_on on the
components that should self-cache, never on ApplicationComponent
(it is inherited). Note the API is marked experimental and may
change in a minor release.
Two more things the digest cannot see: a component rendered from a
runtime value (render @component) and a module included into a
component (modules have no template to hash). Declare both with the
same Template Dependency: comment, naming components by class. And
when a cache is written somewhere the component isn't rendered — a
background job precomputing a summary — ProductCardComponent.cache_digest
ties the entry to the component's source.
7. Turbo and Hotwire
Fragment caches and Turbo are good neighbours because Turbo only ever
asks for HTML. A Turbo Frame that lazy-loads (src: with
loading: "lazy") is a separate GET, so the frame's action gets
fresh_when and its template gets cache blocks like any other page —
and because a lazy frame is often the slow, reusable part of a page (a
sidebar, a recommendations panel), it is usually the best place to
cache. Turbo Streams broadcast partials, and a cache block inside
a broadcast partial behaves exactly as it does in a request. Page
refreshes with morphing re-render the whole page on every change,
which is precisely when Russian-doll caching pays off: only the changed
fragment costs anything.
The one rule: keep per-visitor markup out of cached fragments. A
CSRF token, a "signed in as" badge, or a Stimulus target that depends
on the current user inside a cache block will be served to the next
visitor. Render those outside the block, or key the block on the user.
8. Stores and Configuration
Everything above writes through Rails.cache, and the store behind it
is a one-line choice per environment.
| Store | Good for | The catch |
|---|---|---|
Solid Cache
| The Rails 8 default. Database-backed, durable, large, and shared by every process and server with no extra service to run. | Reads are a database query, so it is slower than memory — fine for HTML fragments, wrong for a hot counter. |
:memory_store
| Development and tests: zero setup, per-process. | Not shared between processes or servers; disappears on restart. |
:redis_cache_store
| Sub-millisecond reads when you already run Redis for something else. | One more service to operate, patch, and pay for; Solid Cache exists so most apps don't need it. |
:null_store
| Development with caching switched off — every | Nothing is ever cached, which is the point. |
A new Rails 8 app already has Solid Cache wired up; an existing app
adds the gem and runs its installer, which writes the migration,
config/cache.yml, and the cache database entry. Then point
production at it:
bundle add solid_cache
bin/rails solid_cache:install
# config/environments/production.rb
config.cache_store = :solid_cache_store# config/cache.yml
default: &default
store_options:
max_age: <%= 60.days.to_i %> # retention ceiling
max_size: <%= 256.megabytes %> # FIFO eviction past this
namespace: <%= Rails.env %>In development caching is off by default so you see every change
immediately. Rails toggles it with bin/rails dev:cache, which creates
or removes tmp/caching-dev.txt; LocoMotion's demo wraps the same file
in just demo-cache / just demo-nocache. Turn it on when you are
working on cache behaviour, off when you are working on views.
bin/rails dev:cache
Development mode is now being cached.
9. When Not to Cache
Every cache block is a place where the page can be wrong. It earns that risk only when the fragment is slow to render, shown often, and the same for many visitors. Skip it when:
- The fragment is cheap. A few dozen tags with no queries render faster than the cache read that would replace them.
- The fragment is personalised. If the key has to include the current user, the hit rate is one — you are storing a copy of every page for every visitor and reusing none of them.
- The data changes more often than it is viewed. A live dashboard invalidates faster than it can be read back.
- You have not measured. Profile the page first — the slow part is usually a query, and a query fix helps every visitor without adding an invalidation problem to maintain.
Success!
You know the modern caching toolkit and, more importantly, where each piece belongs. Before you move on: