ruby-on-rails - 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
我该如何解决这个问题?
任何帮助将不胜感激!
解决方案
如果我理解正确,acl_fingerprint
更新用户时不会改变(除非你更新current_admin
,在这种情况下缓存应该过期)。这样就留下'history'
了永远不会改变的字符串,并且user.record
.
简单的解决方案是将用户的updated_at
to 包含到缓存键中(您可以使用user.cache_key_with_version
)。
修改模板后缓存过期的原因是 Rails 在缓存键中包含模板校验和。
推荐阅读
- javascript - 如何使用 jquery 将元素的副本拖动到图像上?
- c++ - c++:分段错误,核心转储
- c - 在获取这些字符串时,如何将字符串复制到另一个用 '\n' 字符分隔的数组?
- c# - 未放置在组合框中时获得组合框项目视觉效果
- google-cloud-firestore - Google Cloud Firestore:如何列出带有子集合的文档?
- python - 在熊猫上使用操作时,找不到在 for 循环中迭代两种方式的方法?
- java - T6 压缩 Tiff 图像
- ios - 如何在编辑其他字段时自动折叠表单中的 DatePicker?
- python - 为 asyncio 制作 tqdm 进度条
- python - 为什么即使在执行 nltk.download 并且所有软件包都已正确安装后,nltk word_tokenize 也无法正常工作?