0
Caching with Rails<br />Vincent Spehner<br />Twitter: @vzmind<br />Blog: vzmind.tumblr.com<br />Janvier 2011<br />
Vzmind aka Ruby on Rails creator<br />
Why caching ?<br />Culture Pub<br />Almost 20 000 Vidéos<br />2 000 000 pages viewed<br />https://github.com/thinkdry/blan...
Cache is everywhere<br /><ul><li>Almostateachstepbeetween the request and the data itself, a cache canbeadded
 Rails cachingkeyworld:
Pages
 Action
Fragment
Sweepers</li></ul>config.action_controller.perform_caching = true<br />app/config/environments/production.rb<br />Proxy<br...
Pages caching<br /><ul><li>Simpliest and mostdisconnectedfrom Rails Cache
Easy to implement but monotlithic and limited
Storedinside the File System only</li></ul>config.action_controller.page_cache_directory = RAILS_ROOT + “/…”<br />config/e...
Action caching<br /><ul><li> A bit flexible withparameters and pathbuilder
Upcoming SlideShare
Loading in...5
×

Rails caching

2,016

Published on

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,016
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
11
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Rails caching"

  1. 1. Caching with Rails<br />Vincent Spehner<br />Twitter: @vzmind<br />Blog: vzmind.tumblr.com<br />Janvier 2011<br />
  2. 2. Vzmind aka Ruby on Rails creator<br />
  3. 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. 4. Cache is everywhere<br /><ul><li>Almostateachstepbeetween the request and the data itself, a cache canbeadded
  5. 5. Rails cachingkeyworld:
  6. 6. Pages
  7. 7. Action
  8. 8. Fragment
  9. 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. 10. Pages caching<br /><ul><li>Simpliest and mostdisconnectedfrom Rails Cache
  11. 11. Easy to implement but monotlithic and limited
  12. 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. 13. Action caching<br /><ul><li> A bit flexible withparameters and pathbuilder
  14. 14. Easy to implement
  15. 15. Carefulwithfilters (Action cachingis an aroundfilteritself)
  16. 16. Carefulwith cache path
  17. 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. 18. Fragment caching<br /><ul><li>Required for dynamicwebsites
  19. 19. Atomiccaching (result/string/object)
  20. 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. 21. Overview of Cache Stores<br /><ul><li>ActiveSupport::Cache::MemoryStore
  22. 22. Default memory storage
  23. 23. Store everything (even object)
  24. 24. Not thread safe
  25. 25. ActiveSupport::Cache::FileStore
  26. 26. Just for my HTML output
  27. 27. Old school
  28. 28. Simple
  29. 29. ActiveSupport::Cache::MemCacheStore
  30. 30. Most popularmemory cache store
  31. 31. Multi servers
  32. 32. Time-basedexpiry support</li></li></ul><li>Keep a clean cache<br /><ul><li>Corrupted cache isdangerous
  33. 33. Maintaining a cache iscostly and addcomplexity
  34. 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. 35. Bypass the Rails Stack<br /><ul><li> Pages cachingneedyou to serve the file « manually »
  36. 36. Nginx/apache rewrite rules
  37. 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. 38. Who is caching?<br /><ul><li>Classiccaching ?
  39. 39. UsingMemcached ?
  40. 40. UsingRack::Cache ?
  41. 41. Never cachedanything ?</li></li></ul><li>memcached Power<br /><ul><li>Avoiding cache cleaningis possible !!
  42. 42. :expire_in > 30.minutes
  43. 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. 44. Thanks you folks !!<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×