Download - Rails Cache
Shanghai On Rails 2007
About ReCity
ReCity -- 城市信息分享平台
• 所有信息均来自于用户
• 用户创建、拥有、维护和分享
• 创新机制使得信息能自动趋于准确、及时、有效
• 不仅仅分享信息,还分享收益
Shanghai On Rails 2007
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 On Rails 2007
Page Cache Example
class UsersController < ApplicationControllercaches_page :show
def show…
end
…
end
Shanghai On Rails 2007
$ ls public/users/show100.html
http://localhost/users/show/100
Rendering users/showCached page: /users/show/100.html
Shanghai On Rails 2007
Page Cache Sweeperclass UserSweeper < ActionController::Caching::Sweeperobserve User
def after_update(record)expire_page :controller=>:users,:action=>:show,:id=>record.id
end…
end
class UsersController < ApplicationControllercache_sweeper :user_sweepercaches_page :show
Shanghai On Rails 2007
Expired page: /users/show/100.html (0.00000)
User.find(100).update_attribute :login,’something’
Shanghai On Rails 2007
Page Cache Problems• Page Cache的内容是静态的html,是完全public,所有用
户看到完全一样的页面,如何处理极少量的不一致或个性化的内容或有有权限验证的内容
1. Ajax2. Client Javascript3. 放弃,改用Action Cache或其他Cache
Shanghai On Rails 2007
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
Action Cache
• 类似Page Cache• Action Cache 执行
1. 调用controller2. 执行before_filter3. 检测cache是否存在,如果存在则返回cache
• 要比Page Cache慢,但是也够快
• 底层调用的Fragment Cache
Shanghai On Rails 2007
Action Cache Example类似于Page Cache:caches_action,expire_action
class UsersController < ApplicationControllerbefore_filter :authenticate caches_page :showcache_sweeper :user_sweeper
def show…
end
end
Shanghai On Rails 2007
Rendering users/showCached fragment: localhost/users/show/100 (0.00000)
http://localhost/users/show/100
$ 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
expire_action :controller=>:users,:action=>:show,:id=>record.id
Shanghai On Rails 2007
Action Cache Problem
• 查询字符
caches_action_with_paramsexpire_action_with_params
Shanghai On Rails 2007
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>
<%end%></ul><%end%>
Shanghai On Rails 2007
但是还是执行了查询?
unless read_fragment()@users = User.paginate :all, :page=>params[:page]||1
end
Shanghai On Rails 2007
Fragment Cache
• 自定义片段的名称
页面中的fragment cache多于一个时,需指定额外名称
类似url_for
cache(:action=>’show’,:part=>’users’)cache(:action=>’show’,:part=>’users’,:page=>1 [,…])
localhost/users/show?page=1localhost/users/show?page=1&part=users
expire_fragment [params…]
Shanghai On Rails 2007
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 On Rails 2007
sweeper generator
./script/generate sweeper SweeperName callback1 callback2
Shanghai On Rails 2007
timed_fragment_cache
• View<% cache 'fragment_name', 10.minutes.from_now do %>
#the cached fragment which does something intensive<% end %>
• Controllerwhen_fragment_expired 'fragment_name', 10.minutes_from_now do
#some intensive codeend
Shanghai On Rails 2007
cache_fu
class User < ActiveRecord::Baseacts_as_cached
end
Shanghai On Rails 2007
CachedModel
class User < CachedModelend
Shanghai On Rails 2007
More
• Mode Cache• Query Cache(Edge Rails)
Shanghai On Rails 2007