SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Downloaden Sie, um offline zu lesen
HTTP caching and PHP
David de Boer
Hello DPC!
• I’m David
• Lead developer at Driebit, Amsterdam
• Open source enthusiast
– Phil Karlton
“There are only two hard things in
Computer Science: cache invalidation and
naming things.”
Why we cache
• Reduce response times
• Reduce server load
• Cheap
How we cache
Client App
Client AppCache
How we cache
Efficient caching
• Maximize cache hits
• TTL ∞



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

...
• Maximize cache hits
• TTL ∞
• Invalidation
Efficient caching
AppCache
Let’s go invalidate
Cache
• Varnish
• Nginx
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)";

	
   }

}
$	
  composer	
  require	
  friendsofsymfony/http-­‐cache	
  @alpha
FOSHttpCache
App
Connect and purge
use	
  FOSHttpCacheProxyClientVarnish;



$servers	
  =	
  ['10.0.0.1:6081'];

$proxyClient	
  =	
  new	
  Varnish($servers);	
  
$proxyClient

	
   -­‐>purge('/news/articles/42')

	
   -­‐>purge('/news')

	
   -­‐>flush();
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;	
  
}
Level up
use	
  FOSHttpCacheCacheInvalidator;	
  
!
$invalidator	
  =	
  new	
  CacheInvalidator(

	
  	
  $proxyClient

);	
  
!
$invalidator

	
  	
  -­‐>invalidateRegex('.*',	
  'image/png')

	
  	
  -­‐>invalidateRegex('^/admin')

	
   -­‐>flush();
Get organized
• Complex relationships between URLs
• Knowing when to invalidate what
• Depends on application logic
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	
  {	
  
	
   	
   	
   #	
  ...	
  
	
   	
   }	
  
	
   }	
  
}	
  
!
#	
  ...
Tagging
//	
  overview.php

header('Cache-­‐Control:	
  max-­‐age=3600');

header('X-­‐Cache-­‐Tags:	
  art-­‐1,art-­‐2,art-­‐3');	
  
!
!
//	
  article.php

header('Cache-­‐Control:	
  max-­‐age=3600');

header('X-­‐Cache-­‐Tags:	
  art-­‐2');	
  
$invalidator-­‐>invalidateTags(['art-­‐2']);
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
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)
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)

{	
  
	
   #	
  ...
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	
  }	
  
Thanks!
david@driebit.nl
https://github.com/FriendsOfSymfony/
FOSHttpCache
https://github.com/FriendsOfSymfony/
FOSHttpCacheBundle
http://foshttpcache.readthedocs.org

Weitere ähnliche Inhalte

Was ist angesagt?

Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
diego_k
 
用Tornado开发RESTful API运用
用Tornado开发RESTful API运用用Tornado开发RESTful API运用
用Tornado开发RESTful API运用
Felinx Lee
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappers
Positive Hack Days
 

Was ist angesagt? (20)

Rapid Infrastructure Provisioning
Rapid Infrastructure ProvisioningRapid Infrastructure Provisioning
Rapid Infrastructure Provisioning
 
Autoscaling with hashi_corp_nomad
Autoscaling with hashi_corp_nomadAutoscaling with hashi_corp_nomad
Autoscaling with hashi_corp_nomad
 
Mojo as a_client
Mojo as a_clientMojo as a_client
Mojo as a_client
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
 
Integrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suiteIntegrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suite
 
Assetic (Symfony Live Paris)
Assetic (Symfony Live Paris)Assetic (Symfony Live Paris)
Assetic (Symfony Live Paris)
 
Webrtc mojo
Webrtc mojoWebrtc mojo
Webrtc mojo
 
Php basic for vit university
Php basic for vit universityPhp basic for vit university
Php basic for vit university
 
PuppetCamp SEA 1 - Version Control with Puppet
PuppetCamp SEA 1 - Version Control with PuppetPuppetCamp SEA 1 - Version Control with Puppet
PuppetCamp SEA 1 - Version Control with Puppet
 
Frontend Servers and NGINX: What, Where and How
Frontend Servers and NGINX: What, Where and HowFrontend Servers and NGINX: What, Where and How
Frontend Servers and NGINX: What, Where and How
 
Web Apps in Perl - HTTP 101
Web Apps in Perl - HTTP 101Web Apps in Perl - HTTP 101
Web Apps in Perl - HTTP 101
 
Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3
 
ITT 2015 - Simon Stewart - Building Android Apps at Speed and Scale
ITT 2015 - Simon Stewart - Building Android Apps at Speed and ScaleITT 2015 - Simon Stewart - Building Android Apps at Speed and Scale
ITT 2015 - Simon Stewart - Building Android Apps at Speed and Scale
 
Assetic (OSCON)
Assetic (OSCON)Assetic (OSCON)
Assetic (OSCON)
 
eZ Publish Cluster Unleashed
eZ Publish Cluster UnleashedeZ Publish Cluster Unleashed
eZ Publish Cluster Unleashed
 
Real time server
Real time serverReal time server
Real time server
 
Ansible leveraging 2.0
Ansible leveraging 2.0Ansible leveraging 2.0
Ansible leveraging 2.0
 
用Tornado开发RESTful API运用
用Tornado开发RESTful API运用用Tornado开发RESTful API运用
用Tornado开发RESTful API运用
 
Manage WordPress with Awesome using wp cli
Manage WordPress with Awesome using wp cliManage WordPress with Awesome using wp cli
Manage WordPress with Awesome using wp cli
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappers
 

Andere mochten auch

HTTP's Best-Kept Secret: Caching
HTTP's Best-Kept Secret: CachingHTTP's Best-Kept Secret: Caching
HTTP's Best-Kept Secret: Caching
rtomayko
 

Andere mochten auch (6)

Drupal et-caching-esi
Drupal et-caching-esiDrupal et-caching-esi
Drupal et-caching-esi
 
Caching in HTTP
Caching in HTTPCaching in HTTP
Caching in HTTP
 
HTTP caching with Varnish
HTTP caching with VarnishHTTP caching with Varnish
HTTP caching with Varnish
 
HTTP's Best-Kept Secret: Caching
HTTP's Best-Kept Secret: CachingHTTP's Best-Kept Secret: Caching
HTTP's Best-Kept Secret: Caching
 
Caching Strategies
Caching StrategiesCaching Strategies
Caching Strategies
 
Advanced Caching Concepts @ Velocity NY 2015
Advanced Caching Concepts @ Velocity NY 2015Advanced Caching Concepts @ Velocity NY 2015
Advanced Caching Concepts @ Velocity NY 2015
 

Ähnlich wie HTTP Caching and PHP

Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
yiditushe
 
Bottom to Top Stack Optimization - CICON2011
Bottom to Top Stack Optimization - CICON2011Bottom to Top Stack Optimization - CICON2011
Bottom to Top Stack Optimization - CICON2011
CodeIgniter Conference
 
Zendcon 2007 Api Design
Zendcon 2007 Api DesignZendcon 2007 Api Design
Zendcon 2007 Api Design
unodelostrece
 
Tips
TipsTips
Tips
mclee
 

Ähnlich wie HTTP Caching and PHP (20)

Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
 
Pecl Picks
Pecl PicksPecl Picks
Pecl Picks
 
VUG5: Varnish at Opera Software
VUG5: Varnish at Opera SoftwareVUG5: Varnish at Opera Software
VUG5: Varnish at Opera Software
 
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
 
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
Thijs Feryn - Leverage HTTP to deliver cacheable websites - Codemotion Berlin...
 
How we use and deploy Varnish at Opera
How we use and deploy Varnish at OperaHow we use and deploy Varnish at Opera
How we use and deploy Varnish at Opera
 
Heavy Web Optimization: Backend
Heavy Web Optimization: BackendHeavy Web Optimization: Backend
Heavy Web Optimization: Backend
 
Bottom to Top Stack Optimization with LAMP
Bottom to Top Stack Optimization with LAMPBottom to Top Stack Optimization with LAMP
Bottom to Top Stack Optimization with LAMP
 
Bottom to Top Stack Optimization - CICON2011
Bottom to Top Stack Optimization - CICON2011Bottom to Top Stack Optimization - CICON2011
Bottom to Top Stack Optimization - CICON2011
 
Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!
 
Zendcon 2007 Api Design
Zendcon 2007 Api DesignZendcon 2007 Api Design
Zendcon 2007 Api Design
 
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry PiGrâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
 
How to build a High Performance PSGI/Plack Server
How to build a High Performance PSGI/Plack Server How to build a High Performance PSGI/Plack Server
How to build a High Performance PSGI/Plack Server
 
Tips
TipsTips
Tips
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
Boost your website by running PHP on Nginx
Boost your website by running PHP on NginxBoost your website by running PHP on Nginx
Boost your website by running PHP on Nginx
 
Lessons from running potentially malicious code inside containers
Lessons from running potentially malicious code inside containersLessons from running potentially malicious code inside containers
Lessons from running potentially malicious code inside containers
 
Performance tuning with zend framework
Performance tuning with zend frameworkPerformance tuning with zend framework
Performance tuning with zend framework
 
php & performance
 php & performance php & performance
php & performance
 
PHP & Performance
PHP & PerformancePHP & Performance
PHP & Performance
 

Mehr von David de Boer

Mehr von David de Boer (6)

Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)
 
Being functional in PHP (PHPDay Italy 2016)
Being functional in PHP (PHPDay Italy 2016)Being functional in PHP (PHPDay Italy 2016)
Being functional in PHP (PHPDay Italy 2016)
 
Being functional in PHP
Being functional in PHPBeing functional in PHP
Being functional in PHP
 
Going crazy with Varnish and Symfony
Going crazy with Varnish and SymfonyGoing crazy with Varnish and Symfony
Going crazy with Varnish and Symfony
 
Planidoo & Zotonic
Planidoo & ZotonicPlanidoo & Zotonic
Planidoo & Zotonic
 
HTTP cache: keeping it fresh
HTTP cache: keeping it freshHTTP cache: keeping it fresh
HTTP cache: keeping it fresh
 

Kürzlich hochgeladen

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Kürzlich hochgeladen (20)

WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 

HTTP Caching and PHP

  • 1. HTTP caching and PHP David de Boer
  • 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.”
  • 4. Why we cache • Reduce response times • Reduce server load • Cheap
  • 7. Efficient caching • Maximize cache hits • TTL ∞
 

  • 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
 ...
  • 9. • Maximize cache hits • TTL ∞ • Invalidation Efficient caching
  • 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)";
   }
 }
  • 13. $  composer  require  friendsofsymfony/http-­‐cache  @alpha FOSHttpCache App
  • 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;   }
  • 16. Level up use  FOSHttpCacheCacheInvalidator;   ! $invalidator  =  new  CacheInvalidator(
    $proxyClient
 );   ! $invalidator
    -­‐>invalidateRegex('.*',  'image/png')
    -­‐>invalidateRegex('^/admin')
   -­‐>flush();
  • 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  {         #  ...       }     }   }   ! #  ...
  • 19. Tagging //  overview.php
 header('Cache-­‐Control:  max-­‐age=3600');
 header('X-­‐Cache-­‐Tags:  art-­‐1,art-­‐2,art-­‐3');   ! ! //  article.php
 header('Cache-­‐Control:  max-­‐age=3600');
 header('X-­‐Cache-­‐Tags:  art-­‐2');   $invalidator-­‐>invalidateTags(['art-­‐2']);
  • 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  }