Skip to content

Commit 4b97578

Browse files
committed
Custom TTL header for Symfony HttpCache and Varnish
1 parent 6c755b3 commit 4b97578

23 files changed

+735
-129
lines changed

doc/includes/custom-ttl.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
By default, the caching proxy looks at the ``s-maxage`` instruction in the
2+
``Cache-Control`` header to know for how long it should cache a page. But the
3+
``Cache-Control`` header is also sent to the client. Any caches on the Internet,
4+
for example the Internet provider or from a cooperate network might look at
5+
``s-maxage`` and cache the page. This can be a problem, notably when you do
6+
:doc:`explicit cache invalidation </cache-invalidator>`. In that
7+
scenario, you want your caching proxy to keep a page in cache for a long time,
8+
but caches outside your control must not keep the page for a long duration.
9+
10+
One option could be to set a high ``s-maxage`` for the proxy and simply rewrite
11+
the response to remove or reduce the ``s-maxage``. This is not a good solution
12+
however, as you start to duplicate your caching rule definitions.
13+
14+
The solution to this issue provided here is to use a separate, different header
15+
called ``X-Reverse-Proxy-TTL`` that controls the TTL of the caching proxy to
16+
keep ``s-maxage`` for other proxies. Because this is not a standard feature,
17+
you need to add configuration to your caching proxy.

doc/spelling_word_list.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ github
2020
vcl
2121
fos
2222
Vcl
23+
inline

doc/symfony-cache-configuration.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ the cache. Instead, overwrite the constructor of AppCache and register the
4343
subscribers there. A simple cache will look like this::
4444

4545
use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
46+
use FOS\HttpCache\SymfonyCache\PurgeSubscriber;
47+
use FOS\HttpCache\SymfonyCache\RefreshSubscriber;
4648
use FOS\HttpCache\SymfonyCache\UserContextSubscriber;
49+
use FOS\HttpCache\SymfonyCache\CustomTtlListener();
4750

4851
class AppCache extends EventDispatchingHttpCache
4952
{
@@ -54,9 +57,10 @@ subscribers there. A simple cache will look like this::
5457
{
5558
parent::__construct($kernel, $cacheDir);
5659

57-
$this->addSubscriber(new UserContextSubscriber());
5860
$this->addSubscriber(new PurgeSubscriber());
5961
$this->addSubscriber(new RefreshSubscriber());
62+
$this->addSubscriber(new UserContextSubscriber());
63+
$this->addSubscriber(new CustomTtlListener());
6064
}
6165
}
6266

@@ -179,4 +183,17 @@ the ``session_name_prefix`` option) in the requests to the backend. If you need
179183
a different behavior, overwrite ``UserContextSubscriber::cleanupHashLookupRequest``
180184
with your own logic.
181185

186+
Custom TTL
187+
~~~~~~~~~~
188+
189+
.. include:: includes/custom-ttl.rst
190+
191+
The ``CustomTtlSubscriber`` looks at a specific header to determine the TTL,
192+
preferring that over ``s-maxage``. The default header is ``X-Reverse-Proxy-TTL``
193+
but you can customize that in the subscriber constructor::
194+
195+
new CustomTtlSubscriber('My-TTL-Header');
196+
197+
The custom header is removed before sending the response to the client.
198+
182199
.. _HttpCache: http://symfony.com/doc/current/book/http_cache.html#symfony-reverse-proxy

doc/testing-your-application.rst

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ Make sure ``symfony/process`` is available in your project:
9898
9999
$ composer require symfony/process
100100
101-
Then set your Varnish configuration (VCL) file. All available configuration
102-
parameters are shown below.
101+
Then set your Varnish configuration (VCL) file. Configuration is handled either
102+
by overwriting the getter or by defining a PHP constant. You can set the
103+
constants in your ``phpunit.xml`` or in the bootstrap file. Available
104+
configuration parameters are:
103105

104106
======================= ========================= ================================================== ===========================================
105107
Constant Getter Default Description
@@ -109,10 +111,22 @@ Constant Getter Default
109111
``VARNISH_PORT`` ``getCachingProxyPort()`` ``6181`` port Varnish listens on
110112
``VARNISH_MGMT_PORT`` ``getVarnishMgmtPort()`` ``6182`` Varnish management port
111113
``VARNISH_CACHE_DIR`` ``getCacheDir()`` ``sys_get_temp_dir()`` + ``/foshttpcache-varnish`` directory to use for cache
112-
``VARNISH_VERSION`` ``getVarnishVersion()`` ``4`` installed varnish application version
113114
``WEB_SERVER_HOSTNAME`` ``getHostName()`` hostname your application can be reached at
114115
======================= ========================= ================================================== ===========================================
115116

117+
The Varnish version is controlled by an environment variable (in case you want
118+
to test both Varnish 3 and 4 on a continuous integration system). See the
119+
``.travis.yml`` of the FOSHttpCache git repository for an example.
120+
121+
==================== ========================= ======= ===========================================
122+
Environment Variable Getter Default Description
123+
==================== ========================= ======= ===========================================
124+
``VARNISH_VERSION`` ``getVarnishVersion()`` ``4`` version of varnish application that is used
125+
==================== ========================= ======= ===========================================
126+
127+
See ``tests/bootstrap.php`` for an example how this repository uses the version
128+
information to set the right ``VARNISH_FILE`` constant.
129+
116130
Enable Assertions
117131
'''''''''''''''''
118132

0 commit comments

Comments
 (0)