【发布时间】:2019-02-08 20:16:32
【问题描述】:
我最近刚刚将我的项目从 Rails 4 更新到 Rails 5.2。通过测试,我注意到缓存没有在应有的地方更新。
通过文档,我注意到 ActionView 缓存的方式有些不同。
Rails 4 在缓存名称中包含 updated_at 值。通过这样做,每次触摸元素时,先前的缓存都会过时并创建一个新的缓存。
views/projects/123-20120806214154/7a1156131a6928cb0026877f8b749ac9
^class ^id ^updated_at ^template tree digest
太好了。但是 Rails 5.2 带来了一些变化,在缓存名称中不再设置 updated_at 值。来自文档:
views/template/action.html.erb:7a1156131a6928cb0026877f8b749ac9/projects/123
^template path ^template tree digest ^class ^id
不过,文档指出:
当项目 updated_at 被触动时,cache_version 会发生变化,即使密钥保持稳定。这意味着,与传统的基于键的缓存过期方法不同,您不会仅仅因为更新了相关记录而生成缓存垃圾、未使用的键。
据此,我认为它在后台做了一些魔术来替换缓存中的值,但事实并非如此。
我的代码:
<% cache ['tags', post] do %>
<%= render "shared/tags", post: post %>
<% end %>
即使我在控制台上post.touch,缓存仍然是旧缓存,我必须手动删除缓存才能正常工作。我错过了什么吗?如何让它再次工作?
我知道我可以做类似的事情
cache "tags/#{post.updated_at}" do
...
但从文档来看,感觉不对。
【问题讨论】:
标签: ruby-on-rails actionview ruby-on-rails-5.2