欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Rails的緩存

系統 1739 0
Rails的Cache分四種:
1,Page Cache - Fastest
2,Action Cache - Next Fastest
3,Fragment Cache - Least Fastest
4,ActiveRecord Cache - Only available in Edge Rails
下面一一介紹上面四種Cache以及Rails如何使用memcached

一、Page Cache
如果開發階段要使用cache,則需要先設置好config/environments/development.rb:
Java代碼 收藏代碼
  1. config.action_controller.perform_caching= true

而production環境下默認是開啟cache功能的
Page Cache是Rails中最快的cache機制,使用Page Cache的前提一般為:
1,需要cache的page對所有用戶一致
2,需要cache的page對public可訪問,不需要authentication
Page Cache使用起來很簡單:
Java代碼 收藏代碼
  1. class BlogController<ApplicationController
  2. caches_page:list,:show
  3. deflist
  4. Post.find(:all,\:order=> "created_ondesc" ,:limit=> 10 )
  5. end
  6. defshow
  7. @post =Post.find(params[:id])
  8. end
  9. end

這樣我們就對BlogController的list和show頁面進行了緩存
這樣做的效果是第一次訪問list和show頁面時生成了public/blog/list.html和public/blog/show/5.html這兩個html頁面
對于分頁情況下的cache,我們需要把url的page參數改寫成"blog/list/:page"這種形式,而不是"blog/list?page=1"這種形式
這樣cache的html頁面即為public/blog/list/1.html
當數據更改時我們需要清除舊的緩存,我們采用Sweepers來做是非常不錯的選擇,這把在BlogController里清除緩存的代碼分離出來
首先編輯config/environment.rb:
Java代碼 收藏代碼
  1. Rails::Initializer.run do |config|
  2. #...
  3. config.load_paths+=%w(#{RAILS_ROOT}/app/sweepers)
  4. #...

這告訴Rails加載#{RAILS_ROOT}/app/sweepers目錄下的文件
我們為BlogController定義app/sweepers/blog_sweeper.rb:
Java代碼 收藏代碼
  1. class BlogSweeper<ActionController::Caching::Sweeper
  2. observePost#ThissweeperisgoingtokeepaneyeonthePostmodel
  3. #IfoursweeperdetectsthataPostwascreatedcall this
  4. defafter_create(post)
  5. expire_cache_for(post)
  6. end
  7. #IfoursweeperdetectsthataPostwasupdatedcall this
  8. defafter_update(post)
  9. expire_cache_for(post)
  10. end
  11. #IfoursweeperdetectsthataPostwasdeletedcall this
  12. defafter_destroy(post)
  13. expire_cache_for(post)
  14. end
  15. private
  16. defexpire_cache_for(record)
  17. #Expirethelistpagenowthatweposteda new blogentry
  18. expire_page(:controller=> 'blog' ,:action=> 'list' )
  19. #Alsoexpiretheshowpage,in case wejusteditablogentry
  20. expire_page(:controller=> 'blog' ,:action=> 'show' ,:id=>record.id)
  21. end
  22. end

然后我們在BlogController里加上該sweeper即可:
Java代碼 收藏代碼
  1. class BlogController<ApplicationController
  2. caches_page:list,:show
  3. cache_sweeper:blog_sweeper,\:only=>[:create,:update,:destroy]
  4. #...
  5. end

我們可以配置cache的靜態html文件的存放位置,這在config/environment.rb里設置:
Java代碼 收藏代碼
  1. config.action_controller.page_cache_directory=RAILS_ROOT+ "/public/cache/"

然后我們設置Apache/Lighttpd對于靜態html文件render時不接觸Rails server即可
所以Page Cache就是最快的Cache,因為它不與Rails server打交道,直接load靜態html

二、Action Cache
Action Cache相關的helper方法是caches_action和expire_action,其他基本和Page Cache一樣
另外我們還可以運行 rake tmp:cache:clear 來清空所有的Action Cache和Fragment Cache
Java代碼 收藏代碼
  1. class BlogController<ApplicationController
  2. before_filter:authentication
  3. caches_action:list,:show
  4. cache_sweeper:blog_sweeper,\:only=>[:create,:update,:destroy]

如上代碼所示,我們將authentication這個filter放在caches_action之前聲明,這樣我們的Action Cache在執行之前會先訪問authentication方法
這樣可以彌補Page Cache不能對需要登錄認證的Page進行Cache的缺點
生成的cache文件為tmp/cache/localhost:3000/blog/list.cache,這樣對不同subdomain的訪問頁面可以cache到不同的目錄
由于每次訪問Action Cache時都需要與Rails server打交道,并且要先運行filters,所以比Page Cache的效率稍低

三、Fragment Cache
Fragment Cache用于處理rhtml頁面中的部分需要cache的模塊,如app/views/blog/list.rhtml:
Java代碼 收藏代碼
  1. <strong>MyBlogPosts</strong>
  2. <%cache do %>
  3. <ul>
  4. <% for postin @posts %>
  5. <li><%=link_topost.title,:controller=> 'blog' ,:action=> 'show' ,:id=>post%></li>
  6. <%end%>
  7. </ul>
  8. <%end%>

生成的cache文件為/tmp/cache/localhost:3000/blog/list.cache
我們需要在BlogController的list方法里加上一行判斷,如果是讀取Fragment Cache,則不必再查詢一次數據庫:
Java代碼 收藏代碼
  1. deflist
  2. unlessread_fragment({})
  3. @post =Post.find(:all,\:order=> 'created_ondesc' ,:limit=> 10 )
  4. end
  5. end

Fragment分頁時的Cache:
Java代碼 收藏代碼
  1. deflist
  2. unlessread_fragment({:page=>params[:page]|| 1 })#Addthepageparamtothecachenaming
  3. @post_pages , @post =paginate:posts,:per_page=> 10
  4. end
  5. end

rhtml頁面也需要改寫:
Java代碼 收藏代碼
  1. <%cache({:page=>params[:page]|| 1 }) do %>
  2. ...Allofthehtmltodisplaytheposts...
  3. <%end%>

生成的cahce文件為/tmp/cache/localhost:3000/blog/list.page=1.cache
從分頁的Fragment Cache可以看出,Fragment Cache可以添加類似名字空間的東西,用于區分同一rhtml頁面的不同Fragment Cache,如:
Java代碼 收藏代碼
  1. cache( "turkey" )=> "/tmp/cache/turkey.cache"
  2. cache(:controller=> 'blog' ,:action=> 'show' ,:id=> 1 )=> "/tmp/cache/localhost:3000/blog/show/1.cache"
  3. cache( "blog/recent_posts" )=> "/tmp/cache/blog/recent_posts.cache"
  4. cache( "#{request.host_with_port}/blog/recent_posts" )=> "/tmp/cache/localhost:3000/blog/recent_posts.cache"

清除Fragment Cache的例子:
Java代碼 收藏代碼
  1. expire_fragment(:controller=> 'blog' ,:action=> 'list' ,:page=> 1 )
  2. expire_fragment(%r{blog/list.*})


四、ActiveRecord Cache
Rails Edge中ActiveRecord已經默認使用SQl Query Cache,對于同一action里面同一sql語句的數據庫操作會使用cache

五、memcached
越來越多的大型站點使用memcached做緩存來加快訪問速度
memcached是一個輕量的服務器進程,通過分配指定數量的內存來作為對象快速訪問的cache
memcached就是一個巨大的Hash表,我們可以存取和刪除key和value:
Java代碼 收藏代碼
  1. @tags =Tag.find:all
  2. Cache.put 'all_your_tags' , @tags
  3. Cache.put 'favorite_skateboarder' , 'TomPenny'
  4. skateboarder=Cache.get 'favorite_skateboarder'
  5. Cache.delete 'all_your_tags'

我們可以使用memcached做如下事情:
1,自動緩存數據庫的一行作為一個ActiveRecord對象
2,緩存render_to_string的結果
3,手動存儲復雜的數據庫查詢作為緩存
cached_model讓我們輕松的緩存ActiveRecord對象,memcahed-client包含在cached_model的安裝里,memcahed-client提供了緩存的delete方法
Java代碼 收藏代碼
  1. sudogeminstallcached_model

我們在config/environment.rb里添加如下代碼來使用memcached:
Java代碼 收藏代碼
  1. require 'cached_model'
  2. memcache_options={
  3. :c_threshold=>10_000,
  4. :compression=> true ,
  5. :debug=> false ,
  6. :namespace=> 'my_rails_app' ,
  7. :readonly=> false ,
  8. :urlencode=> false
  9. }
  10. CACHE=MemCache. new memcache_options
  11. CACHE.servers= 'localhost:11211'

對于production環境我們把上面的代碼挪到config/environments/production.rb里即可
然后讓我們的domain model集成CachedModel而不是ActiveRecord::Base
Java代碼 收藏代碼
  1. class Foo<CachedModel
  2. end

然后我們就可以使用memcached了:
Java代碼 收藏代碼
  1. all_foo=Foo.find:all
  2. Cache.put 'Foo:all' ,all_foo
  3. Cache.get 'Foo:all'

需要注意的幾點:
1,如果你查詢的item不在緩存里,Cache.get將返回nil,可以利用這點來判斷是否需要重新獲取數據并重新插入到緩存
2,CachedModel的記錄默認15分鐘后expire,可以通過設置CachedModel.ttl來修改expiration time
3,手動插入的對象也會過期,Cache.put('foo', 'bar', 60)將在60秒后過期
4,如果分配的內存用盡,則older items將被刪除
5,可以對每個app server啟動一個memcached實例,分配128MB的內存對一般的程序來說足夠

Rails的緩存


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 免费特黄一级欧美大片在线看 | 国产精品国偷自产在线 | 在线观看91精品国产入口 | 国产精品成人一区二区 | JLZZJLZZ日本人护士水好多 | 成人毛片免费网站 | 日韩在线观看网站 | 欧美亚洲视频一区 | 99国产精品久久久 | 国外成人在线视频 | 999久久久| 午夜免费 | 狠狠干天天 | 国产日产在线观看 | 成人免费影| 国产日韩欧美一区二区 | 欧美高清在线精品一区 | 午夜在线成人 | 91精品视频在线播放 | 久久亚洲热 | 中文欧美日韩 | 日韩免费福利视频 | 欧美不卡视频一区发布 | 久久亚洲一区二区 | 国产毛片久久精品 | 亚洲一区影院 | 久草手机在线视频 | 北岛玲亚洲一区在线观看 | 欧美啊啊啊 | 成人在线免费观看网站 | 亚洲视频观看 | 国产精品天天天天影视 | 久在线视频 | 色在线免费 | 欧美一级永久免费毛片在线 | 网站一区| 三级网站在线播放 | 亚洲狠狠婷婷综合久久久久图片 | 欧美日韩在线视频播放 | 亚洲激情一区二区 | 麻豆传媒地址 |