【问题标题】:Expire fragment in Rails 3 for all locales with cache digests在 Rails 3 中为所有具有缓存摘要的区域设置过期片段
【发布时间】:2014-04-07 11:34:50
【问题描述】:

我正在使用带有 cache_digests 的 rails 3,并且我在模板中有一个片段缓存:

<% cache [I18n.locale,'city-list'] do %>
...
<% end %>

在观察者中,我想使所有语言环境的缓存过期,如下所示:

I18n.available_locales.each do |loc|
  ActionController::Base.new.expire_fragment("#{loc}/city-list")
end

但这显然行不通,因为我在 expire 调用中错过了该片段的摘要

有没有一种干净的方法来使这个缓存过期?

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-3 caching


【解决方案1】:

答案似乎是“无法使 cache_digests 手动过期”,请参阅 DHH 的 these answers

要解决此问题,有两种选择:

  1. 绑定密钥一些模型并依赖基于密钥的过期:@ 987654323@

  2. 在缓存调用中跳过摘要,然后手动过期,如问题所示:&lt;% cache [I18n.locale, "city-list"], skip_digest: true do %&gt;

【讨论】:

  • 不确定 1 是否有效。你能扩展一下吗?我想保留摘要,但如果可能,摘要过期。
【解决方案2】:

试试,

<% cache "#{I18n.locale} city-list" do %>
...
<% end %>

#To clear
I18n.available_locales.each do |loc|
  ActionController::Base.new.expire_fragment("#{loc} city-list")
end

【讨论】:

  • 对我不起作用。 expire_fragment 中的key仍然不包含摘要
猜你喜欢
  • 2012-05-27
  • 1970-01-01
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2011-11-28
  • 1970-01-01
相关资源
最近更新 更多