0
0
Ruby on Railsframework~10 mins

Low-level caching with Rails.cache - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Low-level caching with Rails.cache
Call Rails.cache.fetch(key)
Check if key exists in cache
Return cached value
Store value in cache
Return new cached value
Rails.cache.fetch checks if a key exists. If yes, it returns cached data. If no, it runs the block, stores the result, then returns it.
Execution Sample
Ruby on Rails
value = Rails.cache.fetch('greeting') do
  'Hello, world!'
end
puts value
This code tries to get 'greeting' from cache; if missing, it stores and returns 'Hello, world!'.
Execution Table
StepActionCache Key Exists?Block Executed?Cache Store Updated?Returned Value
1Call Rails.cache.fetch('greeting')NoYesYes'Hello, world!'
2Call Rails.cache.fetch('greeting') againYesNoNo'Hello, world!'
💡 Execution stops after returning cached value on second fetch call.
Variable Tracker
VariableStartAfter Step 1After Step 2
cache['greeting']nil'Hello, world!''Hello, world!'
valuenil'Hello, world!''Hello, world!'
Key Moments - 2 Insights
Why does the block run only the first time?
Because in step 1, the cache key 'greeting' does not exist, so the block runs and stores the value. In step 2, the key exists, so the block is skipped (see execution_table rows 1 and 2).
What happens if the cache key expires or is cleared?
Rails.cache.fetch will treat it like a missing key, run the block again, store the new value, and return it, similar to step 1 in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the returned value at step 2?
ABlock content not returned
B'Hello, world!'
Cnil
DError
💡 Hint
Check the 'Returned Value' column in execution_table row 2.
At which step does the cache store get updated?
AStep 2
BBoth steps
CStep 1
DNever
💡 Hint
Look at 'Cache Store Updated?' column in execution_table.
If the cache key 'greeting' is deleted before step 2, what happens?
ABlock runs again and cache updates
BBlock is skipped and nil returned
CError is raised
DOld cached value returned
💡 Hint
Refer to key_moments about cache expiration and step 1 behavior.
Concept Snapshot
Rails.cache.fetch(key) checks cache for key.
If found, returns cached value immediately.
If missing, runs block, stores result, returns it.
Blocks run only when cache miss occurs.
Useful for fast repeated data access.
Full Transcript
Low-level caching with Rails.cache uses the fetch method to check if a cache key exists. When you call Rails.cache.fetch with a key, it first looks in the cache. If the key is found, it returns the cached value without running the block. If the key is not found, it runs the block, stores the block's result in the cache under that key, and returns the new value. This means the block runs only once per cache miss. Subsequent calls with the same key return the cached value quickly. If the cache is cleared or the key expires, the block runs again to refresh the cache. This pattern helps speed up applications by avoiding repeated expensive operations.