Challenge - 5 Problems
Rails Cache Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Rails.cache.fetch block return?
Consider this code snippet in a Rails controller action:
value = Rails.cache.fetch('user_count', expires_in: 5.minutes) do
User.count
end
What will value contain if the cache is empty?Ruby on Rails
value = Rails.cache.fetch('user_count', expires_in: 5.minutes) do User.count end
Attempts:
2 left
💡 Hint
Think about what Rails.cache.fetch does when the key is missing.
✗ Incorrect
Rails.cache.fetch tries to read the key 'user_count'. If missing, it runs the block, caches the result (User.count), and returns it.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this Rails.cache.write usage
Which option shows the correct syntax to write a value to cache with a 10-minute expiration?
Ruby on Rails
Rails.cache.write('key', 'value', expires_in: 10.minutes)
Attempts:
2 left
💡 Hint
Check the exact option name for expiration time.
✗ Incorrect
The correct option is 'expires_in'. Other options like 'expire_in' or 'expires' are invalid and cause errors.
🔧 Debug
advanced2:00remaining
Why does this Rails.cache.fetch block never update the cached value?
Given this code:
Why does the cached value never change even after 1 minute?
Rails.cache.fetch('random_number', expires_in: 1.minute) do
rand(100)
endWhy does the cached value never change even after 1 minute?
Ruby on Rails
Rails.cache.fetch('random_number', expires_in: 1.minute) do rand(100) end
Attempts:
2 left
💡 Hint
Think about the cache store configuration and expiration support.
✗ Incorrect
Some cache stores like MemoryStore do not support expiration properly, so expires_in is ignored and cached value stays.
❓ state_output
advanced2:00remaining
What is the output of this Rails.cache.fetch with race_condition_ttl?
Consider this code snippet:
If two requests run this code at the same time after the cache expired, what will happen?
value = Rails.cache.fetch('expensive_calc', expires_in: 10.seconds, race_condition_ttl: 5.seconds) do
sleep 2
Time.now.to_i
end
puts valueIf two requests run this code at the same time after the cache expired, what will happen?
Ruby on Rails
value = Rails.cache.fetch('expensive_calc', expires_in: 10.seconds, race_condition_ttl: 5.seconds) do sleep 2 Time.now.to_i end puts value
Attempts:
2 left
💡 Hint
Race condition TTL helps avoid multiple recomputations at once.
✗ Incorrect
race_condition_ttl allows the old cached value to be served while one request recomputes the new value, preventing duplicate work.
🧠 Conceptual
expert2:00remaining
Which cache store supports distributed caching in Rails by default?
You want to share cached data across multiple Rails app servers. Which cache store should you choose?
Attempts:
2 left
💡 Hint
Distributed caching means multiple servers share the same cache data.
✗ Incorrect
MemCacheStore uses Memcached, a distributed cache system, allowing multiple app servers to share cached data.