2

I am using fragment caching in rails 7 and encountering a issue . for reference, here is my view code which is views/users/index.html.erb

<p style="color: green"><%= notice %></p>

<h1>Users</h1>

<% cache "users_filter" do %>
  <%= render partial: "users/partials/filter"%>
<% end %>

<table class="">
   <thead>
      <tr>
        <th>Name</th>
        <th>Age</th>
        <th>Action</th>
      </tr>
  </thead>
  <tbody>
  <div id="users">

  <% @users.each do |user| %>
    <% cache user do %>
      <%= render user %>
    <% end %>
  <% end %>

  </div>
  </tbody>
</table>

<% cache "links" do %>
  <%= link_to "New user from here", new_user_path %>
  <%= link_to "Sign Out", destroy_member_session_path, data: { turbo_method: :delete} %>
<% end %>

Now my question is when I am changing anything in the links block which is at the end of this page, all the other cache blocks (which are already cached and does not change) also gets re-cached which they should not. I have separate cache keys for all of them but dont know why this is happening. For reference here are logs.

Started GET "/users" for ::1 at 2023-05-14 14:45:10 +0500
Processing by UsersController#index as HTML
  Member Load (0.1ms)  SELECT "members".* FROM "members" WHERE "members"."id" = ? ORDER BY "members"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Rendering layout layouts/application.html.erb
  Rendering users/index.html.erb within layouts/application
Read fragment views/users/index:fd9e89346f2ad4b3c1b5b5e174f6150c/users_filter (0.1ms)
  Rendered users/partials/_filter.html.erb (Duration: 0.4ms | Allocations: 281)
Write fragment views/users/index:fd9e89346f2ad4b3c1b5b5e174f6150c/users_filter (0.0ms)
  User Load (0.1ms)  SELECT "users".* FROM "users"
  ↳ app/views/users/index.html.erb:20
Read fragment views/users/index:fd9e89346f2ad4b3c1b5b5e174f6150c/users/17-20230514092800723809 (0.1ms)
  Rendered users/_user.html.erb (Duration: 0.5ms | Allocations: 274)
Write fragment views/users/index:fd9e89346f2ad4b3c1b5b5e174f6150c/users/17-20230514092800723809 (0.1ms)
Read fragment views/users/index:fd9e89346f2ad4b3c1b5b5e174f6150c/links (0.0ms)
Write fragment views/users/index:fd9e89346f2ad4b3c1b5b5e174f6150c/links (0.0ms)
  Rendered users/index.html.erb within layouts/application (Duration: 4.8ms | Allocations: 3024)
  Rendered layout layouts/application.html.erb (Duration: 6.6ms | Allocations: 5589)
Completed 200 OK in 14ms (Views: 8.6ms | ActiveRecord: 0.2ms | Allocations: 7741)
Muhammad Ans
  • 157
  • 8

1 Answers1

0

When you're saying you're changing something in the links block, seeing as there's no dynamic data in that block, I assume you mean that you're changing the code in that template file?

Part of the Rails cache key is a hash of the template file so that if the template file ever changes, the caches are all refreshed.

smathy
  • 26,283
  • 5
  • 48
  • 68
  • Yes I am changing code just to test. For example I am changing the title of the link. – Muhammad Ans May 16 '23 at 15:32
  • Right, so as I said, the cache key Rails creates includes the hash (an md5 IIRC) of the template file, so if you change that then all caches in that file are invalidated. – smathy May 16 '23 at 16:22