0
0
Ruby on Railsframework~10 mins

Fragment caching in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Fragment caching
Render View
Check Cache for Fragment
Use Cached
Insert Cached
Continue Rendering
Send Response
Rails checks if a cached fragment exists. If yes, it uses it. If no, it renders and caches the fragment before continuing.
Execution Sample
Ruby on Rails
<% cache('menu') do %>
  <ul>
    <li>Home</li>
    <li>About</li>
  </ul>
<% end %>
This code caches the HTML list inside the cache block under the key 'menu'.
Execution Table
StepActionCache Check ResultFragment RenderedCache UpdatedOutput
1Start rendering viewN/ANoNoNo output yet
2Check cache for key 'menu'Miss (not found)NoNoNo output yet
3Render fragment inside cache blockN/AYesNo<ul><li>Home</li><li>About</li></ul>
4Store rendered fragment in cache with key 'menu'N/AYesYes<ul><li>Home</li><li>About</li></ul>
5Continue rendering rest of viewN/AYesYes<ul><li>Home</li><li>About</li></ul> + rest of view
6Next request: Check cache for key 'menu'Hit (found)NoNo<ul><li>Home</li><li>About</li></ul> from cache
7Insert cached fragment, skip renderingHit (found)NoNo<ul><li>Home</li><li>About</li></ul> from cache
8Continue rendering rest of viewN/ANoNo<ul><li>Home</li><li>About</li></ul> from cache + rest of view
💡 Execution stops after full view is rendered and response sent.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
cache['menu']nilnil<ul><li>Home</li><li>About</li></ul><ul><li>Home</li><li>About</li></ul><ul><li>Home</li><li>About</li></ul>
Key Moments - 2 Insights
Why does Rails skip rendering the fragment on the second request?
Because the cache check at step 6 finds the fragment stored at step 4, so it uses the cached HTML instead of rendering again.
What happens if the cache key changes?
Rails treats it as a cache miss, so it renders the fragment again and stores it under the new key, as shown by the cache miss at step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the cache check result at step 6?
AHit (found)
BMiss (not found)
CError
DNot checked
💡 Hint
Check the 'Cache Check Result' column at step 6 in the execution_table.
At which step is the fragment first stored in the cache?
AStep 6
BStep 2
CStep 4
DStep 8
💡 Hint
Look at the 'Cache Updated' column to find when the cache is updated.
If the cache key changes, what will happen at the cache check?
ACache hit and fragment reused
BCache miss and fragment rendered again
CCache cleared automatically
DError thrown
💡 Hint
Refer to the key_moments explanation about cache key changes causing misses.
Concept Snapshot
Fragment caching in Rails:
Use <% cache(key) do %> ... <% end %> to cache parts of views.
Rails checks cache for the key before rendering.
If found, cached HTML is used directly.
If not, fragment renders and stores in cache.
Speeds up repeated view rendering by reusing HTML.
Full Transcript
Fragment caching in Rails helps speed up rendering by saving parts of the view as HTML in cache. When Rails renders a view with a cache block, it first checks if the cached fragment exists for the given key. If it does, Rails inserts the cached HTML directly, skipping rendering. If not, Rails renders the fragment, stores it in cache, then continues rendering the rest of the view. On subsequent requests, the cached fragment is reused until the cache expires or is cleared. This reduces repeated work and improves performance.