Fragment caching helps speed up your web pages by saving parts of a page so they don't have to be rebuilt every time.
Fragment caching in Ruby on Rails
<% cache('cache_key') do %>
<!-- content to cache -->
<% end %>cache_key to identify the cached fragment.<% cache('menu') do %>
<nav>
<!-- menu items -->
</nav>
<% end %><% cache(['product', product.id]) do %>
<div><%= product.name %></div>
<% end %><% cache(['user', user, 'profile']) do %> <p><%= user.name %></p> <% end %>
This example caches the product details section so it doesn't get rebuilt every time the product page is viewed. The cache key includes the product id, so each product has its own cache. Expire the cache manually when the product changes.
<!-- app/views/products/show.html.erb -->
<h1>Product Details</h1>
<% cache(['product', @product.id]) do %>
<div>
<h2><%= @product.name %></h2>
<p><%= @product.description %></p>
</div>
<% end %>Cache keys should be unique and reflect the content they represent.
When the underlying data changes, you should expire or update the cache to show fresh content.
Fragment caching improves performance but adds complexity in cache management.
Fragment caching saves parts of a page to speed up rendering.
Use unique keys to cache different parts separately.
Remember to expire caches when data changes to keep content fresh.
Practice
Solution
Step 1: Understand fragment caching concept
Fragment caching stores parts of a webpage to avoid regenerating them every time.Step 2: Identify the main benefit
This speeds up page rendering by reusing cached parts instead of rebuilding them.Final Answer:
To save parts of a page so they load faster next time -> Option CQuick Check:
Fragment caching = save parts for speed [OK]
- Confusing caching with user authentication
- Thinking it compresses images
- Assuming it manages database connections
Solution
Step 1: Recall correct fragment caching syntax
The correct syntax usescache 'key' do ... endblock in ERB.Step 2: Check each option
<% cache 'unique_key' do %> ... <% end %>matches the correct block form. Others have wrong block usage or missingdo.Final Answer:
<% cache 'unique_key' do %> ... <% end %> -> Option AQuick Check:
Usecache 'key' doblock [OK]
- Omitting the 'do' keyword
- Using braces {} instead of do...end in ERB
- Placing the key inside the block instead of as argument
<% cache 'post_1' do %> <h1>Post Title</h1> <p>Post content here.</p> <% end %>
What happens if the post content changes but the cache is not expired?
Solution
Step 1: Understand cache behavior on data change
Fragment cache stores rendered content and does not update unless expired or cleared.Step 2: Identify effect of not expiring cache
If cache is not expired, Rails serves the old cached content even if data changed.Final Answer:
The page shows the old cached content, not the updated post -> Option BQuick Check:
Cache stale without expiry = old content shown [OK]
- Assuming cache auto-updates on data change
- Expecting an error when cache is stale
- Thinking cache is ignored automatically
<% cache 'user_42_profile' do %> <p>User profile info</p> <% end %>
But after updating user data, the cache still shows old info. What is the likely problem?
Solution
Step 1: Check cache key usage
Cache keys must change or be expired to reflect updated data.Step 2: Identify why old data shows
If key stays same and cache not expired, old cached content remains visible.Final Answer:
The cache key 'user_42_profile' was not changed or expired after update -> Option AQuick Check:
Cache key unchanged = stale cache shown [OK]
- Assuming syntax error causes stale cache
- Believing Rails disables caching for certain views
- Forgetting to use cache helper in views
Solution
Step 1: Understand caching granularity
When parts update independently, caching each part separately allows selective expiration.Step 2: Apply to comments list
Caching each comment with a unique key lets you expire only changed comments, keeping others cached.Step 3: Evaluate other options
Single static key caches whole list, causing stale data if one comment changes. Avoiding caching loses speed benefits.Final Answer:
Cache each comment fragment separately using a unique key per comment -> Option DQuick Check:
Separate keys for independent parts = correct caching [OK]
- Caching entire list causing stale comments
- Skipping caching due to fear of stale data
- Ignoring comment updates in cache strategy
Ready to prove it?
Pass the test and this pattern moves unknown
