首页 > 解决方案 > Rails 5.2 - 缓存未更新

问题描述

在我看来,我有这个方案:

_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-railscachingredisruby-on-rails-5

解决方案


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

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

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


推荐阅读