我有一个应用程序,我想缓存页面的某些部分,我已经阅读了很多关于如何做到这一点的方法。
我理解片段缓存是在我的项目中执行此操作的最佳方式,但我找不到一个简单的示例来学习如何实现它。
我想将片段缓存与 autoexpire 一起使用。
<% cache(:action => 'recent', :action_suffix => 'all_products') do %>
All available products:
<% Product.all.each do |p| %>
<%= link_to p.name, product_url(p) %>
<% end %>
<% end %>
我在哪里设置autoexpire?有什么例子吗?我怎么能这样做?
答案 0 :(得分:2)
在您的产品型号中,您可以执行以下操作
after_save :expire_caches
after_destroy :expire_caches
# can't do this in a sweeper since there isn't a controller involved
def expire_caches
ActionController::Base.cache_store.delete_matched(%r{product\?for=\d+&fragment=products})