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?

Upvotes: 0

Views: 255

Answers (1)

Uchenna
Uchenna

Reputation: 4089

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

Upvotes: 2

Related Questions