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:
<%# 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.
<%# 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"] }.
Caching reduces expensive computation/queries by storing rendered HTML output.
Fragment cache — cache a portion of a view:
<%# cache @article do %>
<%= render @article %>
<%# end %>Russian-doll cache — nested caching: outer cache wraps inner caches.
Outer expires only when inner changes.
<%# cache @article do — outer: caches the whole article %>
<h1><%= @article.title %></h1>
<%# cache @article.comments do — inner: caches comment list %>
<%= render @article.comments %>
<%# end %>
<%# end %>When one comment changes → only the outer is expired; inner comment caches remain valid. Cache keys are auto-generated from cache_key_with_version (uses updated_at).
Configure store: config.cache_store = :redis_cache_store, { url: ENV["REDIS_URL"] }.