简体   繁体   English

ruby on rails 3.1片段缓存

[英]ruby on rails 3.1 fragment caching

I have an app and I'd like to cache some parts of the page, and I have been reading a lot about the ways to do this. 我有一个应用程序,我想缓存页面的某些部分,我已经阅读了很多关于如何做到这一点的方法。

I understand that fragment caching is the best way to do it on my project, but I can´t find a simple example to learn how to implement this. 我知道片段缓存是在我的项目中执行它的最佳方式,但我找不到一个简单的例子来学习如何实现它。

I would like to use the fragment cache with autoexpire . 我想将片段缓存与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 %>

Where do I set the autoexpire? 我在哪里设置autoexpire? any examples around? 有什么例子吗? how can I do this? 我怎样才能做到这一点?

In your product model you can do something like this 在您的产品型号中,您可以执行以下操作

  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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM