心海天空&Ruby on Rails 学习日记
hpxing · 2007-12-22(近似)
<< rails笔记: view
|
首 页
|
rails笔记 activerecord 1 >>
rails笔记 cache系统
{#timeline}
cache系统 1
cache系统默认只在production下面生效, 要手动生效修改环境(config/environments) ActionController::Base.perform_caching=true|false
手动cache
caches_page :xxxaction 直接cache整个页面,相当于html
caches_action :xxxxaction 只cache action,也就是要执行filter
cache只适合静态内容, 动态变化的内容不适合cache
cache的清除
手动清除,他们接受的参数都和url_for一样,直接按照url来过期
expire_page
expire_action
自动清除
通过定一个sweeper类 < ActionController::Caching::Sweeper实现after_create after_update,after_destory三个方法, 传入的参数就是model对象
在controller中生效的方式是cache_sweeper指令
批量清除 pagecache会放在app/public/下面,清除对应的html即可,比如写一个crontab,cache目录和文件名的控制如下 ActionController::Base.page_cache_directory = "dir/name" ActionController::Base.page_cache_extension = ".html"
rails的忠告, 为了避免新出现的google web accelarator这样的工具破坏web, 所有风险大的操作都应该隐藏在POST协议中, 不要直接用get,不然一旦被客户端(和真实用户在一个session中)抓住, url就不保险了
cache系统 2
通过 <%cache do%> ... <%end%>
来实现部分cache
通过expire_fragment(:action=>'xxx')来清除对应cache
如果一个页面里面有多个part 可以使用 cache(:part=>"yyy") 在清除的时候加入:part=>"yyy"就可以了
cache(:controller=>..:action=>...)参数和url_for类似
cache的存储方式有4种可选
ActionController::Caching::Fragments::MemoryStore.new 直接内存(not a good idea when data is large)
ActionController::Caching::Fragments::FileStore.new(path) 直接文件
ActionController::Caching::Fragments::DRbStore.new(url) drbserver
ActionController::Caching::Fragments::MemCachedStore.new(host) 使用memcached server
Tag:
rails
hrtsea
@ {#posttime} |
引用(0)
|
编辑
评论
发表评论
Name (required)
Mail (will not be published) (required)
Website
原始快照:web.archive.org