Page and action caching help your website load faster by saving copies of pages or actions. This means the server can quickly show saved content instead of making it again every time.
Page and action caching in Ruby on Rails
caches_page :index caches_action :show
caches_page saves the entire HTML output of a page.
caches_action saves the result of a controller action, which can be more flexible.
class ProductsController < ApplicationController
caches_page :index
endclass ArticlesController < ApplicationController
caches_action :show
endclass HomeController < ApplicationController
caches_page :welcome
caches_action :dashboard
endThis example caches the full HTML of the posts index page. When users visit again, Rails serves the saved page quickly without running the index code again.
class PostsController < ApplicationController caches_page :index def index @posts = Post.all end end
Page caching works best for static content but is less flexible if you need to show different content to different users.
Action caching allows filters and authentication to run before serving cached content.
Remember to expire caches when content changes, so users see fresh data.
Page caching saves the full HTML page for fast loading.
Action caching saves the controller action output and can run filters before serving.
Use caching to improve speed but manage cache expiration carefully.
Practice
page caching and action caching in Rails?Solution
Step 1: Understand what page caching does
Page caching saves the entire HTML page as a static file, so the web server can serve it directly without hitting Rails.Step 2: Understand what action caching does
Action caching saves the output of a controller action but still runs filters like authentication before serving cached content.Final Answer:
Page caching saves the full HTML page, while action caching saves the controller action output with filters. -> Option DQuick Check:
Page caching = full HTML, action caching = action output + filters [OK]
- Confusing page caching with action caching filters
- Thinking action caching saves static files
- Assuming page caching runs controller filters
index action in a Rails controller?Solution
Step 1: Recall Rails page caching syntax
Rails uses the methodcaches_pagefollowed by the action name symbol to enable page caching.Step 2: Verify the correct method name
The correct method iscaches_page :index. Other options are invalid method names.Final Answer:
caches_page :index -> Option AQuick Check:
Page caching method = caches_page [OK]
- Using 'cache_page' instead of 'caches_page'
- Confusing page caching syntax with action caching
- Adding parentheses incorrectly
class ProductsController < ApplicationController
caches_action :show
before_action :authenticate_user!
def show
@product = Product.find(params[:id])
end
endWhat happens when a cached
show page is requested by a user who is not authenticated?Solution
Step 1: Understand action caching behavior with filters
Action caching runs controller filters likebefore_actionbefore serving cached content.Step 2: Apply to authentication filter
Theauthenticate_user!filter runs before serving the cachedshowpage, so unauthenticated users are redirected.Final Answer:
The user is redirected to login because filters run before serving cached content. -> Option AQuick Check:
Action caching runs filters before cache [OK]
- Thinking cached page skips authentication
- Assuming cache serves static files directly
- Believing cache is ignored if filters exist
caches_page :index to your controller but the page does not seem cached. Which of the following is the most likely cause?Solution
Step 1: Understand how page caching serves cached pages
Page caching saves static HTML files in thepublic/folder, served directly by the web server.Step 2: Check web server configuration
If the web server (like Nginx or Apache) is not set to serve files frompublic/, cached pages won't be served.Final Answer:
Your web server is not configured to serve cached files frompublic/directory. -> Option CQuick Check:
Web server must serve public/ for page caching [OK]
- Assuming Rails serves cached pages directly
- Clearing cache without checking server config
- Thinking skip_before_action is needed for caching
show 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?Solution
Step 1: Identify caching method that runs filters
Action caching runs filters like authentication before serving cached content, suitable for logged-in user pages.Step 2: Apply correct syntax for action caching
Usecaches_action :showin the controller to cache the action output with filters.Final Answer:
Usecaches_action :showto cache with filters running before serving. -> Option BQuick Check:
Action caching = cache + filters [OK]
- Choosing page caching which skips filters
- Confusing fragment caching with action caching
- Thinking caching can't work with authentication
Ready to prove it?
Pass the test and this pattern moves unknown
