亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Rails的緩存

系統 1681 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條評論
主站蜘蛛池模板: 日本一级特黄毛片免费视频9 | 欧美亚洲国产成人高清在线 | 国产欧美日韩一区 | 欧美成人免费夜夜黄啪啪 | 大尺度视频网站久久久久久久久 | 美国一级毛片片免费 | 不卡一区二区在线 | 99久久精品免费精品国产 | 久久在线播放 | 久久综合九色综合91 | 日本一区色 | 国产成人综合久久 | 久久99精品久久久久久野外 | 波多野结衣一区在线 | 91精品啪在线观看国产色 | 欧美做爱毛片 | 黑人欧美一级毛片 | 夜夜夜爽爽爽久久久 | 色视频免费国产观看 | 精品成人久久 | 伊人久久青草青青综合 | 国产一区二区在线 |播放 | 在线成人精品国产区免费 | 99热久久这里只有精品7 | 永久黄网站色视频免费 | 日日干天天插 | 午夜精品久久久 | 日日夜操 | 国产高清自拍 | 4huh34四虎最新888 | 欧美香蕉爽爽人人爽观看猫咪 | 欧美激情在线一区二区三区 | 日韩夜夜操 | 欧美成人精品一级高清片 | 亚洲欧洲在线观看 | 亚洲欧美第一页 | 青青热在线精品视频免费 | 2022久久国产精品免费热麻豆 | 老司机亚洲精品影院在线 | 爱爱免费观看高清视频在线播放 | 国产天堂视频 |