Rails Cache

  • Published on
    19-Jan-2015

  • View
    4.194

  • Download
    3

Embed Size (px)

DESCRIPTION

shanghaionrails first event presentation given by martin.xus

Transcript

<ul><li> 1. Rails Cache martin@recity.nethttp://www.recity.net </li></ul> <p> 2. About ReCity ReCity -- Shanghai On Rails 2007 3. Rails Cache Page Cache Action Cache Fragment CacheShanghai On Rails 2007 4. Use Cacheconfig.action_controller.perform_caching = trueShanghai On Rails 2007 5. Page Cache Rails Cached Page,Rails,Web Server HtmlShanghai On Rails 2007 6. Page Cache Example class UsersController &lt; ApplicationControllercaches_page :show def show end end Shanghai On Rails 2007 7. http://localhost/users/show/100 Rendering users/showCached page: /users/show/100.html $ ls public/users/show 100.html Shanghai On Rails 2007 8. Page Cache Sweeper class UserSweeper &lt; ActionController::Caching::Sweeperobserve Userdef after_update(record)expire_page :controller=&gt;:users,:action=&gt;:show,:id=&gt;record.id end endclass UsersController &lt; ApplicationController cache_sweeper :user_sweeper caches_page :showShanghai On Rails 2007 9. User.find(100).update_attribute :login,somethingExpired page: /users/show/100.html (0.00000)Shanghai On Rails 2007 10. Page Cache Problems Page Cachehtmlpublic 1. Ajax2. Client Javascript3. Action CacheCacheShanghai On Rails 2007 11. Page Cache Problems Page Cachesurl_for ?page=1?sort=score&amp;user_id=100&amp;category_id=1 1routes /users/list/page 2cacheShanghai On Rails 2007 12. Action Cache Page Cache Action Cache controller 1.before_filter 2.cachecache 3. Page Cache Fragment Cache Shanghai On Rails 2007 13. Action Cache Example Page Cachecaches_actionexpire_actionclass UsersController &lt; ApplicationController before_filter :authenticate caches_page :show cache_sweeper :user_sweeper def show endendShanghai On Rails 2007 14. http://localhost/users/show/100Rendering users/show Cached fragment: localhost/users/show/100 (0.00000)$ ls -al tmp/cache/localhost/users/show/* tmp/cache/localhost/users/show/100.cacheFragment read: localhost/users/show/100 (0.00000)Shanghai On Rails 2007 15. expire_action :controller=&gt;:users,:action=&gt;:show,:id=&gt;record.idShanghai On Rails 2007 16. Action Cache Problem caches_action_with_paramsexpire_action_with_paramsShanghai On Rails 2007 17. Fragment Cache cache /tmp/cache.cache expire_fragment Shanghai On Rails 2007 18. Fragment Cache Example </p> <p> Shanghai On Rails 2007 19. unless read_fragment() @users = User.paginate :all, :page=&gt;params[:page]||1endShanghai On Rails 2007 20. Fragment Cache fragment cache url_forcache(:action=&gt;show,:part=&gt;users) cache(:action=&gt;show,:part=&gt;users,:page=&gt;1 [,]) localhost/users/show?page=1 localhost/users/show?page=1&amp;part=usersexpire_fragment [params]Shanghai On Rails 2007 21. Action/Fragment Cache DRB MemCached Shanghai On Rails 2007 22. Expiring Strategy Model Others Shanghai On Rails 2007 23. Useful Plugins sweeper generator timed_fragment_cache cache_fu cached_model Shanghai On Rails 2007 24. sweeper generator./script/generate sweeper SweeperName callback1 callback2Shanghai On Rails 2007 25. timed_fragment_cache View #the cached fragment which does something intensive Controllerwhen_fragment_expired 'fragment_name', 10.minutes_from_now do #some intensive codeend Shanghai On Rails 2007 26. cache_fu class User &lt; ActiveRecord::Base acts_as_cached endShanghai On Rails 2007 27. CachedModel class User &lt; CachedModel end Shanghai On Rails 2007 28. More Mode Cache Query Cache(Edge Rails) Shanghai On Rails 2007 29. Questions? Martin@Recity.NetShanghai On Rails 2007 </p>