ruby-on-rails Rails中的片段缓存

bq9c1y66  于 2023-05-19  发布在  Ruby
关注(0)|答案(1)|浏览(130)

我在rails 7中使用片段缓存,遇到了一个问题。作为参考,这里是我的视图代码,它是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 %>

现在我的问题是,当我改变链接块中的任何内容时,所有其他缓存块(已经缓存并且没有改变)也会重新缓存,而它们不应该。我有单独的缓存键为所有这些,但不知道为什么会发生这种情况。这里有日志供参考。

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)
u3r8eeie

u3r8eeie1#

当你说你要改变链接块中的一些东西时,因为那个块中没有动态数据,我想你的意思是你要改变模板文件中的代码?
Rails缓存键的一部分是模板文件的散列,因此如果模板文件发生更改,缓存将全部刷新。

相关问题