dimanche 26 août 2018

How does a Rail fragment cache benefit your app, i.e. prevent database calls?

The information in my header/footer is derived from a bunch of dictionary tables in my database, and the data is going to change extremely infrequently. I would think this is the perfect opportunity for caching, so that every page won't touch the database as far as rendering the header/footer is concerned.

The Rails guide gives an example of fragment caching so I'm thinking for my nav links would be something like...

<% @categories.each do |category| %>
  <% cache category do %>
    <li class="nav-link">
      <%= link_to category.name, category_path(category) %>
    </li>
  <% end %>
<% end %>

But I don't understand how this prevents contact with the database or optimizes anything. The controller is making the call for @categories before the view renders which means the SQL query is made every page request... some HTML is being cached, but is the rendering process really a significant saving even for a larger snippet?? I feel like the strain on the database is what you really want to be limiting especially if dealing with a lot of concurrent traffic.

What is the appropriate caching strategy for things like footer / navigation partials.?




Aucun commentaire:

Enregistrer un commentaire