Complete the code to cache the result of a method call in Rails.
Rails.cache.[1]('user_1') { User.find(1) }
The fetch method tries to read from cache and runs the block if the cache is empty, storing the result.
Complete the code to expire a cached fragment in Rails.
expire_fragment([1])The expire_fragment method takes the cache key string to remove the cached fragment.
Fix the error in the caching code to properly cache a view fragment.
<% cache [1] do %>
<p>Cached content here</p>
<% end %>The cache helper expects a string or cache key to identify the fragment. Quotes are needed for a string key.
Fill both blanks to create a cache key with a timestamp to expire cache after update.
cache_key = "users/[1]-#{User.maximum(:[2])}"
The cache key includes 'list' and the 'updated_at' timestamp to expire cache when users update.
Fill all three blanks to write a method that caches a user's profile with expiration.
def cached_profile(user) Rails.cache.[1]("profile_#{user.[2]", expires_in: [3]) do user.profile_data end end
The method uses fetch to get or store the cache, uses the user's id in the key, and sets expiration to 5 minutes.