Kickin' Ass with Cache-Fu (without notes)
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Kickin' Ass with Cache-Fu (without notes)

  • 15,589 views
Uploaded on

My memcached talk at RailsConf 2007 (without notes). See the blog post: http://errtheblog.com/post/4872

My memcached talk at RailsConf 2007 (without notes). See the blog post: http://errtheblog.com/post/4872

More in: Technology , Design
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • Excellent. You've shown your credibility on presentation with this slideshow. This one deserves thumbs up. I'm John, owner of www.freeringtones.ws/ . Perhaps to see more quality slides from you.

    Best wishes.
    Are you sure you want to
    Your message goes here
  • outstanding display..convinced me to have a hardlook at my company model..brilliant

    Janie
    http://financejedi.com
    http://healthjedi.com
    Are you sure you want to
    Your message goes here
  • cool stuff!
    Are you sure you want to
    Your message goes here
  • There's also a version with notes: http://www.slideshare.net/err/kickin-ass-with-cachefu/
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
15,589
On Slideshare
15,571
From Embeds
18
Number of Embeds
2

Actions

Shares
Downloads
241
Comments
4
Likes
11

Embeds 18

http://www.slideshare.net 17
http://webcache.googleusercontent.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. CHRIS WANSTRATH ERR FREE [ http://errfree.com ]
  • 2. 50,000,000 pages no downtime
  • 3. memcached.
  • 4. Memcaching Rails CHRIS WANSTRATH ERR FREE [ http://errfree.com ]
  • 5. Memcaching Rails CHRIS WANSTRATH ERR FREE [ http://errfree.com ]
  • 6. chris wanstrath railsconf 2007 kickin’ ass with cache-fu
  • 7. {}
  • 8. class Memcache < Hash undef :each, :keys end
  • 9. class Memcache < DRbHash undef :each, :keys end
  • 10. $ memcached -vv <3 server listening <7 new client connection <7 get app-test:Story:1 >7 END <7 set app-test:Story:2 0 >7 STORED <7 delete app-test:Story:1 >7 DELETED
  • 11. $ memcached -vv <3 server listening <7 new client connection <7 get app-test:Story:1 >7 END <7 set app-test:Story:2 0 >7 STORED <7 delete app-test:Story:1 >7 DELETED
  • 12. YAGNI
  • 13. UYRDNI
  • 14. UYRDNI (unless you really do need it)
  • 15. class Presentation < ActiveRecord::Base def self.get_cache(id) if data = @cache.get(id) data else data = find(id) @cache.set(id, data) data end end end
  • 16. class Presentation < ActiveRecord::Base def self.get_cache(id) @cache.get(id) || @cache.set(id, find(id)) end end
  • 17. Fragments Actions Sessions & Objects
  • 18. memcache-client
  • 19. memcache-client $ gem install memcache-client
  • 20. topfunky memcached
  • 21. CachedModel
  • 22. Fragment Cache Store
  • 23. Session Store
  • 24. cache_fu
  • 25. cache_fu ( acts_as_cached 2.0 )
  • 26. Fragments Actions Sessions & Objects
  • 27. acts_as_cached
  • 28. config/memcached.yml defaults: ttl: 1800 namespace: railsconf sessions: false fragments: false servers: localhost:11211
  • 29. config/memcached.yml defaults: ttl: 1800 namespace: railsconf sessions: true fragments: true servers: localhost:11211
  • 30. config/memcached.yml production: benchmarking: false sessions: true fragments: true servers: - 192.185.254.121:11211 - 192.185.254.138:11211 - 192.185.254.160:11211
  • 31. config/memcached.yml production: benchmarking: false sessions: true fragments: true servers: - 192.185.254.121:11211 - 192.185.254.138:11211 - 192.185.254.160:11211
  • 32. class Presentation < ActiveRecord::Base acts_as_cached end
  • 33. get_cache expire_cache
  • 34. class Presentation < ActiveRecord::Base acts_as_cached after_save :expire_cache end
  • 35. class Presentation < ActiveRecord::Base def self.get_cache(id) if data = @cache.get(id) data else data = find(id) @cache.set(id, data) data end end end
  • 36. class Presentation < ActiveRecord::Base def self.get_cache(id) if not (data = @cache.get(id)).nil? data else data = find(id) @cache.set(id, data) data end end end
  • 37. class Presentation < ActiveRecord::Base def self.get_cache(id) if not (data = @cache.get(id)).nil? data else data = find(id) || false @cache.set(id, data) data end end end
  • 38. Presentation.get_cache(1337)
  • 39. class Presentation < ActiveRecord::Base acts_as_cached :conditions => 'published = 1' end
  • 40. class Presentation < ActiveRecord::Base acts_as_cached :finder => :find_live end
  • 41. Topic.find :all, :conditions => [quot;created_at > ?quot;, 1.week.ago], :order => 'post_count desc', :limit => 5
  • 42. class Topic < ActiveRecord::Base def self.weekly_popular(limit = 5) find :all, :conditions => [quot;created_at > ?quot;, 1.week.ago], :order => 'post_count desc', :limit => limit end end
  • 43. Topic.weekly_popular
  • 44. DB: 0.00 (0%)
  • 45. class Topic < ActiveRecord::Base def self.cached_weekly_popular get_cache(:weekly_popular) do weekly_popular end end end
  • 46. Topic.cached_weekly_popular
  • 47. ruby mocha
  • 48. bdd test spec
  • 49. A Ruby object acting as cached - should be able to retrieve a cached version of itself - should be able to set itself to the cache - should pass its cached self into a block when supplied - should be able to expire its cache - should be able to reset its cache - should be able to tell if it is cached - should be able to set itself to the cache with an arbitrary ttl Finished in 0.028509 seconds. 28 specifications (53 requirements), 0 failures
  • 50. context quot;Calling #cached_weekly_popularquot; do specify quot;should call #weekly_popular if not cachedquot; do Topic.expects(:fetch_cache).returns(nil) Topic.cached_weekly_popular.should.equal Topic.weekly_popular end specify quot;should return if cachedquot; do Topic.expects(:get_cache).returns(true) Topic.expects(:weekly_popular).never Topic.cached_weekly_popular end end
  • 51. Topic.cached(:weekly_popular)
  • 52. def self.cache_key_with_date(id) date = Date.today.to_s.tr(' ', '_') cache_key_without_date(id) + ':' + date end class << self alias_method_chain :cache_key, :date end
  • 53. class Topic < ActiveRecord::Base def self.date_for_key Date.today.to_s.tr(' ', '_') end def self.cached_weekly_popular key = 'weekly_popular' + date_for_key get_cache(key) { weekly_popular } end end
  • 54. Topic.find(1, 2, 3)
  • 55. Topic.get_cache(1, 2, 3)
  • 56. user_ids = @topic.posts.map(&:user_id).uniq @users = User.get_cache(user_ids)
  • 57. class ApplicationController before_filter :local_cache_for_request end
  • 58. # pulls from memcache @user = User.get_cache(1) # pulls from local cache @user = User.get_cache(1)
  • 59. class ApplicationController before_filter :set_cache_override def set_cache_override returning true do ActsAsCached.skip_cache_gets = !!params[:skip_cache] end end end
  • 60. www.mysite.com/home?skip_cache=1
  • 61. reset_cache
  • 62. @topic.set_cache
  • 63. class Presentation < ActiveRecord::Base acts_as_cached after_save :reset_cache end
  • 64. class Presentation < ActiveRecord::Base acts_as_cached :version => 1 end
  • 65. monit
  • 66. libketama
  • 67. 1 600 200 400
  • 68. 1 600 cache_get :railsconf 200 400
  • 69. 1 :railsconf == 100 600 200 400
  • 70. 1 :railsconf == 200 600 200 400
  • 71. 1 :railsconf == 200 600 200
  • 72. 1 700 :railsconf == 200 600 200 500 300 400
  • 73. l33t h4x0rs • Geoffrey Grosenbach • Rob Sanheim • Ryan King • Lourens Naudé • Michael Moen • Corey Donohoe • PJ Hyett • Eric Hodel
  • 74. {} ( thanks. any questions? )
  • 75. http://flickr.com/photos/seibuone/144588686/ http://flickr.com/photos/j00zt1n/255430115/ http://flickr.com/photos/jameswong/145397570/ http://flickr.com/photos/xalpha/58368229/ http://flickr.com/photos/63503896@N00/35723413/ http://flickr.com/photos/mrcrash/145451993/ thanks http://flickr.com/photos/psybernoid/398301743/ http://flickr.com/photos/45royale/422227291/ flickr http://flickr.com/photos/andrson/420810541/ http://flickr.com/photos/joshuaweiland/370931770/ http://flickr.com/photos/zesmerelda/27258314/ http://flickr.com/photos/slice/390271923/ http://flickr.com/photos/cocoen/411960476/ http://flickr.com/photos/pinguino/198885132/ http://flickr.com/photos/davidfmiller/468476118/ http://laughingsquid.com - Scott Beale http://flickr.com/photos/bail56/313536999/ http://flickr.com/photos/65995199@N00/272672183/