---
title: "Caching With Rails"
author: "clark1231"
date: "2013-02-18T16:55:12Z"
tags: []
categories: ["互联网"]
slug: "caching-with-rails"
draft: false
lastmod: "2013-02-18T16:55:56Z"
id: 1807441
url: "https://caok1231.iteye.com/blog/1807441"
---

原文在我另一个博客上，这边就不在另外排版了，可参见[http://caok1231.com/blog/2013/01/30/caching-with-rails/](http://caok1231.com/blog/2013/01/30/caching-with-rails/)

1.Page caching

Page caching是最简单最高效的一种，它会将Action最后的HTML结果存成public/下的HTML文件，也就是静态网页。

class ProductsController &lt; ActionController

  caches\_page :index

  def index; end

end

不过缺点也同样明显，由于是静态网页，对于任何的request都会返回同一个结果，适用面比较窄。 比如用kaminari等定义了翻页功能，就将失效，始终停留在第一页。

 

如果要真的使用的话，可以在有新的product创建的时候更新index。

class ProductsController &lt; ActionController

  caches\_page :index

 

  def index

    @products = Products.all

  end

 

  def create

    expire\_page :action =&gt; :index

  end

end

expire\_page会在有create操作的时候，清除cache里的资料重新生成。

 

2.Action caching

 

Rails在development模式下的cache默认是关闭的。

 

打开Rails的cache很简单，只要在环境对应的配置文件config/environments/\*.rb中设置即可：

 

config.action\_controller.perform\_caching = true

action caching与page caching的区别在于request会经过web server并且被rails application接收，直到所有的before filters被处理。

class ProductsController &lt; ActionController

  before\_filter :authenticate, :only =&gt; :create

  caches\_action :index

 

  def index

    @products = Product.all

  end

 

  def create

    expire\_action :action =&gt; :index

  end

end

缺点也和Page caching一样，无法提供不同使用者有不同內容。

 

expire\_action会在有create操作的时候，清除cache里的资料重新生成。

 

3.Fragment Caching

 

动态web应用程序的页面往往由多个components组成，而每个component经常需要采取不同的cache、expire策略。针对这个问题，rails提供了fragment cache。

 

Fragment caching可以只缓存HTML中的一小段元素，我們可以自由选择要cache的区域。这种cache发生在View中，所以我們须把cache程式写在View中，用cache包起來要cache的Template：

 

如果一个页面有多个component被cache，则需要添加suffix来区分它们：

&lt;% cache(:action =&gt; 'recent', :action\_suffix =&gt; 'all\_products') do %&gt;

  All available products:

如果你想要一个无需绑定到相应action的cache块，可以赋予一个全局的key

&lt;% cache 'all\_available\_products' do %&gt;

  All available products:

&lt;% end %&gt;

cache方法接受一个可选参数。这个参数被用作缓存的key(默认情况下，页面的URL会被作为缓存的key)。如果我们把模型(model)当作参数,那么模型的cache\_key属性将被作为这个key。这就是说，当article更新的时候这个缓存片段就会过期。

&lt;% title @article.name %&gt;  

&lt;% cache @article do %&gt;

  &lt;p class="author"&gt;&lt;em&gt;from &lt;%=h @article.author\_name %&gt;&lt;/em&gt;&lt;/p&gt;  

  &lt;% for comment in @article.comments %&gt;  

    &lt;div id="comment"&gt;

      &lt;strong&gt;&lt;%= link\_to\_unless comment.site\_url.blank?, h(comment.author\_name), h(comment.site\_url) %&gt;&lt;/strong&gt;  

      &lt;em&gt;on &lt;%= comment.created\_at.strftime('%b %d, %Y at %H:%M') %&gt;&lt;/em&gt;

      &lt;%= simple\_format comment.content %&gt;  

    &lt;/div&gt;  

  &lt;% end %&gt; 

  &lt;!-- Rest of code omitted --&gt;  

&lt;% end %&gt;  

&lt;h3&gt;Add your comment:&lt;/h3&gt;  

&lt;%= render :partial =&gt; 'comments/form' %&gt;

cache\_key由模型(model)名,模型的id和updated\_at属性组成。key的最后一段非常有用，因为这一段组成部分，这个key每次都会应为模型的更新而改变。这样每次模型的任意属性有更改，这个缓存片段都会过期。

 

在我们的应用程序里一个Article可能有很多Comments。如果我们使用article页面的表单对article添加一条comment，这条comment将不会作为article页面的一部分被显示。这是因为article已经被缓存了，article页面只会显示缓存里面的comments。当一条comment被添加时article的时间戳未被修改，所以缓存片段不会过期。

 

要想实现当添加或修改comment时article页面显示新的comment，我们仅仅需要对comment模型(model)做一点点修改:

 

class Comment &lt; ActiveRecord::Base

  belongs\_to :article, :touch =&gt; true

end

给belongs\_to关系添加 :touch =&gt; true 意味着当创建，更新或者删除一条comment的时候，该comment属于(belongs\_to)的article被touched。现在我们添加一条comment，缓存会失效并且页面会更新而且显示刚添加的comment。

 

清理cache

 

手动清除

rake tmp:cache:clear

在资料修改或刪除时，在适当的Controller Action中过期这些cache资料

expire\_fragment(:controller =&gt; 'products', :action =&gt; 'recent',  :action\_suffix =&gt; 'all\_prods)

expire\_fragment(:key =&gt; \['all\_available\_products', @latest\_product.created\_at].join(':'))

 

4.Sweepers

 

但如上面这样到处写expire方法，显然不是最好的方式。Rails针对此提供了sweeper机制：把cache清理移入一个observer类，此类会监测一个对象的变化，并且通过相应的钩子来清理此对象相关的cache。

class StoreSweeper &lt; ActionController::Caching::Sweeper

  # This sweeper is going to keep an eye on the Product model

  observe Product

 

  # If our sweeper detects that a Product was created call this

  def after\_create(product)

      expire\_cache\_for(product)

  end

 

  # If our sweeper detects that a Product was updated call this

  def after\_update(product)

    expire\_cache\_for(product)

  end

 

  # If our sweeper detects that a Product was deleted call this

  def after\_destroy(product)

    expire\_cache\_for(product)

  end

 

  private

  def expire\_cache\_for(record)

    # Expire the list page now that we added a new product

    expire\_page(:controller =&gt; '#{record}', :action =&gt; 'list')

 

    # Expire a fragment

   expire\_fragment(:controller =&gt; '#{record}', :action =&gt; 'recent', :action\_suffix =&gt; 'all\_products')

  end

end

 

Cache sweeper在controller里面就是一个after或者aroud filter

class ProductsController &lt; ActionController

  cache\_sweeper :store\_sweeper, :only =&gt; \[ :create ]

 

  def create

  end

end

 

5,Counter cache

 

如果需要常计算has\_many的Model有多少笔记录，例如显示文章列表时，也要显示每篇有多少留言回复。

&lt;% @topics.each do |topic| %&gt;

  主題：&lt;%= topic.subject %&gt;

  回复數：&lt;%= topic.posts.size %&gt;

&lt;% end %&gt;

这时Rails会产生一笔笔的SQL count查询：

 

SELECT * FROM \`posts\` LIMIT 5 OFFSET 0

SELECT count(\*) AS count\_all FROM \`posts\` WHERE (\`posts\`.topic\_id = 1 )

SELECT count(\*) AS count\_all FROM \`posts\` WHERE (\`posts\`.topic\_id = 2 )

SELECT count(\*) AS count\_all FROM \`posts\` WHERE (\`posts\`.topic\_id = 3 )

SELECT count(\*) AS count\_all FROM \`posts\` WHERE (\`posts\`.topic\_id = 4 )

SELECT count(\*) AS count\_all FROM \`posts\` WHERE (\`posts\`.topic\_id = 5 )

Counter cache功能可以把這個數字存進資料庫，不再需要一筆筆的SQL count查詢，並且會在Post數量有更新的時候，自動更新這個值。

 

首先，你必须要在Topic Model新增一個栏位叫做posts\_count，依照慣例是\_count结尾，型別是integer，有预设值0。

 

rails g migration add\_posts\_count\_to\_topic

编辑Migration：

 

class AddPostsCountToTopic &lt; ActiveRecord::Migration

  def self.up

    add\_column :topics, :posts\_count, :integer, :default =&gt; 0

 

    # 如果是网站上线后才新增这个功能，这里需要先计算设定好初始值

    Topic.find\_each do |topic|

      topic.update\_attribute(:posts\_count, topic.posts.size)

    end

  end

 

  def self.down

    remove\_column :topics, :posts\_count

  end

end

编辑Models，加入:counter\_cache =&gt; true：

 

class Topic &lt; ActiveRecord::Base

  has\_many :posts

end

 

class Posts &lt; ActiveRecord::Base

  belongs\_to :topic, :counter\_cache =&gt; true

end

这样同样的@topic.posts.size，就会自动变成使用@topic.posts\_count，而不會用SQL count查詢一次。

 

6.rails.cache

 

Rails.cache.read("city")   # =&gt; nil

Rails.cache.write("city", "Duckburgh")

Rails.cache.read("city")   # =&gt; "Duckburgh"

 

cache.write("today", "Monday")

cache.fetch("today")  # =&gt; "Monday"

 

cache.fetch("city")   # =&gt; nil

cache.fetch("city") do

  "Duckburgh"

end

cache.fetch("city")   # =&gt; "Duckburgh"

 

原文在我另一个博客上，这边就不在另外排版了，可参见[http://caok1231.com/blog/2013/01/30/caching-with-rails/](http://caok1231.com/blog/2013/01/30/caching-with-rails/)