SlideShare una empresa de Scribd logo
1 de 39
Descargar para leer sin conexión
« Cachez-moi cette page ! »




Atelier Pratique
1h30


Hugo Hamon (@hhamon)
http://hugohamon.com
Qui suis-je ?
Au menu de cet atelier
1.  Introduction
2.  Avantages
3.  Expiration (Expires & Cache-Control)
4.  Validation (Etag & Last-Modi ed)
5.  Reverse Proxy Cache
6.  Edge Side Includes
Introduction au Cache HTTP

§  Mai 1996 – RFC1945 (HTTP 1.0)

§  Mars 1999 – RFC2616 (HTTP 1.1)

         http://www.ietf.org/rfc/rfc2616.txt
         http://tools.ietf.org/wg/httpbis/
Pourquoi cacher ?

§    Ne pas générer la même réponse deux fois
§    Diminuer la charge sur le serveur web
§    Diminuer la bande passante
§    Diminuer les temps de chargement
§    Servir plus de monde avec moins de serveurs

§  Améliorer l’expérience utilisateur
Objectifs ?


Etre le plus dynamique et le
plus performant en sollicitant
l’application le moins
possible.
Types de caches
           Browser Cache

Browser




                                          Gateway Cache
                           Proxy Cache

                                                          Web Server
           Browser Cache




Browser



   Client-Side Caching                   Server-Side Caching
Quels sont les contenus cachables ?

Seules les réponses à des requêtes GET et
HEAD peuvent être cachées car elles ne
changent pas l’état de la ressource.

Les requêtes POST, PUT et DELETE ne sont
pas cachables !
Stratégies




             Expiration
Expiration


Détermine la durée pendant laquelle une
réponse doit être considérée « fraîche » par le
cache. Au delà de cette période, la ressource
est considérée comme « périmée ».
 Avantages : soulager la charge du serveur web
Expiration




             Expires
Expires
$expires = new DateTime('2011-10-15 11:00:00');   PHP
$expires->setTimezone(new DateTimeZone('UTC'));
$date = $expires->format('D, d M Y H:i:s');

header(sprintf('Expires: %s GMT', $date));


HTTP/1.1 200 OK
Date: Thu, 18 Aug 2011 18:19:10 GMT
Expires: Sat, 15 Oct 2011 09:00:00 GMT
Content-Type: text/html

<html>
    ...
</html>
                                                   HTTP
Expiration




             Cache-Control
Cache-Control
                                               PHP
header('HTTP/1.1 200 OK');
header('Cache-Control: private, maxage=60');


HTTP/1.1 200 OK
Date: Thu, 18 Aug 2011 18:29:30 GMT
Cache-Control: private, maxage=60
Content-Type: text/html

<html>
    ...
</html>                                        HTTP
Stratégies




             Validation
Validation

Détermine si une ressource a changé depuis
la dernière demande du client en marquant
cette dernière à l’aide d’un identi ant ou
d’un tampon.

 Avantages : diminuer le tra c sur le réseau
Validation




         304 est votre ami !
Validation



                  Etag
             If-None-Match
Entity Tag
// Generate the resource etag         PHP
$etag = 'abcdef';

header('HTTP/1.1 200 OK');
header('Etag: '. $etag);


HTTP/1.1 200 OK
Date: Thu, 18 Aug 2011 19:33:12 GMT
Etag: abcdef
Content-Type: text/html

<html>
    ...
</html>                               HTTP
If-None-Match

                                                     PHP
// Generate the resource etag
$etag = 'abcdef';

if (isset($_SERVER['HTTP_IF_NONE_MATCH'])
    && $etag === $_SERVER['HTTP_IF_NONE_MATCH']) {

    header('HTTP/1.1 304 Not Modified');
    exit;
}
If-None-Match

                            HTTP
GET /etag.php HTTP/1.1
Host: www.paris-web.local
If-None-Match: abcdef



                            HTTP
HTTP/1.1 304 Not Modified
Validation



               Last-Modi ed
             If-Modi ed-Since
Last-Modi ed
// Determine the last modified date            PHP
$date = 'Sat, 12 Aug 2011 10:00:00 GMT';

header('HTTP/1.1 200 OK');
header('Last-Modified: '. $date);


HTTP/1.1 200 OK
Date: Thu, 18 Aug 2011 20:07:55 GMT
Last-Modified: Sat, 12 Aug 2011 10:00:00 GMT
Content-Type: text/html

<html>
    ...
</html>                                        HTTP
If-Modi ed-Since

                                                    PHP
// Determine the last modified date
$date = 'Sat, 12 Aug 2011 10:00:00 GMT';

if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
    && $date === $_SERVER['HTTP_IF_MODIFIED_SINCE']) {

    header('HTTP/1.1 304 Not Modified');
    exit;
}
If-Modi ed-Since

                                                   HTTP
GET /last-modified.php HTTP/1.1
Host: www.paris-web.local
If-Modified-Since: Sat, 12 Aug 2011 10:00:00 GMT



                                                   HTTP
HTTP/1.1 304 Not Modified
Validation & Expiration


Combiner les deux stratégies reste possible
en sachant que l’expiration l’emporte
d’abord sur la validation.
Cache côté serveur


        Reverse Proxy Cache
          Surrogate Cache
          Gateway Caches
Reverse Proxy Cache


Un reverse proxy cache siège devant le
serveur web, intercepte les requêtes
entrantes et retourne les réponses fraîches
de son cache.
Quelques caches intermédiaires connus…


            Squid
           Varnish
      Mod_Cache (Apache)
Varnish-Cache.org
Con guration de Varnish
# Make Varnish listen to port 80
backend default {
      .host = "127.0.0.1";
      .port = "80";
}

# Add ESI support header to all incoming requests
sub vcl_recv {
    set req.http.Surrogate-Capability = "abc=ESI/1.0";
}

# Remove Surrogate-Control header from response headers
# And parse the response for ESI
sub vcl_fetch {
    if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
        unset beresp.http.Surrogate-Control;
        set beresp.do_esi = true;
    }
}
Cacher des réponses dans Varnish


            Cache-Control
               Public
           Shared MaxAge
Cacher des réponses dans Varnish
                                                PHP
header('HTTP/1.1 200 OK');
header('Cache-Control: public, s-maxage=60');


HTTP/1.1 200 OK
Date: Thu, 18 Aug 2011 20:54:08 GMT
Cache-Control: public, s-maxage=60
Content-Type: text/html

<html>
    ...
</html>
                                                HTTP
Cacher tout en restant dynamique



         Edge Side Includes
         http://www.w3.org/TR/esi-lang
Edge Side Includes




  <esi:include src="/banner.html"/>
Edge Side Includes



                     Non caché
Edge Side Includes



                               3600 seconds




                     <esi:include … />
Edge Side Includes
                                                                                               http://paris-web.local/index.php
                http://paris-web.local/index.php
         1                                                                              2


                                                                                            Lorem ipsum dolor sit




                                                                  Reverse Proxy Cache
                                                                                            amet, consectetur
                                                                                                                         <esi:include >
                                                                                            adipiscing elit. Praesent eu




                                                                                                                                           Serveur Web
                                                                                            odio eget eros vehicula
                                                                                            pulvinar id sed turpis.
Client




                                                                                            Vivamus a velit quam,
             Lorem ipsum dolor sit          Lorem ipsum                                     auctor euismod tortor.
             amet, consectetur              dolor sit amet,
             adipiscing elit. Praesent eu   consectetur
             odio eget eros vehicula        adipiscing elit                                    http://paris-web.local/sidebar.html
             pulvinar id sed turpis.                                                    3
             Vivamus a velit quam,
             auctor euismod tortor.                                                                                      Lorem ipsum
                                                                                                                         dolor sit amet,
                                                                                                                         consectetur
                                                              4                                                          adipiscing elit
« Happy Caching! »




Questions ?



Hugo Hamon (@hhamon)
http://hugohamon.com

Más contenido relacionado

Destacado

Speed up your developments with Symfony2
Speed up your developments with Symfony2Speed up your developments with Symfony2
Speed up your developments with Symfony2Hugo Hamon
 
Designing for developers
Designing for developersDesigning for developers
Designing for developersKirsten Hunter
 
This stuff is cool, but...HOW CAN I GET MY COMPANY TO DO IT?
This stuff is cool, but...HOW CAN I GET MY COMPANY TO DO IT?This stuff is cool, but...HOW CAN I GET MY COMPANY TO DO IT?
This stuff is cool, but...HOW CAN I GET MY COMPANY TO DO IT?Mark Heckler
 
API 101 Workshop from APIStrat Conference
API 101 Workshop from APIStrat ConferenceAPI 101 Workshop from APIStrat Conference
API 101 Workshop from APIStrat ConferenceKirsten Hunter
 
Prototyping in the cloud
Prototyping in the cloudPrototyping in the cloud
Prototyping in the cloudKirsten Hunter
 
Symfony2 en pièces détachées
Symfony2 en pièces détachéesSymfony2 en pièces détachées
Symfony2 en pièces détachéesHugo Hamon
 
Design Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleDesign Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleHugo Hamon
 
Symfony2 - Un Framework PHP 5 Performant
Symfony2 - Un Framework PHP 5 PerformantSymfony2 - Un Framework PHP 5 Performant
Symfony2 - Un Framework PHP 5 PerformantHugo Hamon
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistenceHugo Hamon
 
Monitor the quality of your Symfony projects
Monitor the quality of your Symfony projectsMonitor the quality of your Symfony projects
Monitor the quality of your Symfony projectsHugo Hamon
 
Symfony2 - extending the console component
Symfony2 - extending the console componentSymfony2 - extending the console component
Symfony2 - extending the console componentHugo Hamon
 
Quantifying your-fitness
Quantifying your-fitnessQuantifying your-fitness
Quantifying your-fitnessKirsten Hunter
 
Api intensive - What they Are
Api intensive - What they AreApi intensive - What they Are
Api intensive - What they AreKirsten Hunter
 

Destacado (20)

Api 101
Api 101Api 101
Api 101
 
Speed up your developments with Symfony2
Speed up your developments with Symfony2Speed up your developments with Symfony2
Speed up your developments with Symfony2
 
Designing for developers
Designing for developersDesigning for developers
Designing for developers
 
Facebook appsincloud
Facebook appsincloudFacebook appsincloud
Facebook appsincloud
 
API First
API FirstAPI First
API First
 
Quantifying fitness
Quantifying fitnessQuantifying fitness
Quantifying fitness
 
This stuff is cool, but...HOW CAN I GET MY COMPANY TO DO IT?
This stuff is cool, but...HOW CAN I GET MY COMPANY TO DO IT?This stuff is cool, but...HOW CAN I GET MY COMPANY TO DO IT?
This stuff is cool, but...HOW CAN I GET MY COMPANY TO DO IT?
 
API 101 Workshop from APIStrat Conference
API 101 Workshop from APIStrat ConferenceAPI 101 Workshop from APIStrat Conference
API 101 Workshop from APIStrat Conference
 
Prototyping in the cloud
Prototyping in the cloudPrototyping in the cloud
Prototyping in the cloud
 
Symfony2 en pièces détachées
Symfony2 en pièces détachéesSymfony2 en pièces détachées
Symfony2 en pièces détachées
 
Design Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleDesign Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et Pimple
 
Symfony2 - Un Framework PHP 5 Performant
Symfony2 - Un Framework PHP 5 PerformantSymfony2 - Un Framework PHP 5 Performant
Symfony2 - Un Framework PHP 5 Performant
 
Liberating your data
Liberating your dataLiberating your data
Liberating your data
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
 
Monitor the quality of your Symfony projects
Monitor the quality of your Symfony projectsMonitor the quality of your Symfony projects
Monitor the quality of your Symfony projects
 
Polyglot copy
Polyglot copyPolyglot copy
Polyglot copy
 
Symfony2 - extending the console component
Symfony2 - extending the console componentSymfony2 - extending the console component
Symfony2 - extending the console component
 
Quantifying your-fitness
Quantifying your-fitnessQuantifying your-fitness
Quantifying your-fitness
 
Git store
Git storeGit store
Git store
 
Api intensive - What they Are
Api intensive - What they AreApi intensive - What they Are
Api intensive - What they Are
 

Similar a Développeurs, cachez-moi ça ! (Paris Web 2011)

La mise en cache et ses secrets
La mise en cache et ses secretsLa mise en cache et ses secrets
La mise en cache et ses secretsAymeric Bouillat
 
BordeauxJUG : Portails &amp; Portlets Java
BordeauxJUG : Portails &amp; Portlets JavaBordeauxJUG : Portails &amp; Portlets Java
BordeauxJUG : Portails &amp; Portlets JavaCamblor Frédéric
 
Ajax - GTI780 & MTI780 - ETS - A08
Ajax - GTI780 & MTI780 - ETS - A08Ajax - GTI780 & MTI780 - ETS - A08
Ajax - GTI780 & MTI780 - ETS - A08Claude Coulombe
 
Installation Et Configuration De Nutch
Installation Et Configuration De NutchInstallation Et Configuration De Nutch
Installation Et Configuration De NutchMohamed Ben Bouzid
 
Ajax GTI780 & MTI780 ETS A09
Ajax  GTI780 & MTI780  ETS  A09Ajax  GTI780 & MTI780  ETS  A09
Ajax GTI780 & MTI780 ETS A09Claude Coulombe
 
Drupal 8, symfony
Drupal 8, symfonyDrupal 8, symfony
Drupal 8, symfonyjeUXdiCode
 
Présentation complète de l'HTML5
Présentation complète de l'HTML5Présentation complète de l'HTML5
Présentation complète de l'HTML5jverrecchia
 
HTML5... La révolution maintenant!
HTML5... La révolution maintenant!HTML5... La révolution maintenant!
HTML5... La révolution maintenant!CARA_Lyon
 
HTML5... La révolution maintenant!
HTML5... La révolution maintenant!HTML5... La révolution maintenant!
HTML5... La révolution maintenant!CARA_Lyon
 
Rich Desktop Applications
Rich Desktop ApplicationsRich Desktop Applications
Rich Desktop Applicationsgoldoraf
 
Share point 2013 distributed cache
Share point 2013 distributed cacheShare point 2013 distributed cache
Share point 2013 distributed cacheMichael Nokhamzon
 
Communications Réseaux et HTTP avec PHP
Communications Réseaux et HTTP avec PHPCommunications Réseaux et HTTP avec PHP
Communications Réseaux et HTTP avec PHPjulien pauli
 
Presentation Tomcat Load Balancer
Presentation Tomcat Load BalancerPresentation Tomcat Load Balancer
Presentation Tomcat Load Balancertarkaus
 
Le blog technique de laurent besson(1)
Le blog technique de laurent besson(1)Le blog technique de laurent besson(1)
Le blog technique de laurent besson(1)boblapointe
 
Resource Oriented Architecture
Resource Oriented ArchitectureResource Oriented Architecture
Resource Oriented ArchitectureDNG Consulting
 
Ajax en Java - GTI780 & MTI780 - ETS - A08
Ajax en Java - GTI780 & MTI780 - ETS - A08Ajax en Java - GTI780 & MTI780 - ETS - A08
Ajax en Java - GTI780 & MTI780 - ETS - A08Claude Coulombe
 
Kiwiparty 2011 - Optimisation des sites internet
Kiwiparty 2011 - Optimisation des sites internetKiwiparty 2011 - Optimisation des sites internet
Kiwiparty 2011 - Optimisation des sites internetDevclic
 

Similar a Développeurs, cachez-moi ça ! (Paris Web 2011) (20)

La mise en cache et ses secrets
La mise en cache et ses secretsLa mise en cache et ses secrets
La mise en cache et ses secrets
 
BordeauxJUG : Portails &amp; Portlets Java
BordeauxJUG : Portails &amp; Portlets JavaBordeauxJUG : Portails &amp; Portlets Java
BordeauxJUG : Portails &amp; Portlets Java
 
Ajax - GTI780 & MTI780 - ETS - A08
Ajax - GTI780 & MTI780 - ETS - A08Ajax - GTI780 & MTI780 - ETS - A08
Ajax - GTI780 & MTI780 - ETS - A08
 
Installation Et Configuration De Nutch
Installation Et Configuration De NutchInstallation Et Configuration De Nutch
Installation Et Configuration De Nutch
 
PHP et PHP Framework
PHP et PHP FrameworkPHP et PHP Framework
PHP et PHP Framework
 
Ajax GTI780 & MTI780 ETS A09
Ajax  GTI780 & MTI780  ETS  A09Ajax  GTI780 & MTI780  ETS  A09
Ajax GTI780 & MTI780 ETS A09
 
Drupal 8, symfony
Drupal 8, symfonyDrupal 8, symfony
Drupal 8, symfony
 
Présentation complète de l'HTML5
Présentation complète de l'HTML5Présentation complète de l'HTML5
Présentation complète de l'HTML5
 
HTML5... La révolution maintenant!
HTML5... La révolution maintenant!HTML5... La révolution maintenant!
HTML5... La révolution maintenant!
 
HTML5... La révolution maintenant!
HTML5... La révolution maintenant!HTML5... La révolution maintenant!
HTML5... La révolution maintenant!
 
Rich Desktop Applications
Rich Desktop ApplicationsRich Desktop Applications
Rich Desktop Applications
 
Support de cours angular
Support de cours angularSupport de cours angular
Support de cours angular
 
Share point 2013 distributed cache
Share point 2013 distributed cacheShare point 2013 distributed cache
Share point 2013 distributed cache
 
Meetup angular rshop
Meetup angular rshopMeetup angular rshop
Meetup angular rshop
 
Communications Réseaux et HTTP avec PHP
Communications Réseaux et HTTP avec PHPCommunications Réseaux et HTTP avec PHP
Communications Réseaux et HTTP avec PHP
 
Presentation Tomcat Load Balancer
Presentation Tomcat Load BalancerPresentation Tomcat Load Balancer
Presentation Tomcat Load Balancer
 
Le blog technique de laurent besson(1)
Le blog technique de laurent besson(1)Le blog technique de laurent besson(1)
Le blog technique de laurent besson(1)
 
Resource Oriented Architecture
Resource Oriented ArchitectureResource Oriented Architecture
Resource Oriented Architecture
 
Ajax en Java - GTI780 & MTI780 - ETS - A08
Ajax en Java - GTI780 & MTI780 - ETS - A08Ajax en Java - GTI780 & MTI780 - ETS - A08
Ajax en Java - GTI780 & MTI780 - ETS - A08
 
Kiwiparty 2011 - Optimisation des sites internet
Kiwiparty 2011 - Optimisation des sites internetKiwiparty 2011 - Optimisation des sites internet
Kiwiparty 2011 - Optimisation des sites internet
 

Más de Hugo Hamon

Database Design Patterns
Database Design PatternsDatabase Design Patterns
Database Design PatternsHugo Hamon
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & RESTHugo Hamon
 
Intégration Continue PHP avec Jenkins CI
Intégration Continue PHP avec Jenkins CIIntégration Continue PHP avec Jenkins CI
Intégration Continue PHP avec Jenkins CIHugo Hamon
 
Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Hugo Hamon
 
Intégration continue des projets PHP avec Jenkins
Intégration continue des projets PHP avec JenkinsIntégration continue des projets PHP avec Jenkins
Intégration continue des projets PHP avec JenkinsHugo Hamon
 
Mieux Développer en PHP avec Symfony
Mieux Développer en PHP avec SymfonyMieux Développer en PHP avec Symfony
Mieux Développer en PHP avec SymfonyHugo Hamon
 
Introduction à Symfony2
Introduction à Symfony2Introduction à Symfony2
Introduction à Symfony2Hugo Hamon
 
Exposer des services web SOAP et REST avec symfony 1.4 et Zend Framework
Exposer des services web SOAP et REST avec symfony 1.4 et Zend FrameworkExposer des services web SOAP et REST avec symfony 1.4 et Zend Framework
Exposer des services web SOAP et REST avec symfony 1.4 et Zend FrameworkHugo Hamon
 

Más de Hugo Hamon (8)

Database Design Patterns
Database Design PatternsDatabase Design Patterns
Database Design Patterns
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & REST
 
Intégration Continue PHP avec Jenkins CI
Intégration Continue PHP avec Jenkins CIIntégration Continue PHP avec Jenkins CI
Intégration Continue PHP avec Jenkins CI
 
Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2
 
Intégration continue des projets PHP avec Jenkins
Intégration continue des projets PHP avec JenkinsIntégration continue des projets PHP avec Jenkins
Intégration continue des projets PHP avec Jenkins
 
Mieux Développer en PHP avec Symfony
Mieux Développer en PHP avec SymfonyMieux Développer en PHP avec Symfony
Mieux Développer en PHP avec Symfony
 
Introduction à Symfony2
Introduction à Symfony2Introduction à Symfony2
Introduction à Symfony2
 
Exposer des services web SOAP et REST avec symfony 1.4 et Zend Framework
Exposer des services web SOAP et REST avec symfony 1.4 et Zend FrameworkExposer des services web SOAP et REST avec symfony 1.4 et Zend Framework
Exposer des services web SOAP et REST avec symfony 1.4 et Zend Framework
 

Développeurs, cachez-moi ça ! (Paris Web 2011)

  • 1. « Cachez-moi cette page ! » Atelier Pratique 1h30 Hugo Hamon (@hhamon) http://hugohamon.com
  • 3. Au menu de cet atelier 1.  Introduction 2.  Avantages 3.  Expiration (Expires & Cache-Control) 4.  Validation (Etag & Last-Modi ed) 5.  Reverse Proxy Cache 6.  Edge Side Includes
  • 4. Introduction au Cache HTTP §  Mai 1996 – RFC1945 (HTTP 1.0) §  Mars 1999 – RFC2616 (HTTP 1.1) http://www.ietf.org/rfc/rfc2616.txt http://tools.ietf.org/wg/httpbis/
  • 5. Pourquoi cacher ? §  Ne pas générer la même réponse deux fois §  Diminuer la charge sur le serveur web §  Diminuer la bande passante §  Diminuer les temps de chargement §  Servir plus de monde avec moins de serveurs §  Améliorer l’expérience utilisateur
  • 6. Objectifs ? Etre le plus dynamique et le plus performant en sollicitant l’application le moins possible.
  • 7. Types de caches Browser Cache Browser Gateway Cache Proxy Cache Web Server Browser Cache Browser Client-Side Caching Server-Side Caching
  • 8. Quels sont les contenus cachables ? Seules les réponses à des requêtes GET et HEAD peuvent être cachées car elles ne changent pas l’état de la ressource. Les requêtes POST, PUT et DELETE ne sont pas cachables !
  • 9. Stratégies Expiration
  • 10. Expiration Détermine la durée pendant laquelle une réponse doit être considérée « fraîche » par le cache. Au delà de cette période, la ressource est considérée comme « périmée ». Avantages : soulager la charge du serveur web
  • 11. Expiration Expires
  • 12. Expires $expires = new DateTime('2011-10-15 11:00:00'); PHP $expires->setTimezone(new DateTimeZone('UTC')); $date = $expires->format('D, d M Y H:i:s'); header(sprintf('Expires: %s GMT', $date)); HTTP/1.1 200 OK Date: Thu, 18 Aug 2011 18:19:10 GMT Expires: Sat, 15 Oct 2011 09:00:00 GMT Content-Type: text/html <html> ... </html> HTTP
  • 13. Expiration Cache-Control
  • 14. Cache-Control PHP header('HTTP/1.1 200 OK'); header('Cache-Control: private, maxage=60'); HTTP/1.1 200 OK Date: Thu, 18 Aug 2011 18:29:30 GMT Cache-Control: private, maxage=60 Content-Type: text/html <html> ... </html> HTTP
  • 15. Stratégies Validation
  • 16. Validation Détermine si une ressource a changé depuis la dernière demande du client en marquant cette dernière à l’aide d’un identi ant ou d’un tampon. Avantages : diminuer le tra c sur le réseau
  • 17. Validation 304 est votre ami !
  • 18. Validation Etag If-None-Match
  • 19. Entity Tag // Generate the resource etag PHP $etag = 'abcdef'; header('HTTP/1.1 200 OK'); header('Etag: '. $etag); HTTP/1.1 200 OK Date: Thu, 18 Aug 2011 19:33:12 GMT Etag: abcdef Content-Type: text/html <html> ... </html> HTTP
  • 20. If-None-Match PHP // Generate the resource etag $etag = 'abcdef'; if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $etag === $_SERVER['HTTP_IF_NONE_MATCH']) { header('HTTP/1.1 304 Not Modified'); exit; }
  • 21. If-None-Match HTTP GET /etag.php HTTP/1.1 Host: www.paris-web.local If-None-Match: abcdef HTTP HTTP/1.1 304 Not Modified
  • 22. Validation Last-Modi ed If-Modi ed-Since
  • 23. Last-Modi ed // Determine the last modified date PHP $date = 'Sat, 12 Aug 2011 10:00:00 GMT'; header('HTTP/1.1 200 OK'); header('Last-Modified: '. $date); HTTP/1.1 200 OK Date: Thu, 18 Aug 2011 20:07:55 GMT Last-Modified: Sat, 12 Aug 2011 10:00:00 GMT Content-Type: text/html <html> ... </html> HTTP
  • 24. If-Modi ed-Since PHP // Determine the last modified date $date = 'Sat, 12 Aug 2011 10:00:00 GMT'; if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $date === $_SERVER['HTTP_IF_MODIFIED_SINCE']) { header('HTTP/1.1 304 Not Modified'); exit; }
  • 25. If-Modi ed-Since HTTP GET /last-modified.php HTTP/1.1 Host: www.paris-web.local If-Modified-Since: Sat, 12 Aug 2011 10:00:00 GMT HTTP HTTP/1.1 304 Not Modified
  • 26. Validation & Expiration Combiner les deux stratégies reste possible en sachant que l’expiration l’emporte d’abord sur la validation.
  • 27. Cache côté serveur Reverse Proxy Cache Surrogate Cache Gateway Caches
  • 28. Reverse Proxy Cache Un reverse proxy cache siège devant le serveur web, intercepte les requêtes entrantes et retourne les réponses fraîches de son cache.
  • 29. Quelques caches intermédiaires connus… Squid Varnish Mod_Cache (Apache)
  • 31. Con guration de Varnish # Make Varnish listen to port 80 backend default { .host = "127.0.0.1"; .port = "80"; } # Add ESI support header to all incoming requests sub vcl_recv { set req.http.Surrogate-Capability = "abc=ESI/1.0"; } # Remove Surrogate-Control header from response headers # And parse the response for ESI sub vcl_fetch { if (beresp.http.Surrogate-Control ~ "ESI/1.0") { unset beresp.http.Surrogate-Control; set beresp.do_esi = true; } }
  • 32. Cacher des réponses dans Varnish Cache-Control Public Shared MaxAge
  • 33. Cacher des réponses dans Varnish PHP header('HTTP/1.1 200 OK'); header('Cache-Control: public, s-maxage=60'); HTTP/1.1 200 OK Date: Thu, 18 Aug 2011 20:54:08 GMT Cache-Control: public, s-maxage=60 Content-Type: text/html <html> ... </html> HTTP
  • 34. Cacher tout en restant dynamique Edge Side Includes http://www.w3.org/TR/esi-lang
  • 35. Edge Side Includes <esi:include src="/banner.html"/>
  • 36. Edge Side Includes Non caché
  • 37. Edge Side Includes 3600 seconds <esi:include … />
  • 38. Edge Side Includes http://paris-web.local/index.php http://paris-web.local/index.php 1 2 Lorem ipsum dolor sit Reverse Proxy Cache amet, consectetur <esi:include > adipiscing elit. Praesent eu Serveur Web odio eget eros vehicula pulvinar id sed turpis. Client Vivamus a velit quam, Lorem ipsum dolor sit Lorem ipsum auctor euismod tortor. amet, consectetur dolor sit amet, adipiscing elit. Praesent eu consectetur odio eget eros vehicula adipiscing elit http://paris-web.local/sidebar.html pulvinar id sed turpis. 3 Vivamus a velit quam, auctor euismod tortor. Lorem ipsum dolor sit amet, consectetur 4 adipiscing elit
  • 39. « Happy Caching! » Questions ? Hugo Hamon (@hhamon) http://hugohamon.com