0
0
Ruby on Railsframework~10 mins

Page and action caching in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Page and action caching
User Request Received
Check Page Cache
Yes / No?
Serve Cached
Response
Serve Cached
Response
Store Action Cache
Response
When a user requests a page, Rails first checks if a full page cache exists. If yes, it serves it immediately. If not, it checks for an action cache. If that exists, it serves it. Otherwise, it runs the controller action, generates the response, stores it in action cache, then serves it.
Execution Sample
Ruby on Rails
class ProductsController < ApplicationController
  caches_page :index
  caches_action :show

  def index
    @products = Product.all
  end

  def show
    @product = Product.find(params[:id])
  end
end
This Rails controller caches the entire index page and caches the show action output separately.
Execution Table
StepCache CheckedCache Hit?Action TakenResponse Served
1Page Cache for /productsNoCheck Action CacheNo response yet
2No action cache configured for indexN/ARun index actionNo response yet
3N/AN/AGenerate index page HTMLNo response yet
4N/AN/AStore page cache for /productsNo response yet
5N/AN/AServe generated index pageIndex page HTML served
6Page Cache for /productsYesServe cached pageIndex page HTML served
7Page Cache for /products/1NoCheck Action CacheNo response yet
8Action Cache for show id=1NoRun show actionNo response yet
9N/AN/AGenerate show page HTMLNo response yet
10N/AN/AStore action cache for show id=1No response yet
11N/AN/AServe generated show pageShow page HTML served
12Page Cache for /products/1NoCheck Action CacheNo response yet
13Action Cache for show id=1YesServe cached actionShow page HTML served
14Page Cache for /products/1NoCheck Action CacheNo response yet
💡 Execution stops after serving cached or generated response.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 8After Step 10Final
page_cache_indexemptyemptystored HTMLstored HTMLstored HTMLstored HTML
action_cache_show_1emptyemptyemptyemptystored HTMLstored HTML
Key Moments - 3 Insights
Why does Rails check page cache before action cache?
Page cache stores the full HTML page and is faster to serve. If it exists, Rails can skip running any controller code. This is shown in execution_table rows 1 and 2 where page cache is checked first.
What happens if page cache is missing but action cache exists?
Rails serves the cached action output without running the controller action. This is shown in execution_table rows 12-13 where page cache misses but action cache hits to serve the show page.
When is the cache stored during the request?
Cache is stored after generating the response, as seen in rows 4 and 10 where page and action caches are saved after running actions.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the page cache stored for the index page?
AStep 2
BStep 4
CStep 10
DStep 8
💡 Hint
Check the 'Action Taken' column for storing page cache related to index page.
At which step does Rails serve the cached action for show id=1?
AStep 11
BStep 8
CStep 13
DStep 14
💡 Hint
Look for 'Serve cached action' in the 'Action Taken' column for show id=1.
If the page cache for /products existed at the start, what would happen at step 1?
ARails would serve the cached page immediately
BRails would check action cache next
CRails would run the index action
DRails would generate a new page
💡 Hint
Refer to the 'Cache Hit?' and 'Action Taken' columns at step 1.
Concept Snapshot
Page and action caching in Rails:
- Page caching stores full HTML pages for instant serving.
- Action caching stores controller action output with filters.
- Rails checks page cache first, then action cache.
- If no cache, runs action and stores cache.
- Speeds up response by skipping controller processing.
Full Transcript
Page and action caching in Rails help speed up web responses. When a user requests a page, Rails first looks for a full page cache. If it finds one, it sends that cached page immediately without running any code. If not, it checks for an action cache, which stores the output of controller actions. If action cache exists, Rails serves it. Otherwise, Rails runs the controller action, generates the page, stores the cache, and then serves the response. This process reduces server work and makes pages load faster. The example controller caches the index page fully and caches the show action separately. The execution table shows step-by-step how Rails checks caches, runs actions, stores caches, and serves responses. Variables track when caches are empty or stored. Key moments clarify why page cache is checked before action cache, when caches are stored, and what happens if caches exist or not. The quiz tests understanding of cache storage and serving steps. This caching pattern is important for improving Rails app performance.