Caching giảm số lần tính toán/query tốn kém bằng cách lưu kết quả HTML.
Fragment cache — cache một đoạn view:
erb
<%# cache @article do %>
<%= render @article %>
<%# end %>Russian-doll cache — cache lồng nhau: outer cache bao inner cache.
Outer chỉ expired khi inner thay đổi.
erb
<%# cache @article do — outer: cache cả article %>
<h1><%= @article.title %></h1>
<%# cache @article.comments do — inner: cache list comments %>
<%= render @article.comments %>
<%# end %>
<%# end %>Khi 1 comment thay đổi → chỉ outer expired, inner comment cache vẫn còn. Cache key tự động từ cache_key_with_version (dùng updated_at).
Configure store: config.cache_store = :redis_cache_store, { url: ENV["REDIS_URL"] }.