0
0
Ruby on Railsframework~20 mins

Low-level caching with Rails.cache - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rails Cache Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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
AThe current count of User records from the database, and it caches this value for 5 minutes.
BAlways nil because the cache key 'user_count' does not exist yet.
CAn error because the block is missing a return statement.
DThe string 'User.count' is cached and returned as a string.
Attempts:
2 left
💡 Hint
Think about what Rails.cache.fetch does when the key is missing.
📝 Syntax
intermediate
2: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)
ARails.cache.write('key', value: 'value', expires_in: 10.minutes)
BRails.cache.write('key', 'value', expire_in: 10.minutes)
CRails.cache.write('key', 'value', expires_in: 10.minutes)
DRails.cache.write('key', 'value', expires: 10.minutes)
Attempts:
2 left
💡 Hint
Check the exact option name for expiration time.
🔧 Debug
advanced
2:00remaining
Why does this Rails.cache.fetch block never update the cached value?
Given this code:
Rails.cache.fetch('random_number', expires_in: 1.minute) do
  rand(100)
end

Why 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
AThe block is never executed because the key always exists in cache until manually deleted.
BThe expires_in option is ignored because it must be passed as a string, not a symbol.
CThe cache key is misspelled, so it fetches a different key each time.
DThe cache store does not support expiration, so the value stays forever.
Attempts:
2 left
💡 Hint
Think about the cache store configuration and expiration support.
state_output
advanced
2:00remaining
What is the output of this Rails.cache.fetch with race_condition_ttl?
Consider this code snippet:
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

If 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
ABoth requests recompute the value simultaneously causing duplicate work.
BThe first request recomputes and caches the new time; the second request returns the old cached value during recomputation.
CBoth requests return nil because the cache expired and block is not executed.
DThe code raises an error because race_condition_ttl is not a valid option.
Attempts:
2 left
💡 Hint
Race condition TTL helps avoid multiple recomputations at once.
🧠 Conceptual
expert
2: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?
AMemCacheStore
BFileStore
CMemoryStore
DNullStore
Attempts:
2 left
💡 Hint
Distributed caching means multiple servers share the same cache data.