Commit a9ff89d1 authored by Fabien Potencier's avatar Fabien Potencier

added HttpCacheExtension

parent d4028343
...@@ -65,6 +65,7 @@ All of these are within the ``Silex\Extension`` namespace. ...@@ -65,6 +65,7 @@ All of these are within the ``Silex\Extension`` namespace.
* :doc:`TranslationExtension <extensions/translation>` * :doc:`TranslationExtension <extensions/translation>`
* :doc:`UrlGeneratorExtension <extensions/url_generator>` * :doc:`UrlGeneratorExtension <extensions/url_generator>`
* :doc:`ValidatorExtension <extensions/validator>` * :doc:`ValidatorExtension <extensions/validator>`
* :doc:`HttpCacheExtension <extensions/http_cache>`
Creating an extension Creating an extension
--------------------- ---------------------
......
HttpCacheExtension
==================
The *HttpCacheExtension* provides support for the Symfony2 Reverse Proxy.
Parameters
----------
* **http_cache.cache_dir**: The cache directory to store the HTTP cache data.
* **http_cache.options** (optional): An array of options for the `HttpCache
<http://api.symfony.com/2.0/Symfony/Component/HttpKernel/HttpCache/HttpCache.html>`_
constructor.
Services
--------
* **http_cache**: An instance of `HttpCache
<http://api.symfony.com/2.0/Symfony/Component/HttpKernel/HttpCache/HttpCache.html>`_,
Registering
-----------
::
$app->register(new Silex\Extension\HttpCacheExtension(), array(
'cache_dir' => __DIR__.'/cache/',
));
Usage
-----
Silex already supports any Reverse Proxy like Varnish out of the box by
setting Response HTTP cache headers:
$app->get('/', function() {
return new Response('Foo', 200, array(
'Cache-Control' => 's-maxage=5',
));
});
This extension allows you to use the Symfony2 reverse proxy natively with
Silex applications by using the `http_cache` service to handle the Request::
$app['http_cache']->handle($request)->send();
The extension also provide `ESI
<http://www.doctrine-project.org/docs/dbal/2.0/en/>`_ support::
$app->get('/', function() {
return new Response(<<<EOF
<html>
<body>
Hello
<esi:include src="/included" />
</body>
</html>
EOF
, 200, array(
'Cache-Control' => 's-maxage=20',
'Surrogate-Control' => 'content="ESI/1.0"',
));
});
$app->get('/included', function() {
return new Response('Foo', 200, array(
'Cache-Control' => 's-maxage=5',
));
});
$app['http_cache']->handle($request)->send();
For more information, consult the `Symfony2 HTTP Cache documentation
<http://symfony.com/doc/current/book/http_cache.html>`_.
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Extension;
use Silex\Application;
use Silex\ExtensionInterface;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpKernel\HttpCache\Esi;
use Symfony\Component\HttpKernel\HttpCache\Store;
class HttpCacheExtension implements ExtensionInterface
{
public function register(Application $app)
{
$app['http_cache'] = $app->share(function () use ($app) {
return new HttpCache($app, $app['http_cache.store'], $app['http_cache.esi'], $app['http_cache.options']);
});
$app['http_cache.esi'] = $app->share(function () use ($app) {
return new Esi();
});
$app['http_cache.store'] = $app->share(function () use ($app) {
return new Store($app['http_cache.cache_dir']);
});
if (!isset($app['http_cache.options'])) {
$app['http_cache.options'] = array();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment