【问题标题】:How do I update the fragment caching when register is updated?更新寄存器时如何更新片段缓存?
【发布时间】: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


    【解决方案1】:

    我想通了。我的项目使用redis来存储缓存,升级后我没有从我的Gemfile中删除gem redis-rails。从redis-activesupport gem 写入缓存的方法是覆盖了 Rails 内置的 redis 方法,该方法是从 Rails 5.2 开始合并的。

    所以解决方法就是删除redis-rails gem。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多