2011-11-14 106 views
0

我有一個應用程序,我想緩存頁面的一些部分,我一直在閱讀很多關於如何做到這一點。軌道上的紅寶石3.1片段緩存

我明白,片段緩存是在我的項目上做到這一點的最好方法,但我找不到一個簡單的例子來學習如何實現它。

我想使用片段緩存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?周圍的任何例子?我怎樣才能做到這一點?

回答

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