Caching With Rails

 Caching With Rails






Caching in Rails is a performance optimization technique that stores copies of objects. It improves the application's performance by reducing response times and server loads. Rails uses several types of caching and it integrates well with caching stores like Memcached, Redis, or even the File System.

Let's understand some of the caching types in detail:

  1. Fragment Caching: Fragment caching allows to caching of the specific parts of views, rather than the entire view. This is useful when you have dynamic sections, such as a post that has comments.

        <% @comments.each do |comment| %>
          <% cache comment do %> 
        <% end %>

    When the request is received for the first time, the system stores a cached HTML copy of the comments, using the updated_at attribute as part of the filename. On subsequent requests, it checks if the updated_at value for the comment has changed. If it hasn’t, the cached version is served, preventing unnecessary re-rendering and improving page load times. If the updated_at value has changed, the cached version is bypassed, and the code block is executed to reflect the updated content.

  2.  Russian Doll Caching: Russian Doll Caching is an advanced caching technique in Rails, designed to optimize the caching of nested fragments within a page. It's particularly useful when you have complex pages with multiple levels of data that change at different rates. By caching individual fragments, Rails ensures that only the parts of the page that change are regenerated, while the rest are served from the cache.

            <% cache @product do %>
                 <h1><%= @product.name %></h1>
                 <% cache @product.reviews do %>
                     <%= render @product.reviews %>
                  <% end %>
             <% end %>


              <% cache @product do %>
                  <h1><%= @product.name %></h1>
                  <% cache @product.reviews do %>
                      <%= render @product.reviews %>
                   <% end %>
              <% end %>

    In large applications, regenerating entire pages on every request can be costly,  especially when most of the page's content rarely changes. Russian Doll Caching allows you to:
    • Cache the entire page (or large portions) and specific sections or components individually. 
    • Minimize unnecessary cache invalidation by only refreshing the parts of the page that need to be updated. 
    • Optimize performance by reducing the data that needs to be regenerated and served dynamically.

  3. Low-Level Caching: This type of caching is helpful when any query does not frequently result in the same data. We can apply caching to that query and set the expiry time. On subsequent requests, it checks whether the key is present. If it is present, it will not execute the query and return the cache data.

    users=Rails.cache.fetch("users_cache", expires_in: 10.minutes) do
      User.where(email: 'xyz@example.com').order(name: :asc) 
            
    end


    Here it will execute the query and save the data into the cache on the first request for the next 10 minutes, on subsequent requests it will not execute the query and directly return the cache data.






                

Comments