rails笔记 cache系统
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
轉載于:https://www.cnblogs.com/baoz/archive/2006/01/24/2269861.html
總結
以上是生活随笔為你收集整理的rails笔记 cache系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Microsfot.Web.UI.Web
- 下一篇: 笔迹鉴别(5) —— 笔迹判别