Weitere ähnliche Inhalte
Ähnlich wie HTTP Caching and PHP (20)
Mehr von David de Boer (6)
Kürzlich hochgeladen (20)
HTTP Caching and PHP
- 2. Hello DPC!
• I’m David
• Lead developer at Driebit, Amsterdam
• Open source enthusiast
- 3. – Phil Karlton
“There are only two hard things in
Computer Science: cache invalidation and
naming things.”
- 8. GET
/apple
HTTP/1.1
Host:
fresh-‐fruit.com
Connection:
close
...
!
!
HTTP/1.1
200
OK
Host:
fresh-‐fruit.com
X-‐Cache:
HIT
Age:
567648000
...
- 12. Varnish
#
/etc/varnish/default.vcl
sub
vcl_hit
{
if
(req.request
==
"PURGE")
{
purge;
error
204
"Purged";
}
}
!
sub
vcl_miss
{
if
(req.request
==
"PURGE")
{
purge;
error
204
"Purged
(Not
in
cache)";
}
}
- 14. Connect and purge
use
FOSHttpCacheProxyClientVarnish;
$servers
=
['10.0.0.1:6081'];
$proxyClient
=
new
Varnish($servers);
$proxyClient
-‐>purge('/news/articles/42')
-‐>purge('/news')
-‐>flush();
- 15. Level up
sub
vcl_recv
{
if
(req.request
==
"BAN")
{
ban("obj.http.x-‐host
~
"
+
req.http.x-‐host
+
"
&&
obj.http.x-‐url
~
"
+
req.http.x-‐url
+
"
&&
obj.http.content-‐type
~
"
+
req.http.x-‐content-‐type
);
!
error
200
"Banned";
}
}
!
sub
vcl_fetch
{
#
Set
ban
lurker-‐friendly
custom
headers
set
beresp.http.x-‐url
=
req.url;
set
beresp.http.x-‐host
=
req.http.host;
}
- 17. Get organized
• Complex relationships between URLs
• Knowing when to invalidate what
• Depends on application logic
- 18. Tagging
sub
vcl_recv
{
if
(req.request
==
"BAN")
{
if
(req.http.x-‐cache-‐tags)
{
ban("obj.http.x-‐host
~
"
+
req.http.x-‐host
+
"
&&
obj.http.x-‐url
~
"
+
req.http.x-‐url
+
"
&&
obj.http.content-‐type
~
"
+
req.http.x-‐content-‐type
+
"
&&
obj.http.x-‐cache-‐tags
~
"
+
req.http.x-‐cache-‐tags
);
error
200
"Banned";
}
else
{
#
...
}
}
}
!
#
...
- 20. use
FOSHttpCacheTestsVarnishTestCase;
!
class
MyTest
extends
VarnishTestCase
{
public
function
testHit()
{
$first
=
$this-‐>getResponse('/cached.php');
$this-‐>assertMiss($first);
$second
=
$this-‐>getResponse('/cached.php');
$this-‐>assertHit($second);
}
}
OK
(1
test,
2
assertions)
Test-driven invalidation
- 21. Test-driven invalidation
class
MyTest
extends
VarnishTestCase
{
public
function
testTags()
{
$miss
=
$this-‐>getResponse('/article.php');
$hit
=
$this-‐>getResponse('/article.php');
$invalidator-‐>invalidateTags(['art-‐2']);
$this-‐>assertMiss($this-‐>getResponse('/article.php'));
}
}
OK
(1
test,
1
assertion)
- 22. Symfony bundle
$
composer
require
friendsofsymfony/http-‐cache-‐bundle
@alpha
!
/**
*
@InvalidateRoute("fruits-‐overview",
params={"type"
=
"latest"})
*
@InvalidateRoute("fruits",
params={"num"
=
"id"})
*/
public
function
editAction($id)
{
#
...
/**
*
@Tag(expression="'fruit-‐'~$id")
*/
public
function
showAction($id)
{
#
...
- 23. Symfony bundle
fos_http_cache:
cache_control:
rules:
-‐
match:
host:
^login.example.com$
headers:
cache_control:
public:
false
s_maxage:
0
last_modified:
"-‐1
hour"
vary:
[Accept-‐Encoding,
Accept-‐Language]
-‐
match:
attributes:
{
_controller:
^AcmeBundle:Default:.*
}
additional_cacheable_status:
[400]
headers:
cache_control:
{
public:
true,
max_age:
15,
s_maxage:
30
}