【问题标题】:Rails 5 - Collection caching not refreshingRails 5 - 集合缓存不刷新
【发布时间】:2016-04-29 09:23:12
【问题描述】:

我想我一定错过了 Collection caching - 1.3.1 的工作原理。当我将cached: true 属性添加到渲染时,它不会在模型更改时刷新缓存。但是,如果没有此属性,它将按预期工作。

attending.html.erb

<%= render partial: 'attending_item', collection: @attendees, locals: {reserved: true}, cached: true %>

_item_attending.html.erb

<% cache([attendee.user, attendee, reserved]) do %>
  <li class="attendee_id_<%= attendee.id %> <%= reserved ? 'reserved' : 'confirmed' %>">
  <%= image_tag(attendee.user.profile.avatar_url(:avatar), size: '25x25') %>
  <%= attendee.user.full_name %>
 </li>
<% end %>

user 模型将通过 Profile 模型上的 touch: true 更新,因此当他们更新头像时:

belongs_to :user, touch: true

有人可以向我解释一下我缺少什么或不了解集合缓存是如何工作的,也许这是 Rails 中的错误?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5


    【解决方案1】:

    这里的游戏迟到了,但我认为您的缓存不会过期,因为它取决于用户和保留值。

    查看 multi_fetch_fragments gem 的更好文档,该文档已合并到 Rails 中以使这成为可能,增加了一些线索。

    我认为您想将您的 cache: true 更改为 cache: Proc.new { |attendee| [attendee, user, reserved] } 以考虑那里的更改。

    此外,我相信您希望删除 _item_attending.html.erb 文件中的 cache 块,因为该部分会自动缓存。

    【讨论】:

      猜你喜欢
      • 2012-02-17
      • 2014-03-13
      • 2014-08-02
      • 2013-04-23
      • 1970-01-01
      • 2013-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多