0
Rails Cache
   级别:基础




                        徐祥军
              martin@recity.net
           http://www.recity.net
About ReCity
ReCity -- 城市信息分享平台
• 所有信息均来自于用户
• 用户创建、拥有、维护和分享
• 创新机制使得信息能自动趋于准确、及时、
  有效
• 不仅仅分享信息,还分享收益


         Shangha...
Rails Cache
• Page Cache
• Action Cache
• Fragment Cache




                   Shanghai On Rails 2007
Use Cache



config.action_controller.perform_caching = true




                   Shanghai On Rails 2007
Page Cache

• Rails 内速度最快的一种缓存
• 简单,容易使用
• Cached Page,不必再执行Rails,直接由Web Server
  加载静态的Html页面




               Shanghai ...
Page Cache Example
class UsersController < ApplicationController
 caches_page :show

 def show
  …
 end

 …

end




     ...
http://localhost/users/show/100

   Rendering users/show
   Cached page: /users/show/100.html


$ ls public/users/show
  1...
Page Cache Sweeper
class UserSweeper < ActionController::Caching::Sweeper
 observe User

  def after_update(record)
   exp...
User.find(100).update_attribute :login,’something’



Expired page: /users/show/100.html (0.00000)




                   ...
Page Cache Problems
• Page Cache的内容是静态的html,是完全public,所有用
  户看到完全一样的页面,如何处理极少量的不一致或个性
  化的内容或有有权限验证的内容
   1. Ajax
   2. Cl...
Page Cache Problems
• Page Caches生成的文件名是通过url_for生成的,参数不
  会被保存
 分页?page=1
 复杂?sort=score&user_id=100&category_id=1


    ...
Action Cache
• 类似Page Cache
• Action Cache 执行
       调用controller
  1.
       执行before_filter
  2.
       检测cache是否存在,如果存在...
Action Cache Example
类似于Page Cache:caches_action,expire_action

      class UsersController < ApplicationController
      ...
http://localhost/users/show/100

    Rendering users/show
    Cached fragment: localhost/users/show/100 (0.00000)



$ ls ...
expire_action :controller=>:users,:action=>:show,:id=>record.id




                         Shanghai On Rails 2007
Action Cache Problem
• 查询字符
 caches_action_with_params
 expire_action_with_params




                         Shanghai On...
Fragment Cache
• 用于cache 视图中代码块
• 默认会在/tmp/cache目录下生成.cache文件
• 清除缓存 expire_fragment




            Shanghai On Rails 2007
Fragment Cache Example

<%cache do%>
<ul>
 <%@users.each do |user|%>
  <li><%=link_to user.login,user_path(user)%></li>
 <...
但是还是执行了查询?


 unless read_fragment()
  @users = User.paginate :all, :page=>params[:page]||1
 end




                     ...
Fragment Cache
• 自定义片段的名称
页面中的fragment cache多于一个时,需指定额外名称
类似url_for

cache(:action=>’show’,:part=>’users’)
cache(:action=>...
Action/Fragment Cache 存储方式

    内存
•
    文件
•
•   DRB
•   MemCached




                Shanghai On Rails 2007
Expiring Strategy
• Model状态
• 时间
• Others




            Shanghai On Rails 2007
Useful Plugins
•   sweeper generator
•   timed_fragment_cache
•   cache_fu
•   cached_model




                  Shanghai...
sweeper generator



./script/generate sweeper SweeperName callback1 callback2




                       Shanghai On Rail...
timed_fragment_cache
• View
<% cache 'fragment_name', 10.minutes.from_now do %>
 #the cached fragment which does something...
cache_fu
class User < ActiveRecord::Base
  acts_as_cached
end




                 Shanghai On Rails 2007
CachedModel
class User < CachedModel
end




                Shanghai On Rails 2007
More
• Mode Cache
• Query Cache(Edge Rails)




                Shanghai On Rails 2007
Questions?
  Martin@Recity.Net




   Shanghai On Rails 2007
Upcoming SlideShare
Loading in...5
×

Rails Cache

4,245

Published on

shanghaionrails first event presentation given by martin.xus

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,245
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
121
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Transcript of "Rails Cache"

  1. 1. Rails Cache 级别:基础 徐祥军 martin@recity.net http://www.recity.net
  2. 2. About ReCity ReCity -- 城市信息分享平台 • 所有信息均来自于用户 • 用户创建、拥有、维护和分享 • 创新机制使得信息能自动趋于准确、及时、 有效 • 不仅仅分享信息,还分享收益 Shanghai On Rails 2007
  3. 3. Rails Cache • Page Cache • Action Cache • Fragment Cache Shanghai On Rails 2007
  4. 4. Use Cache config.action_controller.perform_caching = true Shanghai On Rails 2007
  5. 5. Page Cache • Rails 内速度最快的一种缓存 • 简单,容易使用 • Cached Page,不必再执行Rails,直接由Web Server 加载静态的Html页面 Shanghai On Rails 2007
  6. 6. Page Cache Example class UsersController < ApplicationController caches_page :show def show … end … end Shanghai On Rails 2007
  7. 7. http://localhost/users/show/100 Rendering users/show Cached page: /users/show/100.html $ ls public/users/show 100.html Shanghai On Rails 2007
  8. 8. Page Cache Sweeper class UserSweeper < ActionController::Caching::Sweeper observe User def after_update(record) expire_page :controller=>:users,:action=>:show,:id=>record.id end … end class UsersController < ApplicationController cache_sweeper :user_sweeper caches_page :show Shanghai On Rails 2007
  9. 9. User.find(100).update_attribute :login,’something’ Expired page: /users/show/100.html (0.00000) Shanghai On Rails 2007
  10. 10. Page Cache Problems • Page Cache的内容是静态的html,是完全public,所有用 户看到完全一样的页面,如何处理极少量的不一致或个性 化的内容或有有权限验证的内容 1. Ajax 2. Client Javascript 3. 放弃,改用Action Cache或其他Cache Shanghai On Rails 2007
  11. 11. Page Cache Problems • Page Caches生成的文件名是通过url_for生成的,参数不 会被保存 分页?page=1 复杂?sort=score&user_id=100&category_id=1 1:将参数配置到routes里面去, /users/list/page 2:放弃,采用其他的cache Shanghai On Rails 2007
  12. 12. Action Cache • 类似Page Cache • Action Cache 执行 调用controller 1. 执行before_filter 2. 检测cache是否存在,如果存在则返回cache 3. • 要比Page Cache慢,但是也够快 • 底层调用的Fragment Cache Shanghai On Rails 2007
  13. 13. Action Cache Example 类似于Page Cache:caches_action,expire_action class UsersController < ApplicationController before_filter :authenticate caches_page :show cache_sweeper :user_sweeper def show … end end Shanghai On Rails 2007
  14. 14. http://localhost/users/show/100 Rendering users/show Cached fragment: localhost/users/show/100 (0.00000) $ ls -al tmp/cache/localhost/users/show/* … tmp/cache/localhost/users/show/100.cache Fragment read: localhost/users/show/100 (0.00000) Shanghai On Rails 2007
  15. 15. expire_action :controller=>:users,:action=>:show,:id=>record.id Shanghai On Rails 2007
  16. 16. Action Cache Problem • 查询字符 caches_action_with_params expire_action_with_params Shanghai On Rails 2007
  17. 17. Fragment Cache • 用于cache 视图中代码块 • 默认会在/tmp/cache目录下生成.cache文件 • 清除缓存 expire_fragment Shanghai On Rails 2007
  18. 18. Fragment Cache Example <%cache do%> <ul> <%@users.each do |user|%> <li><%=link_to user.login,user_path(user)%></li> <%end%> </ul> <%end%> Shanghai On Rails 2007
  19. 19. 但是还是执行了查询? unless read_fragment() @users = User.paginate :all, :page=>params[:page]||1 end Shanghai On Rails 2007
  20. 20. Fragment Cache • 自定义片段的名称 页面中的fragment cache多于一个时,需指定额外名称 类似url_for cache(:action=>’show’,:part=>’users’) cache(:action=>’show’,:part=>’users’,:page=>1 [,…]) localhost/users/show?page=1 localhost/users/show?page=1&part=users expire_fragment [params…] Shanghai On Rails 2007
  21. 21. Action/Fragment Cache 存储方式 内存 • 文件 • • DRB • MemCached Shanghai On Rails 2007
  22. 22. Expiring Strategy • Model状态 • 时间 • Others Shanghai On Rails 2007
  23. 23. Useful Plugins • sweeper generator • timed_fragment_cache • cache_fu • cached_model Shanghai On Rails 2007
  24. 24. sweeper generator ./script/generate sweeper SweeperName callback1 callback2 Shanghai On Rails 2007
  25. 25. timed_fragment_cache • View <% cache 'fragment_name', 10.minutes.from_now do %> #the cached fragment which does something intensive <% end %> • Controller when_fragment_expired 'fragment_name', 10.minutes_from_now do #some intensive code end Shanghai On Rails 2007
  26. 26. cache_fu class User < ActiveRecord::Base acts_as_cached end Shanghai On Rails 2007
  27. 27. CachedModel class User < CachedModel end Shanghai On Rails 2007
  28. 28. More • Mode Cache • Query Cache(Edge Rails) Shanghai On Rails 2007
  29. 29. Questions? Martin@Recity.Net Shanghai On Rails 2007
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×