【问题标题】:How to expire fragment cache for objects index on object update如何在对象更新时使对象索引的片段缓存过期
【发布时间】:2012-02-18 12:08:32
【问题描述】:

我正在片段缓存这个片段:

<% cache('fragment-id') do %>
    <div id="fragment-id">
        <%= render @object %>
    </div> 
<% end %> 

现在,很明显,每次更新 object 时我都需要使缓存过期(从 db 的角度来看)。

所以我应该把这行:

expire_fragment('fragment-id') 

...在控制器中的各处,每次操作更新(或保存)一个对象

但我想知道是否有 DRYer 方法 可以做到这一点,例如在 object 模型(/app/models/object. rb) 在每次更新(或保存)时触发 expire_fragment 命令。

有可能吗?

编辑#1:

也许我可以像这样覆盖对象模型中的保存方法:

def save
    expire_fragment('fragment-id')
    super
end

但是,为了使其正常工作,我必须确保 all 其他保存方法(如 *update_attributes*、save!等。是这样吗?

【问题讨论】:

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


    【解决方案1】:

    您可以使用 Sweeper 观察您的对象,并在其中添加更新挂钩。阅读有关扫地机的信息there

    【讨论】:

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