Performance: Why caching improves response times
Caching reduces server processing time and network delays, speeding up page load and interaction response.
def show @post = Rails.cache.fetch("post_#{params[:id]}") { Post.find(params[:id]) } render json: @post end
def show
@post = Post.find(params[:id])
render json: @post
end| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No caching, fetch from DB every time | N/A (server-side) | N/A | N/A | [X] Bad |
| Cache data with Rails.cache.fetch | N/A (server-side) | N/A | N/A | [OK] Good |
fetch.get, store, and save are not valid Rails cache methods for this pattern.count = 0
result = Rails.cache.fetch('count_key') do
count += 1
end
puts result
result = Rails.cache.fetch('count_key') do
count += 1
end
puts resultPass the test and this pattern moves unknown