Speed up Rails controller speed easily
When we adding a Rails controller action, especially for #show
. Usually it would be like this:
def show
post = Post.find(params[:id])
render json: post
end
It would hit our database everytime there is a request to that action. So what we can do is we cache the Post model into Cachestore.
Usually I use identity_cache gem for that. It uses Memcached as a datastore. So it would be like this if using IdentityCache gem:
def show
post = Post.fetch(params[:id])
render json: post
end
Another optimization is we can add expiry tag
def show
post = Post.fetch(params[:id])
return unless stale?(post)
render json: post
end
See more about IdentityCache at: https://github.com/Shopify/identity_cache
Leave a comment