Performance: Page and action caching
Page and action caching mainly affect how fast the server can respond and how quickly the browser receives fully rendered content.
caches_page :show
def show
@post = Post.find(params[:id])
enddef show
@post = Post.find(params[:id])
render :show
end| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No caching, full render each request | High (new DOM each time) | Multiple reflows per request | High paint cost | [X] Bad |
| Page caching serving static HTML | Low (cached DOM reused) | Single reflow on load | Low paint cost | [OK] Good |
| Action caching with dynamic cache keys | Medium (cached per param) | Few reflows | Medium paint cost | [!] OK |
page caching and action caching in Rails?index action in a Rails controller?caches_page followed by the action name symbol to enable page caching.caches_page :index. Other options are invalid method names.class ProductsController < ApplicationController
caches_action :show
before_action :authenticate_user!
def show
@product = Product.find(params[:id])
end
endshow page is requested by a user who is not authenticated?before_action before serving cached content.authenticate_user! filter runs before serving the cached show page, so unauthenticated users are redirected.caches_page :index to your controller but the page does not seem cached. Which of the following is the most likely cause?public/ folder, served directly by the web server.public/, cached pages won't be served.public/ directory. -> Option Cshow action of a controller but only for logged-in users, and you want filters like authentication to run before serving cached content. Which caching method should you use and how?caches_action :show in the controller to cache the action output with filters.caches_action :show to cache with filters running before serving. -> Option BPass the test and this pattern moves unknown