In a Rails app, why does caching help reduce the number of database queries?
Think about how memory access compares to database access speed.
Caching keeps data ready in fast memory, so Rails doesn't need to ask the database repeatedly. This reduces load and speeds up responses.
In Rails, if cached data expires, what does the app do on the next request?
Consider how cache expiration triggers data refresh.
When cache expires, Rails reloads data from the database and stores it again in cache for future requests.
Given a Rails app with caching enabled, what is the expected difference in response time between a cache hit and a cache miss?
Think about which is faster: memory access or database query.
Cache hits serve data from fast memory, speeding up response. Cache misses require slower database queries, increasing response time.
Which of the following is the correct way to fetch data with caching in Rails?
Look for the method that accepts a block to compute data if cache misses.
Rails.cache.fetch takes a key and a block. If the key is missing, it runs the block and stores the result.
Given this Rails code snippet, why might caching not improve response time?
def show
@user = User.find(params[:id])
@profile = Rails.cache.fetch("profile_#{params[:id]}") do
@user.profile_data
end
enddef show @user = User.find(params[:id]) @profile = Rails.cache.fetch("profile_#{params[:id]}") do @user.profile_data end end
Look at which part of the code always queries the database.
The User.find call runs on every request before caching. So the database query is not avoided, reducing caching benefit.