【问题标题】:ruby on rails 3.1 fragment cachingruby on rails 3.1 片段缓存
【发布时间】:2011-11-14 16:42:01
【问题描述】:

我有一个应用程序,我想缓存页面的某些部分,我已经阅读了很多关于如何做到这一点的方法。

我知道片段缓存是在我的项目中执行此操作的最佳方式,但我找不到一个简单的示例来学习如何实现它。

我想通过 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 %>

我在哪里设置自动过期?周围有什么例子吗?我该怎么做?

【问题讨论】:

    标签: ruby caching ruby-on-rails-3.1 fragment-caching


    【解决方案1】:

    在你的产品模型中你可以做这样的事情

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-24
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      相关资源
      最近更新 更多