Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Rails Cache

4,618 views

Published on

shanghaionrails first event presentation given by martin.xus

Published in: Technology
  • Be the first to comment

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

×