caching on the edge
DESCRIPTION
TRANSCRIPT
![Page 1: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/1.jpg)
Speaker | Company
Title // Presentation Name
Caching on the Edge Fabien Potencier
![Page 2: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/2.jpg)
Who are you?
![Page 3: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/3.jpg)
May 1996
RFC1945 – HTTP/1.0
![Page 4: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/4.jpg)
March 1999
RFC2616 – HTTP/1.1
http://www.ietf.org/rfc/rfc2616.txt
![Page 5: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/5.jpg)
HTTP Bis – Work in progress
http://tools.ietf.org/wg/httpbis/
http://www.flickr.com/photos/visualpanic/233508614
![Page 6: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/6.jpg)
p1: Messaging: Low-level message parsing and connection management p2: Semantics: Methods, status codes and headers p3: Payload: Dealing with content, message-specific headers p4: Conditional Requests: e.g., If-Modified-Since p5: Range Requests: Getting partial content p6: Caching: Browser and intermediary caches p7: Authentication: HTTP authentication framework
![Page 7: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/7.jpg)
HTTP
![Page 8: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/8.jpg)
The Client sends a Request to the Server The Server sends back a Response to the Client
A Client (browser, bot, WS, curl, …) A Server (Apache, nginx, …)
The Request and the Response are HTTP messages
![Page 9: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/9.jpg)
Let’s play with HTTP and HTTP headers
![Page 10: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/10.jpg)
GET / HTTP/1.1 Host: http.trainings.sensiolabs.com
![Page 11: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/11.jpg)
« The request-header fields allow the client to pass additional information about the request, and about the client itself, to the
server. These fields act as request modifiers, with semantics equivalent to the parameters on a programming language method
invocation. »
![Page 12: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/12.jpg)
« Each HTTP header field consists of a case-insensitive field name followed by a colon (":"), optional whitespace, and the field value »
![Page 13: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/13.jpg)
HTTP/1.1 200 OK Date: Wed, 15 Oct 2005 07:07:07 GMT Server: Apache Content-Length: 14 Content-Type: text/html
Hello World!
![Page 14: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/14.jpg)
Live HTTP headers Firefox extension
http://livehttpheaders.mozdev.org/
![Page 15: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/15.jpg)
$ curl -i http://http.trainings.sensiolabs.com/
HTTP/1.1 200 OK Date: Wed, 15 Oct 2005 07:07:07 GMT Server: Apache Content-Length: 14 Content-Type: text/html
Hello World!
![Page 16: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/16.jpg)
$ telnet localhost 80 Trying ::1... Connected to localhost. Escape character is '^]'. GET / HTTP/1.1 Host: http.trainings.sensiolabs.com
HTTP/1.1 200 OK Date: Wed, 15 Oct 2005 07:07:07 GMT Server: Apache Content-Length: 14 Content-Type: text/html
Hello World!
Connection closed by foreign host.
![Page 17: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/17.jpg)
HTTP Headers with PHP
![Page 18: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/18.jpg)
header('Content-Type: text/plain');
header('content-type: text/plain');
![Page 19: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/19.jpg)
Caching in the HTTP Specification
![Page 20: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/20.jpg)
p1: Messaging: Low-level message parsing and connection management p2: Semantics: Methods, status codes and headers p3: Payload: Dealing with content, message-specific headers p4: Conditional Requests: e.g., If-Modified-Since p5: Range Requests: Getting partial content p6: Caching: Browser and intermediary caches p7: Authentication: HTTP authentication framework
![Page 21: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/21.jpg)
HTTP Expiration HTTP Validation
Fresh vs Stale
![Page 22: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/22.jpg)
HTTP Headers for Expiration
Cache-Control
Expires
![Page 23: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/23.jpg)
HTTP Headers for Validation
Last-Modified / If-Modified-Since
ETag / If-None-Match
![Page 24: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/24.jpg)
HTTP Cache headers only work with “safe” HTTP methods
(like GET & HEAD)
![Page 25: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/25.jpg)
Never change the state of the server when serving a GET request
(we are talking about the application’s state of course, you can log, cache, …)
![Page 26: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/26.jpg)
HTTP Expiration
![Page 27: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/27.jpg)
Expires
« The "Expires" header field gives the date/time after which
the response is considered stale. »
![Page 28: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/28.jpg)
Brow
ser
Your PHP application
Some
Cach
e
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
HTTP/1.1 200 OK Expires: Thu, 01 Dec …
Hello
GET /foo HTTP/1.1 200 OK Expires: Thu, 01 Dec …
Hello
HTTP/1.1 200 OK Expires: Thu, 01 Dec …
Hello
![Page 29: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/29.jpg)
Brow
ser
Your PHP application
Some
Cach
e
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 200 OK Expires: Thu, 01 Dec …
Hello
HTTP/1.1 200 OK Expires: Thu, 01 Dec …
Hello
Before expira4on
Your applica4on is not called
![Page 30: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/30.jpg)
Brow
ser
Your PHP application
Some
Cach
e
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
HTTP/1.1 200 OK Expires: Thu, 01 Dec …
Hello
GET /foo HTTP/1.1 200 OK Expires: Thu, 01 Dec …
Hello
HTTP/1.1 200 OK Expires: Thu, 01 Dec …
Hello
A<er expira4on
Not fresh
![Page 31: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/31.jpg)
Expires: Thu, 01 Dec 2010 16:00:00 GMT
Date in RFC1123 format, not RFC2822 (timezone always GMT, which is ~ UTC)
![Page 32: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/32.jpg)
$expires = gmdate('D, j M Y H:i:s T', time() + 5);
header('Expires: '.$expires);
![Page 33: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/33.jpg)
$date = new DateTime(null, new DateTimeZone('UTC')); $date->modify('+5 seconds');
$expires = $date->format('D, d M Y H:i:s').' GMT';
$date = gmdate('D, j M Y H:i:s T', time() + 5);
header('Expires: '.$expires);
![Page 34: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/34.jpg)
$date->setTimezone(new DateTimeZone('UTC'));
![Page 35: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/35.jpg)
/expires_with_expires_in_5s.php
![Page 36: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/36.jpg)
WARNING
The clocks on the Web server AND the cache (aka browser)
MUST be synchronised
![Page 37: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/37.jpg)
WARNING
« HTTP/1.1 servers SHOULD NOT send Expires dates
more than one year in the future. »
![Page 38: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/38.jpg)
Mostly useful to make static assets (images, css, js, …) cacheable with an extremely long expiry time
But there is a better way!
![Page 39: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/39.jpg)
Use Cache-Control instead
![Page 40: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/40.jpg)
header('Cache-Control: max-age=5');
![Page 41: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/41.jpg)
http.trainings.sensiolabs.com/expires_in_5s.php
![Page 42: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/42.jpg)
Don’t use Expires Use Cache-Control
…except if you want to set a date, which should be pretty rare
![Page 43: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/43.jpg)
HTTP Validation
![Page 44: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/44.jpg)
“304 Not Modified” is your friend
![Page 45: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/45.jpg)
HTTP Headers for Validation
Etag / If-None-Match
![Page 46: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/46.jpg)
Brow
ser
Your PHP application
Some
Cach
e
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 200 OK ETag: abcdef …
Hello
HTTP/1.1 200 OK ETag: abcdef
Hello
HTTP/1.1 200 OK ETag: abcdef
Hello
![Page 47: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/47.jpg)
Brow
ser
Your PHP application
Some
Cach
e
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org If-None-Match: abcdef
HTTP/1.1 304 Not Modified
GET /foo HTTP/1.1 200 OK ETag: abcdef …
Hello
HTTP/1.1 200 OK ETag: abcdef
Hello
If the resource has not changed
![Page 48: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/48.jpg)
Some
Cach
e
GET /foo HTTP/1.1 200 OK ETag: abcdef …
Hello
Brow
ser
Your PHP application
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org If-None-Match: abcdef
If the resource has changed
GET /foo HTTP/1.1 200 OK ETag: 123456 …
Hello
HTTP/1.1 200 OK ETag: 123456
Hello
HTTP/1.1 200 OK ETag: 123456
Hello
![Page 49: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/49.jpg)
// compute ETag value $etag = '...';
if ( isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag ) { header('HTTP/1.1 304 Not Modified'); } else { header('ETag: '.$etag); echo 'Hello'; }
![Page 50: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/50.jpg)
This is a simple but naïve implementation…
![Page 51: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/51.jpg)
HTTP Headers for Validation
Last-Modified / If-Modified-Since
![Page 52: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/52.jpg)
Brow
ser
Your PHP application
Some
Cach
e
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 200 OK Last-Modified: Thu, …
Hello
HTTP/1.1 200 OK Last-Modified: Thu, …
Hello
HTTP/1.1 200 OK Last-Modified: Thu, …
Hello
![Page 53: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/53.jpg)
Brow
ser
Your PHP application
Some
Cach
e
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org If-Modified-Since: Thu,
HTTP/1.1 304 Not Modified
GET /foo HTTP/1.1 200 OK Last-Modified: Thu, …
Hello
HTTP/1.1 200 OK Last-Modified: Thu, …
Hello
If the resource has not changed
![Page 54: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/54.jpg)
Some
Cach
e
GET /foo HTTP/1.1 200 OK Last-Modified: Thu,…
Hello
GET /foo HTTP/1.1 200 OK Last-Modified: Sun,…
Hello
Brow
ser
Your PHP application
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org If-Modified-Since: Thu,
If the resource has changed
HTTP/1.1 200 OK Last-Modified: Sun, …
Hello
HTTP/1.1 200 OK Last-Modified: Sun, …
Hello
![Page 55: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/55.jpg)
Expiration & Validation
![Page 56: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/56.jpg)
Brow
ser
Your PHP application
Some
Cach
e
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 200 OK Etag: abcdef Cache-‐Control: max-‐age=10 …
Hello
HTTP/1.1 200 OK ETag: abcdef Cache-‐Control: max-‐age=10
Hello
HTTP/1.1 200 OK ETag: abcdef Cache-‐Control: max-‐age=10
Hello
![Page 57: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/57.jpg)
Brow
ser
Your PHP application
Some
Cach
e
GET /foo HTTP/1.1 Host: foo.org
Before expira4on
Your applica4on is not called
GET /foo HTTP/1.1 200 OK Etag: abcdef Cache-‐Control: max-‐age=10 …
Hello
HTTP/1.1 200 OK ETag: abcdef Cache-‐Control: max-‐age=10
Hello
![Page 58: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/58.jpg)
Brow
ser
Your PHP application
Some
Cach
e
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org If-None-Match: abcdef
HTTP/1.1 304 Not Modified Cache-‐Control: max-‐age=10
A<er expira4on but resource s4ll valid
GET /foo HTTP/1.1 200 OK Etag: abcdef Cache-‐Control: max-‐age=10 …
Hello
HTTP/1.1 200 OK ETag: abcdef Cache-‐Control: max-‐age=10
Hello
![Page 59: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/59.jpg)
You can combine HTTP headers the way you want
Expiration wins over Validation
![Page 60: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/60.jpg)
Expiration allows you to scale as less requests hit your server (and client speed is better too)
Validation saves bandwidth
![Page 61: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/61.jpg)
The goal is to never generate the same response twice
![Page 62: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/62.jpg)
PHP and Cache HTTP headers
![Page 63: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/63.jpg)
session_start(); $_SESSION['foo'] = 'bar';
echo 'Hello';
![Page 64: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/64.jpg)
http.trainings.sensiolabs.com/index.php
http.trainings.sensiolabs.com/cookie.php
![Page 65: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/65.jpg)
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
![Page 66: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/66.jpg)
Kinds of caches
![Page 67: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/67.jpg)
Browser Cache
![Page 68: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/68.jpg)
On the server side
Brow
ser
Your PHP application
Brow
ser C
ache
![Page 69: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/69.jpg)
With
in a Com
pany
On the server side
Brow
ser
Your PHP application Br
owse
r Br
owse
r
Brow
ser C
ache
Br
owse
r Cac
he
Brow
ser C
ache
![Page 70: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/70.jpg)
Proxy Cache
![Page 71: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/71.jpg)
On the server side
With
in a Com
pany
Brow
ser
Your PHP application Br
owse
r Br
owse
r
Brow
ser C
ache
Br
owse
r Cac
he
Brow
ser C
ache
Prox
y Cac
he
![Page 72: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/72.jpg)
On the server side
With
in a Com
pany
Brow
ser
Your PHP application Br
owse
r Br
owse
r
Brow
ser C
ache
Br
owse
r Cac
he
Brow
ser C
ache
Prox
y Cac
he
With
in a Com
pany
Brow
ser
Brow
ser
Brow
ser
Brow
ser C
ache
Br
owse
r Cac
he
Brow
ser C
ache
Prox
y Cac
he
![Page 73: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/73.jpg)
Gateway Cache
![Page 74: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/74.jpg)
On the server side
Your PHP application Br
owse
r
Brow
ser C
ache
Gatew
ay Ca
che
![Page 75: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/75.jpg)
With
in a Com
pany
On the server side
Brow
ser
Your PHP application Br
owse
r Br
owse
r
Brow
ser C
ache
Br
owse
r Cac
he
Brow
ser C
ache
Gatew
ay Ca
che
![Page 76: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/76.jpg)
On the server side W
ithin a Com
pany
Brow
ser
Your PHP application Br
owse
r Br
owse
r
Brow
ser C
ache
Br
owse
r Cac
he
Brow
ser C
ache
Prox
y Cac
he
Brow
ser
Brow
ser
Brow
ser C
ache
Br
owse
r Cac
he
Gatew
ay Ca
che
![Page 77: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/77.jpg)
Your PHP application
Gateway Cache Reverse Proxy Cache
Surrogate Cache HTTP Accelerator
Browser
Browser Cache
Proxy Cache
![Page 78: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/78.jpg)
Browser Cache
Local cache for when you hit “back” or
when images are reused throughout a website
![Page 79: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/79.jpg)
Proxy Cache
A shared cache Many people behind a single proxy
Installed by large corporations and ISPs Reduce latency and network traffic
![Page 80: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/80.jpg)
Gateway Cache
A shared cache on the server side Installed by network administrators
Make websites more scalable, reliable and performing better
CDNs like Akaïma are gateway caches
![Page 81: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/81.jpg)
Today, we will mainly talk about Gateway caches
![Page 82: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/82.jpg)
Edge Caching
![Page 83: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/83.jpg)
App Browser
Browser
Gateway Cache
Gateway Cache
![Page 84: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/84.jpg)
HTTP 1.1 allows caching anything by default unless explicit Cache-Control header
![Page 85: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/85.jpg)
In practice, most caches avoid anything with
Cache-Control Cookie / Set-Cookie
WWW-Authenticate / Authorization POST / PUT
302 / 307 status codes
![Page 86: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/86.jpg)
Cache-Control: private
This is the default with PHP when you have a session Browser cache will still work fine here
(public means shared caches, private means browser cache)
![Page 87: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/87.jpg)
A gateway cache won't cache anything "private" or carrying a cookie
In a real-world: tracking cookies (Google Analytics)
![Page 88: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/88.jpg)
All proxies do caching based on the same HTTP headers
![Page 89: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/89.jpg)
We need several clients to understand how it works and how the cached is shared
![Page 90: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/90.jpg)
HTTP Expiration
![Page 91: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/91.jpg)
Gatew
ay Ca
che
Bob
Your PHP application
Bob’s
Cach
e
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 200 OK C-C: max-age=600 …
Hello
HTTP/1.1 200 OK C-C: max-age=600
Hello
HTTP/1.1 200 OK C-C: max-age=600
Hello
GET /foo HTTP/1.1 200 OK C-C: max-age=600 …
Hello
HTTP/1.1 200 OK C-C: max-age=600
Hello
![Page 92: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/92.jpg)
Bob’s
Cach
e
Gatew
ay Ca
che
Bob
Your PHP application
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 200 OK C-C: max-age=600 …
Hello
HTTP/1.1 200 OK C-C: max-age=600
Hello
GET /foo HTTP/1.1 200 OK C-C: max-age=600 …
Hello
![Page 93: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/93.jpg)
Brow
ser C
ache
Alice
Your PHP application
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
HTTP/1.1 200 OK C-C: max-age=600
Hello
HTTP/1.1 200 OK C-C: max-age=600
Hello
Gatew
ay Ca
che
GET /foo HTTP/1.1 200 OK C-C: max-age=600 …
Hello
GET /foo HTTP/1.1 200 OK C-C: max-age=600 …
Hello
![Page 94: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/94.jpg)
HTTP Validation
![Page 95: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/95.jpg)
Bob’s
Cach
e
Gatew
ay Ca
che
Bob
Your PHP application
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 200 OK Etag: abcde …
Hello
HTTP/1.1 200 OK Etag: abcde
Hello
HTTP/1.1 200 OK Etag: abcde
Hello
GET /foo HTTP/1.1 200 OK Etag: abcde …
Hello
HTTP/1.1 200 OK Etag: abcde
Hello
![Page 96: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/96.jpg)
Bob’s
Cach
e
Gatew
ay Ca
che
Bob
Your PHP application
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org If-None-Match: ab
GET /foo HTTP/1.1 Host: foo.org If-None-Match: ab
GET /foo HTTP/1.1 200 OK Etag: ab …
Hello
HTTP/1.1 200 OK Etag: ab
Hello
GET /foo HTTP/1.1 200 OK Etag: ab …
Hello
304 Not Modified 304 Not Modified
![Page 97: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/97.jpg)
Gatew
ay Ca
che
Your PHP application
Brow
ser C
ache
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org If-None-Match: ab
GET /foo HTTP/1.1 200 OK Etag: ab …
Hello
HTTP/1.1 200 OK Etag: ab
Hello
304 Not Modified
Alice
HTTP/1.1 200 OK Etag: ab
Hello
GET /foo HTTP/1.1 200 OK Etag: ab …
Hello
![Page 98: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/98.jpg)
Gateway caches
Varnish (only does that and tries to do it well) Squid (just one way to use it)
mod_cache (Apache)
![Page 99: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/99.jpg)
Gateway caches are a great way to make your website performs better
![Page 100: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/100.jpg)
But what if you cannot cache whole pages? What if a page has "more" dynamic parts?
![Page 101: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/101.jpg)
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vel nulla arcu, vitae cursus nunc. Integer semper turpis et enim porRtor iaculis. Nulla facilisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vehicula ves4bulum dictum. Aenean non velit tortor. Nullam adipiscing malesuada aliquam. Mauris dignissim, urna quis iaculis tempus, justo libero porRtor est, nec eleifend est elit vitae ante. Curabitur interdum luctus metus.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vel nulla arcu, vitae cursus nunc. Integer semper turpis et enim porRtor iaculis. Nulla facilisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vehicula ves4bulum dictum. Aenean non velit tortor. Nullam adipiscing malesuada aliquam. Mauris dignissim, urna quis iaculis tempus, justo libero porRtor est, nec eleifend est elit vitae ante. Curabitur interdum luctus metus, in pulvinar lectus rutrum sit amet. Duis gravida, metus in dictum eleifend, dolor risus 4ncidunt ligula, non volutpat nulla sapien in elit. Nulla rutrum erat id neque suscipit eu ultricies odio sollicitudin. Aliquam a mi vel eros placerat hendrerit. Phasellus porRtor, augue sit amet vulputate venena4s, dui leo commodo odio, a euismod turpis ligula in elit.
cacheable for 10 seconds cacheable for 5 seconds
![Page 102: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/102.jpg)
ESI… or Edge Side Includes
http://www.w3.org/TR/esi-lang
![Page 103: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/103.jpg)
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vel nulla arcu, vitae cursus nunc. Integer semper turpis et enim porRtor iaculis. Nulla facilisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vehicula ves4bulum dictum. Aenean non velit tortor. Nullam adipiscing malesuada aliquam. Mauris dignissim, urna quis iaculis tempus, justo libero porRtor est, nec eleifend est elit vitae ante. Curabitur interdum luctus metus.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vel nulla arcu, vitae cursus nunc. Integer semper turpis et enim porRtor iaculis. Nulla facilisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vehicula ves4bulum dictum. Aenean non velit tortor. Nullam adipiscing malesuada aliquam. Mauris dignissim, urna quis iaculis tempus, justo libero porRtor est, nec eleifend est elit vitae ante. Curabitur interdum luctus metus, in pulvinar lectus rutrum sit amet. Duis gravida, metus in dictum eleifend, dolor risus 4ncidunt ligula, non volutpat nulla sapien in elit. Nulla rutrum erat id neque suscipit eu ultricies odio sollicitudin. Aliquam a mi vel eros placerat hendrerit. Phasellus porRtor, augue sit amet vulputate venena4s, dui leo commodo odio, a euismod turpis ligula in elit.
![Page 104: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/104.jpg)
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vel nulla arcu, vitae cursus nunc. Integer semper turpis et enim porRtor iaculis. Nulla facilisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vehicula ves4bulum dictum. Aenean non velit tortor. Nullam adipiscing malesuada aliquam. Mauris dignissim, urna quis iaculis tempus, justo libero porRtor est, nec eleifend est elit vitae ante. Curabitur interdum luctus metus, in pulvinar lectus rutrum sit amet. Duis gravida, metus in dictum eleifend, dolor risus 4ncidunt ligula, non volutpat nulla sapien in elit. Nulla rutrum erat id neque suscipit eu ultricies odio sollicitudin. Aliquam a mi vel eros placerat hendrerit. Phasellus porRtor, augue sit amet vulputate venena4s, dui leo commodo odio, a euismod turpis ligula in elit.
<esi:include src="..." />
![Page 105: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/105.jpg)
<esi:include src="http://..." />
![Page 106: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/106.jpg)
Brow
ser C
ache
Gatew
ay Ca
che
Brow
ser
Your PHP application
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
GET /bar HTTP/1.1 Host: foo.org
Lorem ipsum dolor
<esi:include src="hYp.." />
HTTP/1.1 200 OK
Lorem ipsum dolor
HTTP/1.1 200 OK
Lorem ipsum dolor sit amet,
Lorem ipsum dolor
HTTP/1.1 200 OK
Lorem ipsum dolor sit amet,
Lorem ipsum dolor
HTTP/1.1 200 OK
![Page 107: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/107.jpg)
Brow
ser C
ache
Gatew
ay Ca
che
Brow
ser
Your PHP application GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
GET /bar HTTP/1.1 Host: foo.org
Lorem ipsum dolor
<esi:include src="hYp.." />
HTTP/1.1 200 OK C-C: max-age=10
Lorem ipsum dolor
HTTP/1.1 200 OK C-C: max-age=5
Lorem ipsum dolor sit amet,
Lorem ipsum dolor
HTTP/1.1 200 OK
Lorem ipsum dolor sit amet,
Lorem ipsum dolor
HTTP/1.1 200 OK
GET /foo C-C: max-age=10
Lor <esi:include />
GET /bar C-C: max-age=5
Lorem
GET /foo C-C: max-age=10
Lor Lorem
![Page 108: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/108.jpg)
Brow
ser C
ache
Gatew
ay Ca
che
Brow
ser
Your PHP application GET /foo HTTP/1.1 Host: foo.org
Lorem ipsum dolor sit amet,
Lorem ipsum dolor
HTTP/1.1 200 OK
GET /foo C-C: max-age=10
Lor <esi:include />
GET /bar C-C: max-age=5
Lorem
GET /foo C-C: max-age=10
Lor Lorem 2 seconds later…
![Page 109: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/109.jpg)
Brow
ser C
ache
Gatew
ay Ca
che
Brow
ser
Your PHP application GET /foo HTTP/1.1 Host: foo.org
Lorem ipsum dolor sit amet,
Lorem ipsum dolor
HTTP/1.1 200 OK
GET /foo C-C: max-age=10
Lor <esi:include />
GET /bar C-C: max-age=5
Lorem
GET /foo C-C: max-age=10
Lor Lorem 7 seconds later…
WRONG
![Page 110: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/110.jpg)
Cache-Control: max-age=10
Cache-Control: s-maxage=10
![Page 111: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/111.jpg)
Brow
ser C
ache
Gatew
ay Ca
che
Brow
ser
Your PHP application GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
GET /bar HTTP/1.1 Host: foo.org
Lorem ipsum dolor
<esi:include src="hYp.." />
HTTP/1.1 200 OK C-C: s-maxage=10
Lorem ipsum dolor
HTTP/1.1 200 OK C-C: s-maxage=5
Lorem ipsum dolor sit amet,
Lorem ipsum dolor
HTTP/1.1 200 OK
Lorem ipsum dolor sit amet,
Lorem ipsum dolor
HTTP/1.1 200 OK
GET /foo C-C: s-maxage=10
Lor <esi:include />
GET /bar C-C: s-maxage=5
Lorem
![Page 112: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/112.jpg)
Brow
ser C
ache
Gatew
ay Ca
che
Brow
ser
Your PHP application GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
Lorem ipsum dolor sit amet,
Lorem ipsum dolor
HTTP/1.1 200 OK
Lorem ipsum dolor sit amet,
Lorem ipsum dolor
HTTP/1.1 200 OK
GET /foo C-C: s-maxage=10
Lor <esi:include />
GET /bar C-C: s-maxage=5
Lorem
2 seconds later…
![Page 113: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/113.jpg)
Brow
ser C
ache
Gatew
ay Ca
che
Brow
ser
Your PHP application GET /foo HTTP/1.1 Host: foo.org
GET /foo HTTP/1.1 Host: foo.org
GET /bar HTTP/1.1 Host: foo.org
Lorem ipsum dolor
HTTP/1.1 200 OK C-C: s-maxage=5
Lorem ipsum dolor sit amet,
Lorem ipsum dolor
HTTP/1.1 200 OK
Lorem ipsum dolor sit amet,
Lorem ipsum dolor
HTTP/1.1 200 OK
GET /foo C-C: s-maxage=10
Lor <esi:include />
GET /bar C-C: s-maxage=5
Lorem
7 seconds later…
![Page 114: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/114.jpg)
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vel nulla arcu, vitae cursus nunc. Integer semper turpis et enim porRtor iaculis. Nulla facilisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vehicula ves4bulum dictum. Aenean non velit tortor. Nullam adipiscing malesuada aliquam. Mauris dignissim, urna quis iaculis tempus, justo libero porRtor est, nec eleifend est elit vitae ante. Curabitur interdum luctus metus.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vel nulla arcu, vitae cursus nunc. Integer semper turpis et enim porRtor iaculis. Nulla facilisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vehicula ves4bulum dictum. Aenean non velit tortor. Nullam adipiscing malesuada aliquam. Mauris dignissim, urna quis iaculis tempus, justo libero porRtor est, nec eleifend est elit vitae ante. Curabitur interdum luctus metus, in pulvinar lectus rutrum sit amet. Duis gravida, metus in dictum eleifend, dolor risus 4ncidunt ligula, non volutpat nulla sapien in elit. Nulla rutrum erat id neque suscipit eu ultricies odio sollicitudin. Aliquam a mi vel eros placerat hendrerit. Phasellus porRtor, augue sit amet vulputate venena4s, dui leo commodo odio, a euismod turpis ligula in elit.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vel nulla arcu, vitae cursus nunc. Integer semper turpis et enim porRtor iaculis. Nulla facilisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vehicula ves4bulum dictum. Aenean non velit tortor. Nullam adipiscing malesuada aliquam.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vel nulla arcu, vitae cursus nunc.
main template
layout
base layout
included page included page
included page
![Page 115: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/115.jpg)
sub vcl_fetch { esi; }
![Page 116: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/116.jpg)
varnishlog -i TxURL
varnishlog –o VCL_call hit | grep RxURL
![Page 117: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/117.jpg)
Make your application work when ESI is enabled and when it’s not
And only parse for ESIs when it’s needed
![Page 118: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/118.jpg)
Surrogate-Capability
Surrogate-Control
http://www.w3.org/TR/edge-arch
![Page 119: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/119.jpg)
Surrogate-Capability: abc="Surrogate/1.0 ESI/1.0"
![Page 120: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/120.jpg)
Surrogate-Control: content="ESI/1.0"
![Page 121: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/121.jpg)
RewriteEngine On RewriteRule ^(.*)$ index.php [QSA,L]
![Page 122: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/122.jpg)
sub vcl_recv { set req.http.Surrogate-Capability = "abc=ESI/1.0"; }
sub vcl_fetch { if (beresp.http.Surrogate-Control ~ "ESI/1.0") { unset beresp.http.Surrogate-Control; esi; } }
![Page 123: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/123.jpg)
function has_surrogate_ESI_capability() { return isset($_SERVER['HTTP_SURROGATE_CAPABILITY']) && preg_match('#ESI/1.0#', $_SERVER['HTTP_SURROGATE_CAPABILITY']); }
![Page 124: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/124.jpg)
function add_surrogate_control() { header('Surrogate-Control: content="ESI/1.0"'); }
![Page 125: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/125.jpg)
function include($path) { if (has_surrogate_ESI_capability()) { $path = get_absolute_path($path);
echo '<esi:include src="'.$path.'" />';
add_surrogate_control(); } else { include dirname(__FILE__).'/'.$path; } }
![Page 126: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/126.jpg)
Imagine the power when you combine ESI, expiration, validation, max-age, s-maxage, …
![Page 127: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/127.jpg)
Why would you want to reinvent the wheel? and implement your own caching system?
![Page 128: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/128.jpg)
Because you cannot afford to use Varnish? Because you use a shared hosting company?
![Page 129: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/129.jpg)
<?php
// With the Symfony2 HTTP accelerator instead of Varnish // won't work if you use exit() for instance... // won't work if you have "global" state // This is just a skeleton to get you started
// Of course, this is native with Symfony2 ;)
![Page 130: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/130.jpg)
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\Cache\Cache; use Symfony\Component\HttpKernel\Cache\Esi; use Symfony\Component\HttpKernel\Cache\Store;
class AppKernel implements HttpKernelInterface { public function handle(Request $request = null, $type = HttpKernelInterface::MASTER_REQUEST, $raw = false) { return new Response($content, 200, $headers); }
public function getRequest() {} }
![Page 131: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/131.jpg)
$_SERVER['HTTP_SURROGATE_CAPABILITY'] = 'symfony2="ESI/1.0"';
// quick hack... not secure at all! $base = str_replace('index.php', '', $request->getScriptName()); $script = str_replace($base, '', $request->getRequestUri());
ob_start();
include __DIR__.'/'.$script;
$content = ob_get_clean(); $headers = array(); foreach (headers_list() as $header) { $elements = explode(':', $header, 2); $headers[$elements[0]] = trim($elements[1]); }
// do not deal with response others than 200 // implementation depends on your code return new Response($content, 200, $headers);
![Page 132: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/132.jpg)
$kernel = new AppKernel(); $store = new Store('/path/to/http_cache'); $esi = new Esi();
$cache = new Cache($kernel, $store, $esi); $cache->handle()->send();
error_log($cache->getLog());
![Page 133: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/133.jpg)
Goal Be as dynamic as needed
Hit the application as less as possible
![Page 134: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/134.jpg)
The power is even bigger than what you think…
![Page 135: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/135.jpg)
<esi:include src="http://...” alt="http://...” onerror="continue" />
![Page 136: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/136.jpg)
stale-while-revalidate - rfc5861
![Page 137: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/137.jpg)
stale-if-error - rfc5861
![Page 138: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/138.jpg)
There are only two hard things in Computer Science: cache invalidation and naming things. -- Phil Karlton
http://martinfowler.com/bliki/TwoHardThings.html
![Page 139: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/139.jpg)
PURGE: varnishadm -T localhost:6082 purgeurl "^/images/ »
![Page 140: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/140.jpg)
acl purge_acl { "localhost"; "1.2.3.4"; }
sub vcl_recv { if (req.request == "PURGE") { if (!client.ip ~ purge_acl) { error 405 "Not allowed."; } purge_url(req.url); error 200 "Purged."; }
return(lookup); }
curl -X PURGE http://...
![Page 141: Caching on the Edge](https://reader033.vdocument.in/reader033/viewer/2022042521/54b7a0c74a79591c048b4597/html5/thumbnails/141.jpg)
WARNING
Don't use GZIP with ESI as Varnish won't be able to assemble the page