【问题标题】:Rails 5.2 - cache is not being updatedRails 5.2 - 缓存未更新
【发布时间】:2020-10-27 02:42:05
【问题描述】:

我认为有这个方案:

_user_data.html.erb

<tbody>
  <%= render partial: 'edit_history_inline_user', collection: @presenter.collection, as: :user, cached: Proc.new{|user| [acl_fingerprint, 'history', user.record] } %>
</tbody>

_edit_history_inline_user.html.erb

<td class="align_center">
  <%= user.first_name %>
</td>
<td class="align_center">
  <%= user.last_name %>
</td>
...

问题是当我更新user 模型时,_edit_history_inline_user.html.erb 中的数据没有更新。但是,如果我在 _edit_history_inline_user.html.erb 文件中进行一些更改,例如(添加 -- 字符):

<td class="align_center">
  -- <%= user.first_name %> --
</td>

将显示正确/最近修改的数据。这怎么可能?为什么修改后的数据显示不正确?

这是我在父部分视图中使用的acl_fingerprint 方法:

def acl_fingerprint
  Rails.cache.fetch("#{current_admin.cache_key(:current_sign_in_at, :updated_at)}/acl_fingerprint", expires_in: 3.hours) do
    CacheService.acl_fingerprint(current_admin, params[:controller])
  end
end

我该如何解决这个问题?

任何帮助将不胜感激!

【问题讨论】:

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


    【解决方案1】:

    如果我理解正确,acl_fingerprint 在您更新用户时不会更改(除非您更新current_admin,在这种情况下缓存应该过期)。这样就留下了永远不会改变的'history' 字符串和user.record

    简单的解决方案是将用户的updated_at 包含到缓存键中(您可以使用user.cache_key_with_version)。

    修改模板后缓存过期的原因是Rails在缓存键中包含模板校验和。

    【讨论】:

    • 嗨@RolandasBarysas,感谢您的回答。问题是我正在使用.update_all 方法更新模型,该方法不会为updated_at 字段添加时间戳。因为我那里的查询相当复杂,所以我通过简单地将updated_at: DateTime.now 添加到.update_all 方法来解决这种情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 2020-05-09
    • 2017-02-23
    相关资源
    最近更新 更多