Rails caching
Upcoming SlideShare
Loading in...5
×
 

Rails caching

on

  • 2,247 views

A brief overview of caching technics using Rails

A brief overview of caching technics using Rails

Statistics

Views

Total Views
2,247
Views on SlideShare
2,161
Embed Views
86

Actions

Likes
1
Downloads
9
Comments
0

3 Embeds 86

http://vzmind.tumblr.com 84
http://www.tumblr.com 1
http://theoldreader.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Rails caching Rails caching Presentation Transcript

  • Caching with Rails
    Vincent Spehner
    Twitter: @vzmind
    Blog: vzmind.tumblr.com
    Janvier 2011
  • Vzmind aka Ruby on Rails creator
  • Why caching ?
    Culture Pub
    Almost 20 000 Vidéos
    2 000 000 pages viewed
    https://github.com/thinkdry/blank-application
  • Cache is everywhere
    • Almostateachstepbeetween the request and the data itself, a cache canbeadded
    • Rails cachingkeyworld:
    • Pages
    • Action
    • Fragment
    • 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
  • Pages caching
    • Simpliest and mostdisconnectedfrom Rails Cache
    • Easy to implement but monotlithic and limited
    • 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
  • Action caching
    • A bit flexible withparameters and pathbuilder
    • Easy to implement
    • Carefulwithfilters (Action cachingis an aroundfilteritself)
    • Carefulwith cache path
    • 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
  • Fragment caching
    • Required for dynamicwebsites
    • Atomiccaching (result/string/object)
    • 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
  • Overview of Cache Stores
    • ActiveSupport::Cache::MemoryStore
    • Default memory storage
    • Store everything (even object)
    • Not thread safe
    • ActiveSupport::Cache::FileStore
    • Just for my HTML output
    • Old school
    • Simple
    • ActiveSupport::Cache::MemCacheStore
    • Most popularmemory cache store
    • Multi servers
    • Time-basedexpiry support
  • Keep a clean cache
    • Corrupted cache isdangerous
    • Maintaining a cache iscostly and addcomplexity
    • 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
  • Bypass the Rails Stack
    • Pages cachingneedyou to serve the file « manually »
    • Nginx/apache rewrite rules
    • 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;
    }
  • Who is caching?
    • Classiccaching ?
    • UsingMemcached ?
    • UsingRack::Cache ?
    • Never cachedanything ?
  • memcached Power
    • Avoiding cache cleaningis possible !!
    • :expire_in > 30.minutes
    • 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
  • Thanks you folks !!