Let's face it: PHP is slow. Let's speed up our Zend Framework 2 web application by putting Varnish Cache in front of it!
Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. Varnish Cache is really, really fast. It typically speeds up delivery with a factor of 300 - 1000x, depending on your architecture.
Edge Side Includes is a simple markup language for edge level dynamic web content assembly. The purpose is to tackle the problem of web infrastructure scaling and is an application of edge computing.
<esi:include src="http://example.com/1.html" alt="http://bak.example.com/2.html" onerror="continue"/>
Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.
"minimum-stability": "dev",
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": "2.*",
"socalnick/scn-esi-widget": "dev-master"
}
return array(
'modules' => array(
'Application',
'ScnEsiWidget',
),
);
backend default {
.host = "127.0.0.1";
.port = "10088";
}
sub vcl_recv {
# Set a header announcing Surrogate Capability to the origin
# ScnEsiWidget sees this header and emits ESI tag for widgets
set req.http.Surrogate-Capability = "varnish=ESI/1.0";
}
sub vcl_fetch {
# Unset the Surrogate Control header and do ESI
if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
unset beresp.http.Surrogate-Control;
set beresp.do_esi = true;
}
}
public function esiAction()
{
$viewModel = new ViewModel();
$this->esiWidget()->addToViewModel($viewModel, '/application/index/recent-tweets', 'recentTweets');
$headers = $this->getResponse()->getHeaders();
$cacheControl = new \Zend\Http\Header\CacheControl();
$cacheControl->addDirective('max-age', '60');
$headers->addHeader($cacheControl);
return $viewModel;
}
<div><?php echo $this->recentTweets ?></div>
public function recentTweetsAction()
{
$headers = $this->getResponse()->getHeaders();
$cacheControl = new \Zend\Http\Header\CacheControl();
$cacheControl->addDirective('max-age', '10');
$headers->addHeader($cacheControl);
$viewModel = new ViewModel();
$viewModel->setTerminal(true);
return $viewModel;
}
<ul>
<li><?php echo date('h:i:s')?> @SocalNick: This is a recent tweet!</li>
<li><?php echo date('h:i:s', time() - 10)?> @Touge: I'm at SFPHP!</li>
</ul>
Nicholas Calugar
Senior Software Engineer @ IGN Entertainment
twitter.com/socalnick
https://github.com/socalnick
Code: https://github.com/SocalNick/ScnEsiWidget
Slides: http://socalnick.github.com/talks/2012/09/27/zf2-fast-lane/