Rails caching

  • View
    2.010

  • Download
    7

Embed Size (px)

DESCRIPTION

A brief overview of caching technics using Rails

Text of Rails caching

  • 1.Caching with Rails
    Vincent Spehner
    Twitter: @vzmind
    Blog: vzmind.tumblr.com
    Janvier 2011

2. Vzmind aka Ruby on Rails creator
3. Why caching ?
Culture Pub
Almost 20 000 Vidos
2 000 000 pages viewed
https://github.com/thinkdry/blank-application
4. Cache is everywhere

  • Almostateachstepbeetween the request and the data itself, a cache canbeadded

5. Rails cachingkeyworld: 6. Pages 7. Action 8. Fragment 9. Sweepersconfig.action_controller.perform_caching = true
app/config/environments/production.rb
Proxy
(Squid, Varnish)
Web Server
(Apache, Nginx)
Rails Stack
(Rack,App)
DB (PostgreSQL, MySQL)
Browser
Cache as earlier as youcan
10. Pages caching

  • Simpliest and mostdisconnectedfrom Rails Cache

11. Easy to implement but monotlithic and limited 12. Storedinside the File System onlyconfig.action_controller.page_cache_directory = RAILS_ROOT + /
config/environment.rb
class Articles # Add cache on articles index page
caches_page :index
app/controllers/articles_controller.rb
Proxy
Web Server
Rails Stack
DB
Browser
public/cache/articles.html
13. Action caching

  • A bit flexible withparameters and pathbuilder

14. Easy to implement 15. Carefulwithfilters (Action cachingis an aroundfilteritself) 16. Carefulwith cache path 17. Chooseyour Cache Storeconfig.action_controller.cache_store = :file_store, RAILS_ROOT + /public/cache
config/environment.rb
class Articles caches_action :show,
:cache_path => Proc.new{ |controller|
if controller.request.mobile_device?
controller.params.merge(:mobile=>controller.request.mobile_device?)
else
controller.params
end}
app/controllers/articles_controller.rb
expire_action :action => :show
App/sweepers/article_sweeper.rb
Proxy
Web Server
Rails Stack
DB
Browser
public/cache/articles.html
18. Fragment caching

  • Required for dynamicwebsites

19. Atomiccaching (result/string/object) 20. Chooseyour CacheStore- cache(:action => list', :action_suffix => 'all_articles) do
= All articles:
-@articles.each do|article|
=article.title
app/views/articles/index.haml.html
expire_fragment(:controller => articles', :action => list', :action_suffix => 'all_articles')
app/sweepers/article_sweeper.rb
Proxy
Web Server
Rails Stack
DB
Browser
public/cache/articles.html
21. Overview of Cache Stores

  • 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