【问题标题】:Rails - how do I refresh an item in a cached partial?Rails - 如何刷新缓存部分中的项目?
【发布时间】:2019-10-28 19:42:17
【问题描述】:

我有这个场景(主视图模板):

<% if @cars.count(:all) > 0 %>
  <% presenter.collection.each_pair do |date, collection| %>
    <tr>
      <td>
        ...
      </td>
    </tr>
    <%= render partial: 'car_row_content', collection: collection, as: :car, cached: Proc.new{|car| [cache_prefix, acl_fingerprint, car.record] } %>
  <% end %>
<% end %>

下面是部分car_row_content 的样子:

<tr>
  <td>
    <%= car.something1 %>
  </td>
  <td>
    <%= car.something2 %>
  </td>
  <td>
    <%= car.something3 %>
  </td>
</tr>

在这部分中,something1something2 属性始终保持不变,但 something3 属性变化很大(一天几次)。

这里发生了什么 - 当我们的一位管理员更改属性 something3 时,它通常不会立即“刷新”主视图模板,我们会在那里看到“旧”值。如何强制 Rails 缓存在发生更改时立即刷新?

【问题讨论】:

    标签: ruby-on-rails caching


    【解决方案1】:

    使用缓存键,当你更新任何东西时,缓存键也会更新,你的数据也会更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多