简体   繁体   中英

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 .

 <% 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? 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})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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