Your SlideShare is downloading. ×
0
Rails caching
Rails caching
Rails caching
Rails caching
Rails caching
Rails caching
Rails caching
Rails caching
Rails caching
Rails caching
Rails caching
Rails caching
Rails caching
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Rails caching

2,014

Published on

A brief overview of caching technics using Rails

A brief overview of caching technics using Rails

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,014
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
11
Comments
0
Likes
1
Embeds 0
No embeds

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. Caching with Rails<br />Vincent Spehner<br />Twitter: @vzmind<br />Blog: vzmind.tumblr.com<br />Janvier 2011<br />
  • 2. Vzmind aka Ruby on Rails creator<br />
  • 3. Why caching ?<br />Culture Pub<br />Almost 20 000 Vidéos<br />2 000 000 pages viewed<br />https://github.com/thinkdry/blank-application<br />
  • 4. Cache is everywhere<br /><ul><li>Almostateachstepbeetween the request and the data itself, a cache canbeadded
  • 5. Rails cachingkeyworld:
  • 6. Pages
  • 7. Action
  • 8. Fragment
  • 9. Sweepers</li></ul>config.action_controller.perform_caching = true<br />app/config/environments/production.rb<br />Proxy<br />(Squid, Varnish)<br />Web Server<br />(Apache, Nginx)<br />Rails Stack<br />(Rack, App)<br />DB (PostgreSQL, MySQL)<br />Browser<br /> Cache as earlier as youcan<br />
  • 10. Pages caching<br /><ul><li>Simpliest and mostdisconnectedfrom Rails Cache
  • 11. Easy to implement but monotlithic and limited
  • 12. Storedinside the File System only</li></ul>config.action_controller.page_cache_directory = RAILS_ROOT + “/…”<br />config/environment.rb<br />class Articles < ContentController<br />  # Add cache on articles index page<br />  caches_page :index<br />app/controllers/articles_controller.rb<br />Proxy<br />Web Server<br />Rails Stack<br />DB<br />Browser<br />public/cache/articles.html<br />
  • 13. Action caching<br /><ul><li> A bit flexible withparameters and pathbuilder
  • 14. Easy to implement
  • 15. Carefulwithfilters (Action cachingis an aroundfilteritself)
  • 16. Carefulwith cache path
  • 17. Chooseyour Cache Store</li></ul>config.action_controller.cache_store = :file_store, RAILS_ROOT + /public/cache“<br />config/environment.rb<br />class Articles < ContentController<br />  caches_action :show, <br />    :cache_path => Proc.new{ |controller|<br />      if controller.request.mobile_device? <br />        controller.params.merge(:mobile=>controller.request.mobile_device?)<br />      else<br />        controller.params<br />      end}<br />app/controllers/articles_controller.rb<br />expire_action :action => :show<br />App/sweepers/article_sweeper.rb<br />Proxy<br />Web Server<br />Rails Stack<br />DB<br />Browser<br />public/cache/articles.html<br />
  • 18. Fragment caching<br /><ul><li>Required for dynamicwebsites
  • 19. Atomiccaching (result/string/object)
  • 20. Chooseyour Cache Store</li></ul>- cache(:action => ‘list', :action_suffix => 'all_articles’) do<br /> = All articles:<br />-@articles.each do |article|<br />= article.title<br />app/views/articles/index.haml.html<br />expire_fragment(:controller => ‘articles', :action => ‘list', :action_suffix => 'all_articles')<br />app/sweepers/article_sweeper.rb<br />Proxy<br />Web Server<br />Rails Stack<br />DB<br />Browser<br />public/cache/articles.html<br />
  • 21. Overview of Cache Stores<br /><ul><li>ActiveSupport::Cache::MemoryStore
  • 22. Default memory storage
  • 23. Store everything (even object)
  • 24. Not thread safe
  • 25. ActiveSupport::Cache::FileStore
  • 26. Just for my HTML output
  • 27. Old school
  • 28. Simple
  • 29. ActiveSupport::Cache::MemCacheStore
  • 30. Most popularmemory cache store
  • 31. Multi servers
  • 32. Time-basedexpiry support</li></li></ul><li>Keep a clean cache<br /><ul><li>Corrupted cache isdangerous
  • 33. Maintaining a cache iscostly and addcomplexity
  • 34. Time basedflushing if usingmemcached</li></ul>class ArticleSweeper < ActionController::Caching::Sweeper<br />  # This sweeper observe articles model and delete cache on save<br />  observe :article<br />  <br />  def after_save(article)<br />    clear_article_cache(article)<br />  end<br />  def clear_article_cache(article)<br />      expire_pageindex_path, client<br />  end<br />End<br />app/sweepers/article_sweeper.rb<br />
  • 35. Bypass the Rails Stack<br /><ul><li> Pages cachingneedyou to serve the file « manually »
  • 36. Nginx/apache rewrite rules
  • 37. Hack for subdomainif any</li></ul># Check index CACHE <br />set $requested_page $request_uri; <br /># Initialize variables <br />set $cache_path "/cache/views/$http_host/static/"; <br />if ($request_uri ~ ^/$){<br />set $requested_page "index"; <br />}<br /># Check if the page is in the cache. <br />if (-f $document_root/$cache_path/$requested_page.html) { <br /> rewrite (.*) $cache_path/$requested_page.html break; <br />} <br />
  • 38. Who is caching?<br /><ul><li>Classiccaching ?
  • 39. UsingMemcached ?
  • 40. UsingRack::Cache ?
  • 41. Never cachedanything ?</li></li></ul><li>memcached Power<br /><ul><li>Avoiding cache cleaningis possible !!
  • 42. :expire_in > 30.minutes
  • 43. Intelligent keys</li></ul>- cache(“#{article.id}-#{article.update_at.to_i}” -data) do<br />  = My heavy stuffs to cache<br /># => /cache/3-1234567-data<br />#Or just writing<br />- cache(article) do<br />  = My heavy stuffs to cache<br /># => /cache/articles/3-1234567<br />#And if related to a specific user<br />- cache([article,user]) do<br />  = My heavy stuffs to cache<br /># => /cache/articles/3-1234567/users/23-12345678<br />
  • 44. Thanks you folks !!<br />

×