[英]ruby on rails 3.1 fragment caching
我有一个应用程序,我想缓存页面的某些部分,我已经阅读了很多关于如何做到这一点的方法。
我知道片段缓存是在我的项目中执行它的最佳方式,但我找不到一个简单的例子来学习如何实现它。
我想将片段缓存与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? 有什么例子吗? 我怎样才能做到这一点?
在您的产品型号中,您可以执行以下操作
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})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.