Rails caching

  • 1,954 views
Uploaded on

A brief overview of caching technics using Rails

A brief overview of caching technics using Rails

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,954
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
10
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
    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 Vidéos
    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. Sweepers
    config.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 only
    config.action_controller.page_cache_directory = RAILS_ROOT + “/…”
    config/environment.rb
    class Articles < ContentController
      # 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 Store
    config.action_controller.cache_store = :file_store, RAILS_ROOT + /public/cache“
    config/environment.rb
    class Articles < ContentController
      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 Cache Store
    - 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
  • Keep a clean cache
    • Corrupted cache isdangerous
    • 33. Maintaining a cache iscostly and addcomplexity
    • 34. Time basedflushing if usingmemcached
    class ArticleSweeper < ActionController::Caching::Sweeper
      # This sweeper observe articles model and delete cache on save
      observe :article
      
      def after_save(article)
        clear_article_cache(article)
      end
      def clear_article_cache(article)
          expire_pageindex_path, client
      end
    End
    app/sweepers/article_sweeper.rb
  • 35. Bypass the Rails Stack
    • Pages cachingneedyou to serve the file « manually »
    • 36. Nginx/apache rewrite rules
    • 37. Hack for subdomainif any
    # Check index CACHE
    set $requested_page $request_uri;
    # Initialize variables
    set $cache_path "/cache/views/$http_host/static/";
    if ($request_uri ~ ^/$){
    set $requested_page "index";
    }
    # Check if the page is in the cache.
    if (-f $document_root/$cache_path/$requested_page.html) {
    rewrite (.*) $cache_path/$requested_page.html break;
    }
  • 38. Who is caching?
    • Classiccaching ?
    • 39. UsingMemcached ?
    • 40. UsingRack::Cache ?
    • 41. Never cachedanything ?
  • memcached Power
    • Avoiding cache cleaningis possible !!
    • 42. :expire_in > 30.minutes
    • 43. Intelligent keys
    - cache(“#{article.id}-#{article.update_at.to_i}” -data) do
     = My heavy stuffs to cache
    # => /cache/3-1234567-data
    #Or just writing
    - cache(article) do
     = My heavy stuffs to cache
    # => /cache/articles/3-1234567
    #And if related to a specific user
    - cache([article,user]) do
     = My heavy stuffs to cache
    # => /cache/articles/3-1234567/users/23-12345678
  • 44. Thanks you folks !!