SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Downloaden Sie, um offline zu lesen
Caching tips




     Leo Soto M.
     Continuum
     Lecture & Beef, Marzo 2013

Sunday, March 3, 13
Caching tips
     (for webapps)




     Leo Soto M.
     Continuum
     Lecture & Beef, Marzoss 2013

Sunday, March 3, 13
“There are only
      two hard things
      in computer
      science...



Sunday, March 3, 13
...cache
                      invalidation and
                      naming things”

                      - Phil Karlton


Sunday, March 3, 13
HTTP Caching

                      •Client-side!
                      •...or “middle-side” (proxys!)
                      •Conceptos claves:
                       •Frescura (freshness)
                       •Validacion/Invalidacion

Sunday, March 3, 13
FRESH CACHES


                •HTTP/1.0:
                      Expires:	
  Fri,	
  01	
  March	
  2013	
  20:00:00	
  GMT

                •HTTP/1.1:
                      Cache-­‐Control:	
  *pila-­‐de-­‐opciones*
                      Cache-­‐Control:	
  private,	
  max-­‐age=3600




Sunday, March 3, 13
VALIDATED CACHES

                •HTTP/1.0:
                      Last-­‐Modified	
  /	
  If-­‐Modified-­‐Since
                      304	
  Not	
  Modified

                •HTTP/1.1:
                      ETag	
  /	
  If-­‐None-­‐Match
                      304	
  Not	
  Modified




Sunday, March 3, 13
CACHE INVALIDATION

                •METODOS HTTP QUE INVALIDAN
                      CACHES FRESCAS:
                      POST	
  /resource
                      DELETE	
  /resource
                      PUT	
  /resource




Sunday, March 3, 13
EN RAILS

                •FRESH CACHES:
                      expires_in	
  1.hour



                •VALIDATED CACHES
                      if	
  stale?(etag:	
  @modelo)
                      	
  	
  otros_hits_a_la_bd
                      end

Sunday, March 3, 13
stale?(etag:	
  x)	
  -­‐>	
  Etag:	
  md5(expand_cache_key(x))
     # File activesupport/lib/active_support/cache.rb, line 77
     def expand_cache_key(key, namespace = nil)
       expanded_cache_key = namespace ? "#{namespace}/" : ""

          if prefix = ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]
            expanded_cache_key << "#{prefix}/"
          end

       expanded_cache_key << retrieve_cache_key(key)
       expanded_cache_key
     end

   # File activesupport/lib/active_support/cache.rb, line 90
   def retrieve_cache_key(key)
     case
     when key.respond_to?(:cache_key) then key.cache_key
     when key.is_a?(Array)            then key.map { |element|
       retrieve_cache_key(element)
     }.to_param
     else
     end.to_s
   end

Sunday, March 3, 13
# File activerecord/lib/active_record/integration.rb, line 37
    def cache_key
      case
      when new_record?
        "#{self.class.model_name.cache_key}/new"
      when timestamp = self[:updated_at]
        timestamp = timestamp.utc.to_s(:number)
        "#{self.class.model_name.cache_key}/#{id}-#{timestamp}"
      else
        "#{self.class.model_name.cache_key}/#{id}"
      end
    end




Sunday, March 3, 13
EN RAILS



                •SIN ETAGGER
                      stale?(etag:	
  [current_user,	
  @modelo])
                      stale?(etag:	
  [current_user,	
  @otro])
                      stale?(etag:	
  [current_user,	
  @otromas])




Sunday, March 3, 13
EN RAILS


                •CON ETAGGER
                      etag	
  {	
  current_user	
  }
                      stale?(etag:	
  modelo)
                      stale?(etag:	
  otro)
                      stale?(etag:	
  otromas)



Sunday, March 3, 13
EN RAILS


                •EN AC PERFORACIONES
                      etag	
  {	
  [current_user,	
  ENV[‘COMMIT_HASH’]	
  }
                      stale?(etag:	
  modelo)
                      stale?(etag:	
  otro)
                      stale?(etag:	
  otromas)



Sunday, March 3, 13
Rails Fragment
                         Caching
                      -­‐cache	
  ‘foo’	
  do...end
                      -­‐cache	
  modelo	
  do...end
                      -­‐cache	
  [reporte,	
  ‘inbox’]	
  do..end


   # File actionpack/lib/action_controller/caching/fragments.rb, line 51
   def fragment_cache_key(key)
     ActiveSupport::Cache.expand_cache_key(
       key.is_a?(Hash) ?
         url_for(key).split("://").last :
         key,
     :views)
   end

Sunday, March 3, 13
RUSSIAN DOLL
                                 CACHING

                      -­‐	
  cache	
  [reporte,	
  ‘perforaciones’]	
  do
                      	
  	
  -­‐	
  perforacion.each	
  do	
  |p|
                      	
  	
  	
  	
  -­‐	
  cache	
  perforacion	
  do
                      	
  	
  	
  	
  	
  	
  %h1=	
  perforacion.titulo
                      	
  	
  	
  	
  	
  	
  %p=	
  perforacion.whatever




Sunday, March 3, 13
TIPS FRAGMENT
                           CACHING


                •Aplicar a cosas que no
                      dependan del usuario actual
                •Usar cache_digests +
                      memcached




Sunday, March 3, 13
CONCLUSIONES
                •HTTP CACHING:
                 •CACHE LOCAL AL USUARIO
                 •AHORRA:
                  •LATENCIA
                  •ANCHO DE BANDA,
                  •TIEMPO DE RESPUESTA DEL
                       SERVIDOR

Sunday, March 3, 13
CONCLUSIONES
                •FRAGMENT CACHING:
                 •CACHE GLOBAL DE LA APP
                 •AHORRA:
                  •HITS A LA BD
                  •CALCULOS
                  •RENDERING
Sunday, March 3, 13
BONUS TRACK
    def prefetch(url)
      content_tag("script",
        %Q{
        $("<link href='#{escape_javascript(url)}'
                 rel='subresource'/>").appendTo('head')
        }.html_safe
      ) unless current_page?(url)
    end


    def tab_detalle_reporte(text, opts = {})
       collection = opts[:collection] || text.downcase.pluralize
       url = send("reporte_#{collection}_path", estado, reporte)
       tab_title(opts.merge!(
         name: text,
         href: url,
         active: tab == collection
       )) + prefetch(url)
     end

Sunday, March 3, 13

Weitere ähnliche Inhalte

Ähnlich wie Caching tips

Cassandra Community Webinar | Cassandra 2.0 - Better, Faster, Stronger
Cassandra Community Webinar | Cassandra 2.0 - Better, Faster, StrongerCassandra Community Webinar | Cassandra 2.0 - Better, Faster, Stronger
Cassandra Community Webinar | Cassandra 2.0 - Better, Faster, StrongerDataStax
 
Beautiful PHP CLI Scripts
Beautiful PHP CLI ScriptsBeautiful PHP CLI Scripts
Beautiful PHP CLI ScriptsJesse Donat
 
Basics of Metaprogramming in Ruby
Basics of Metaprogramming in RubyBasics of Metaprogramming in Ruby
Basics of Metaprogramming in RubyDigital Natives
 
S3 & Glacier - The only backup solution you'll ever need
S3 & Glacier - The only backup solution you'll ever needS3 & Glacier - The only backup solution you'll ever need
S3 & Glacier - The only backup solution you'll ever needMatthew Boeckman
 
Introduction to Twig
Introduction to TwigIntroduction to Twig
Introduction to Twigmarkstory
 
Oracle to Postgres Migration - part 2
Oracle to Postgres Migration - part 2Oracle to Postgres Migration - part 2
Oracle to Postgres Migration - part 2PgTraining
 
Solr Troubleshooting - TreeMap approach
Solr Troubleshooting - TreeMap approachSolr Troubleshooting - TreeMap approach
Solr Troubleshooting - TreeMap approachAlexandre Rafalovitch
 
Solr Troubleshooting - Treemap Approach: Presented by Alexandre Rafolovitch, ...
Solr Troubleshooting - Treemap Approach: Presented by Alexandre Rafolovitch, ...Solr Troubleshooting - Treemap Approach: Presented by Alexandre Rafolovitch, ...
Solr Troubleshooting - Treemap Approach: Presented by Alexandre Rafolovitch, ...Lucidworks
 
Introduction to NoSQL with MongoDB
Introduction to NoSQL with MongoDBIntroduction to NoSQL with MongoDB
Introduction to NoSQL with MongoDBHector Correa
 
Laying out the future
Laying out the futureLaying out the future
Laying out the futureChris Mills
 
D3.js capita selecta
D3.js capita selectaD3.js capita selecta
D3.js capita selectaJoris Klerkx
 
Lcna tutorial-2012
Lcna tutorial-2012Lcna tutorial-2012
Lcna tutorial-2012Gluster.org
 
Lcna 2012-tutorial
Lcna 2012-tutorialLcna 2012-tutorial
Lcna 2012-tutorialGluster.org
 
The WordPress Hacker's Guide to the \Galaxy() [@MidwestPHP]
The WordPress Hacker's Guide to the \Galaxy() [@MidwestPHP]The WordPress Hacker's Guide to the \Galaxy() [@MidwestPHP]
The WordPress Hacker's Guide to the \Galaxy() [@MidwestPHP]Jason Rhodes
 
The WordPress Hacker's Guide to the \Galaxy() [@Baltimore PHP]
The WordPress Hacker's Guide to the \Galaxy() [@Baltimore PHP]The WordPress Hacker's Guide to the \Galaxy() [@Baltimore PHP]
The WordPress Hacker's Guide to the \Galaxy() [@Baltimore PHP]Jason Rhodes
 
CSS3 and jQuery for Designers
CSS3 and jQuery for DesignersCSS3 and jQuery for Designers
CSS3 and jQuery for Designerslomalogue
 
Deprecating ActiveRecord Attributes without making Zombies
Deprecating ActiveRecord Attributes without making ZombiesDeprecating ActiveRecord Attributes without making Zombies
Deprecating ActiveRecord Attributes without making Zombiesyann ARMAND
 

Ähnlich wie Caching tips (20)

Cassandra Community Webinar | Cassandra 2.0 - Better, Faster, Stronger
Cassandra Community Webinar | Cassandra 2.0 - Better, Faster, StrongerCassandra Community Webinar | Cassandra 2.0 - Better, Faster, Stronger
Cassandra Community Webinar | Cassandra 2.0 - Better, Faster, Stronger
 
Beautiful PHP CLI Scripts
Beautiful PHP CLI ScriptsBeautiful PHP CLI Scripts
Beautiful PHP CLI Scripts
 
Basics of Metaprogramming in Ruby
Basics of Metaprogramming in RubyBasics of Metaprogramming in Ruby
Basics of Metaprogramming in Ruby
 
S3 & Glacier - The only backup solution you'll ever need
S3 & Glacier - The only backup solution you'll ever needS3 & Glacier - The only backup solution you'll ever need
S3 & Glacier - The only backup solution you'll ever need
 
Introduction to Twig
Introduction to TwigIntroduction to Twig
Introduction to Twig
 
Rails Intro & Tutorial
Rails Intro & TutorialRails Intro & Tutorial
Rails Intro & Tutorial
 
Oracle to Postgres Migration - part 2
Oracle to Postgres Migration - part 2Oracle to Postgres Migration - part 2
Oracle to Postgres Migration - part 2
 
Solr Troubleshooting - TreeMap approach
Solr Troubleshooting - TreeMap approachSolr Troubleshooting - TreeMap approach
Solr Troubleshooting - TreeMap approach
 
Solr Troubleshooting - Treemap Approach: Presented by Alexandre Rafolovitch, ...
Solr Troubleshooting - Treemap Approach: Presented by Alexandre Rafolovitch, ...Solr Troubleshooting - Treemap Approach: Presented by Alexandre Rafolovitch, ...
Solr Troubleshooting - Treemap Approach: Presented by Alexandre Rafolovitch, ...
 
Introduction to NoSQL with MongoDB
Introduction to NoSQL with MongoDBIntroduction to NoSQL with MongoDB
Introduction to NoSQL with MongoDB
 
What's new in Rails5?
What's new in Rails5?What's new in Rails5?
What's new in Rails5?
 
Laying out the future
Laying out the futureLaying out the future
Laying out the future
 
D3.js capita selecta
D3.js capita selectaD3.js capita selecta
D3.js capita selecta
 
Lcna tutorial-2012
Lcna tutorial-2012Lcna tutorial-2012
Lcna tutorial-2012
 
Lcna 2012-tutorial
Lcna 2012-tutorialLcna 2012-tutorial
Lcna 2012-tutorial
 
The WordPress Hacker's Guide to the \Galaxy() [@MidwestPHP]
The WordPress Hacker's Guide to the \Galaxy() [@MidwestPHP]The WordPress Hacker's Guide to the \Galaxy() [@MidwestPHP]
The WordPress Hacker's Guide to the \Galaxy() [@MidwestPHP]
 
The WordPress Hacker's Guide to the \Galaxy() [@Baltimore PHP]
The WordPress Hacker's Guide to the \Galaxy() [@Baltimore PHP]The WordPress Hacker's Guide to the \Galaxy() [@Baltimore PHP]
The WordPress Hacker's Guide to the \Galaxy() [@Baltimore PHP]
 
Wphackergalaxy
WphackergalaxyWphackergalaxy
Wphackergalaxy
 
CSS3 and jQuery for Designers
CSS3 and jQuery for DesignersCSS3 and jQuery for Designers
CSS3 and jQuery for Designers
 
Deprecating ActiveRecord Attributes without making Zombies
Deprecating ActiveRecord Attributes without making ZombiesDeprecating ActiveRecord Attributes without making Zombies
Deprecating ActiveRecord Attributes without making Zombies
 

Mehr von Leonardo Soto

El arte oscuro de estimar v3
El arte oscuro de estimar v3El arte oscuro de estimar v3
El arte oscuro de estimar v3Leonardo Soto
 
Una historia de ds ls en ruby
Una historia de ds ls en rubyUna historia de ds ls en ruby
Una historia de ds ls en rubyLeonardo Soto
 
El Lado Cool de Java
El Lado Cool de JavaEl Lado Cool de Java
El Lado Cool de JavaLeonardo Soto
 
Mi Arsenal de Testing en Rails
Mi Arsenal de Testing en RailsMi Arsenal de Testing en Rails
Mi Arsenal de Testing en RailsLeonardo Soto
 
Mapas en la web con Cloudmade
Mapas en la web con CloudmadeMapas en la web con Cloudmade
Mapas en la web con CloudmadeLeonardo Soto
 
Decent exposure: Controladores sin @ivars
Decent exposure: Controladores sin @ivarsDecent exposure: Controladores sin @ivars
Decent exposure: Controladores sin @ivarsLeonardo Soto
 
Sounds.gd lighting talk (RubyConf Uruguay)
Sounds.gd lighting talk (RubyConf Uruguay)Sounds.gd lighting talk (RubyConf Uruguay)
Sounds.gd lighting talk (RubyConf Uruguay)Leonardo Soto
 
Un tour por Java, Scala, Python, Ruby y Javascript
Un tour por Java, Scala, Python, Ruby y JavascriptUn tour por Java, Scala, Python, Ruby y Javascript
Un tour por Java, Scala, Python, Ruby y JavascriptLeonardo Soto
 
Javascript funcional
Javascript funcionalJavascript funcional
Javascript funcionalLeonardo Soto
 
Jython: Python para la plataforma Java (EL2009)
Jython: Python para la plataforma Java (EL2009)Jython: Python para la plataforma Java (EL2009)
Jython: Python para la plataforma Java (EL2009)Leonardo Soto
 
Jython: Python para la plataforma Java (JRSL 09)
Jython: Python para la plataforma Java (JRSL 09)Jython: Python para la plataforma Java (JRSL 09)
Jython: Python para la plataforma Java (JRSL 09)Leonardo Soto
 

Mehr von Leonardo Soto (20)

El arte oscuro de estimar v3
El arte oscuro de estimar v3El arte oscuro de estimar v3
El arte oscuro de estimar v3
 
Una historia de ds ls en ruby
Una historia de ds ls en rubyUna historia de ds ls en ruby
Una historia de ds ls en ruby
 
El Lado Cool de Java
El Lado Cool de JavaEl Lado Cool de Java
El Lado Cool de Java
 
Dos Años de Rails
Dos Años de RailsDos Años de Rails
Dos Años de Rails
 
Dos años de Rails
Dos años de RailsDos años de Rails
Dos años de Rails
 
Mi Arsenal de Testing en Rails
Mi Arsenal de Testing en RailsMi Arsenal de Testing en Rails
Mi Arsenal de Testing en Rails
 
Mapas en la web con Cloudmade
Mapas en la web con CloudmadeMapas en la web con Cloudmade
Mapas en la web con Cloudmade
 
Startechconf
StartechconfStartechconf
Startechconf
 
RabbitMQ
RabbitMQRabbitMQ
RabbitMQ
 
Decent exposure: Controladores sin @ivars
Decent exposure: Controladores sin @ivarsDecent exposure: Controladores sin @ivars
Decent exposure: Controladores sin @ivars
 
The Hashrocket Way
The Hashrocket WayThe Hashrocket Way
The Hashrocket Way
 
Sounds.gd lighting talk (RubyConf Uruguay)
Sounds.gd lighting talk (RubyConf Uruguay)Sounds.gd lighting talk (RubyConf Uruguay)
Sounds.gd lighting talk (RubyConf Uruguay)
 
Un tour por Java, Scala, Python, Ruby y Javascript
Un tour por Java, Scala, Python, Ruby y JavascriptUn tour por Java, Scala, Python, Ruby y Javascript
Un tour por Java, Scala, Python, Ruby y Javascript
 
Oss
OssOss
Oss
 
Javascript funcional
Javascript funcionalJavascript funcional
Javascript funcional
 
App Engine
App EngineApp Engine
App Engine
 
Introducción a Git
Introducción a GitIntroducción a Git
Introducción a Git
 
Tres Gemas De Ruby
Tres Gemas De RubyTres Gemas De Ruby
Tres Gemas De Ruby
 
Jython: Python para la plataforma Java (EL2009)
Jython: Python para la plataforma Java (EL2009)Jython: Python para la plataforma Java (EL2009)
Jython: Python para la plataforma Java (EL2009)
 
Jython: Python para la plataforma Java (JRSL 09)
Jython: Python para la plataforma Java (JRSL 09)Jython: Python para la plataforma Java (JRSL 09)
Jython: Python para la plataforma Java (JRSL 09)
 

Kürzlich hochgeladen

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

Kürzlich hochgeladen (20)

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

Caching tips

  • 1. Caching tips Leo Soto M. Continuum Lecture & Beef, Marzo 2013 Sunday, March 3, 13
  • 2. Caching tips (for webapps) Leo Soto M. Continuum Lecture & Beef, Marzoss 2013 Sunday, March 3, 13
  • 3. “There are only two hard things in computer science... Sunday, March 3, 13
  • 4. ...cache invalidation and naming things” - Phil Karlton Sunday, March 3, 13
  • 5. HTTP Caching •Client-side! •...or “middle-side” (proxys!) •Conceptos claves: •Frescura (freshness) •Validacion/Invalidacion Sunday, March 3, 13
  • 6. FRESH CACHES •HTTP/1.0: Expires:  Fri,  01  March  2013  20:00:00  GMT •HTTP/1.1: Cache-­‐Control:  *pila-­‐de-­‐opciones* Cache-­‐Control:  private,  max-­‐age=3600 Sunday, March 3, 13
  • 7. VALIDATED CACHES •HTTP/1.0: Last-­‐Modified  /  If-­‐Modified-­‐Since 304  Not  Modified •HTTP/1.1: ETag  /  If-­‐None-­‐Match 304  Not  Modified Sunday, March 3, 13
  • 8. CACHE INVALIDATION •METODOS HTTP QUE INVALIDAN CACHES FRESCAS: POST  /resource DELETE  /resource PUT  /resource Sunday, March 3, 13
  • 9. EN RAILS •FRESH CACHES: expires_in  1.hour •VALIDATED CACHES if  stale?(etag:  @modelo)    otros_hits_a_la_bd end Sunday, March 3, 13
  • 10. stale?(etag:  x)  -­‐>  Etag:  md5(expand_cache_key(x)) # File activesupport/lib/active_support/cache.rb, line 77 def expand_cache_key(key, namespace = nil) expanded_cache_key = namespace ? "#{namespace}/" : "" if prefix = ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"] expanded_cache_key << "#{prefix}/" end expanded_cache_key << retrieve_cache_key(key) expanded_cache_key end # File activesupport/lib/active_support/cache.rb, line 90 def retrieve_cache_key(key) case when key.respond_to?(:cache_key) then key.cache_key when key.is_a?(Array) then key.map { |element| retrieve_cache_key(element) }.to_param else end.to_s end Sunday, March 3, 13
  • 11. # File activerecord/lib/active_record/integration.rb, line 37 def cache_key case when new_record? "#{self.class.model_name.cache_key}/new" when timestamp = self[:updated_at] timestamp = timestamp.utc.to_s(:number) "#{self.class.model_name.cache_key}/#{id}-#{timestamp}" else "#{self.class.model_name.cache_key}/#{id}" end end Sunday, March 3, 13
  • 12. EN RAILS •SIN ETAGGER stale?(etag:  [current_user,  @modelo]) stale?(etag:  [current_user,  @otro]) stale?(etag:  [current_user,  @otromas]) Sunday, March 3, 13
  • 13. EN RAILS •CON ETAGGER etag  {  current_user  } stale?(etag:  modelo) stale?(etag:  otro) stale?(etag:  otromas) Sunday, March 3, 13
  • 14. EN RAILS •EN AC PERFORACIONES etag  {  [current_user,  ENV[‘COMMIT_HASH’]  } stale?(etag:  modelo) stale?(etag:  otro) stale?(etag:  otromas) Sunday, March 3, 13
  • 15. Rails Fragment Caching -­‐cache  ‘foo’  do...end -­‐cache  modelo  do...end -­‐cache  [reporte,  ‘inbox’]  do..end # File actionpack/lib/action_controller/caching/fragments.rb, line 51 def fragment_cache_key(key) ActiveSupport::Cache.expand_cache_key( key.is_a?(Hash) ? url_for(key).split("://").last : key, :views) end Sunday, March 3, 13
  • 16. RUSSIAN DOLL CACHING -­‐  cache  [reporte,  ‘perforaciones’]  do    -­‐  perforacion.each  do  |p|        -­‐  cache  perforacion  do            %h1=  perforacion.titulo            %p=  perforacion.whatever Sunday, March 3, 13
  • 17. TIPS FRAGMENT CACHING •Aplicar a cosas que no dependan del usuario actual •Usar cache_digests + memcached Sunday, March 3, 13
  • 18. CONCLUSIONES •HTTP CACHING: •CACHE LOCAL AL USUARIO •AHORRA: •LATENCIA •ANCHO DE BANDA, •TIEMPO DE RESPUESTA DEL SERVIDOR Sunday, March 3, 13
  • 19. CONCLUSIONES •FRAGMENT CACHING: •CACHE GLOBAL DE LA APP •AHORRA: •HITS A LA BD •CALCULOS •RENDERING Sunday, March 3, 13
  • 20. BONUS TRACK def prefetch(url) content_tag("script", %Q{ $("<link href='#{escape_javascript(url)}' rel='subresource'/>").appendTo('head') }.html_safe ) unless current_page?(url) end def tab_detalle_reporte(text, opts = {}) collection = opts[:collection] || text.downcase.pluralize url = send("reporte_#{collection}_path", estado, reporte) tab_title(opts.merge!( name: text, href: url, active: tab == collection )) + prefetch(url) end Sunday, March 3, 13