June 10, 2017
Let’s show a list of products.
# Showing a list of products <% @products.each do |product| %> <%= product.name %> <%= product.description %> <%= product.price %> <% end %>
This works fine. Until…there are thousands of records. So, let’s apply caching.
Now using Russian-doll caching.
<% cache(["products_list", @products.pluck(:id), @products.maximum(:updated_at)]) do %> <% @products.each do |product| %> # do the same thing <% end %> <% end %>
Here, we set a key for the cache that simply says that if this key changes, we bust out the cache.
Since Rails automatically updates the
updated_at column whenever a record is
updated, we can use it as part of the cache key.
Caching, in general, is easy to implement in Rails. The challenge here is identifying what the cache key is and when to bust the cache.
Also, please be aware that caching doesn’t mean that you will gain performance. We also need to consider how the cache is stored since that may add network latency from the application to the cache store.
Hi, I'm Mark Chavez. Creator of Get Things Done, yamda, public_apis, js_issues, bitcoin_index and a whole bunch of open-source projects. Join me on my adventures as I unfold the good and bad bits about software writing. You can also follow me on twitter and github for more goodies. Also, I love #oss!