簡體   English   中英

ruby on rails 3.1片段緩存

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM