a SensioLabs Product

The PHP micro-framework
based on the Symfony Components

Warning: Silex 1.3 is not maintained anymore. Please upgrade to Silex 2.x.
You are reading the documentation for Silex 1.3. Switch to the documentation for Silex 2.0.

Questions & Feedback

License

Creative Commons License Silex documentation is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.

HTTP Cache

The HttpCacheServiceProvider provides support for the Symfony 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 constructor.

Services

  • http_cache: An instance of HttpCache.
  • http_cache.esi: An instance of Esi, that implements the ESI capabilities to Request and Response instances.
  • http_cache.store: An instance of Store, that implements all the logic for storing cache metadata (Request and Response headers).

Registering

1
2
3
$app->register(new Silex\Provider\HttpCacheServiceProvider(), array(
    'http_cache.cache_dir' => __DIR__.'/cache/',
));

Usage

Silex already supports any reverse proxy like Varnish out of the box by setting Response HTTP cache headers:

1
2
3
4
5
6
7
use Symfony\Component\HttpFoundation\Response;

$app->get('/', function() {
    return new Response('Foo', 200, array(
        'Cache-Control' => 's-maxage=5',
    ));
});

Tip

If you want Silex to trust the X-Forwarded-For* headers from your reverse proxy at address $ip, you will need to whitelist it as documented in Trusting Proxies.

If you would be running Varnish in front of your application on the same machine:

use Symfony\Component\HttpFoundation\Request;

Request::setTrustedProxies(array('127.0.0.1', '::1'));
$app->run();

This provider allows you to use the Symfony reverse proxy natively with Silex applications by using the http_cache service. The Symfony reverse proxy acts much like any other proxy would, so you will want to whitelist it:

use Symfony\Component\HttpFoundation\Request;

Request::setTrustedProxies(array('127.0.0.1'));
$app['http_cache']->run();

The provider also provides ESI support:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$app->get('/', function() {
    $response = new Response(<<<EOF
<html>
    <body>
        Hello
        <esi:include src="/included" />
    </body>
</html>

EOF
    , 200, array(
        'Surrogate-Control' => 'content="ESI/1.0"',
    ));

    $response->setTtl(20);

    return $response;
});

$app->get('/included', function() {
    $response = new Response('Foo');
    $response->setTtl(5);

    return $response;
});

$app['http_cache']->run();

If your application doesn't use ESI, you can disable it to slightly improve the overall performance:

$app->register(new Silex\Provider\HttpCacheServiceProvider(), array(
   'http_cache.cache_dir' => __DIR__.'/cache/',
   'http_cache.esi'       => null,
));

Tip

To help you debug caching issues, set your application debug to true. Symfony automatically adds a X-Symfony-Cache header to each response with useful information about cache hits and misses.

If you are not using the Symfony Session provider, you might want to set the PHP session.cache_limiter setting to an empty value to avoid the default PHP behavior.

Finally, check that your Web server does not override your caching strategy.

For more information, consult the Symfony HTTP Cache documentation.

Website powered by Symfony and Twig, deployed on
The Silex logo is © 2010-2017 SensioLabs