Skip to content

Symfony HttpCache listener for custom ttl header #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions doc/includes/custom-ttl.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
By default, the caching proxy looks at the ``s-maxage`` instruction in the
``Cache-Control`` header to know for how long it should cache a page. But the
``Cache-Control`` header is also sent to the client. Any caches on the Internet,
for example the Internet provider or from a cooperate network might look at
``s-maxage`` and cache the page. This can be a problem, notably when you do
:doc:`explicit cache invalidation </cache-invalidator>`. In that
scenario, you want your caching proxy to keep a page in cache for a long time,
but caches outside your control must not keep the page for a long duration.

One option could be to set a high ``s-maxage`` for the proxy and simply rewrite
the response to remove or reduce the ``s-maxage``. This is not a good solution
however, as you start to duplicate your caching rule definitions.

The solution to this issue provided here is to use a separate, different header
called ``X-Reverse-Proxy-TTL`` that controls the TTL of the caching proxy to
keep ``s-maxage`` for other proxies. Because this is not a standard feature,
you need to add configuration to your caching proxy.
1 change: 1 addition & 0 deletions doc/spelling_word_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ github
vcl
fos
Vcl
inline
19 changes: 18 additions & 1 deletion doc/symfony-cache-configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ the cache. Instead, overwrite the constructor of AppCache and register the
subscribers there. A simple cache will look like this::

use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
use FOS\HttpCache\SymfonyCache\PurgeSubscriber;
use FOS\HttpCache\SymfonyCache\RefreshSubscriber;
use FOS\HttpCache\SymfonyCache\UserContextSubscriber;
use FOS\HttpCache\SymfonyCache\CustomTtlListener();

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

$this->addSubscriber(new UserContextSubscriber());
$this->addSubscriber(new PurgeSubscriber());
$this->addSubscriber(new RefreshSubscriber());
$this->addSubscriber(new UserContextSubscriber());
$this->addSubscriber(new CustomTtlListener());
}
}

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

Custom TTL
~~~~~~~~~~

.. include:: includes/custom-ttl.rst

The ``CustomTtlSubscriber`` looks at a specific header to determine the TTL,
preferring that over ``s-maxage``. The default header is ``X-Reverse-Proxy-TTL``
but you can customize that in the subscriber constructor::

new CustomTtlSubscriber('My-TTL-Header');

The custom header is removed before sending the response to the client.

.. _HttpCache: http://symfony.com/doc/current/book/http_cache.html#symfony-reverse-proxy
20 changes: 17 additions & 3 deletions doc/testing-your-application.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ Make sure ``symfony/process`` is available in your project:

$ composer require symfony/process

Then set your Varnish configuration (VCL) file. All available configuration
parameters are shown below.
Then set your Varnish configuration (VCL) file. Configuration is handled either
by overwriting the getter or by defining a PHP constant. You can set the
constants in your ``phpunit.xml`` or in the bootstrap file. Available
configuration parameters are:

======================= ========================= ================================================== ===========================================
Constant Getter Default Description
Expand All @@ -109,10 +111,22 @@ Constant Getter Default
``VARNISH_PORT`` ``getCachingProxyPort()`` ``6181`` port Varnish listens on
``VARNISH_MGMT_PORT`` ``getVarnishMgmtPort()`` ``6182`` Varnish management port
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was rather puzzling me with my varnish 3 locally. i think it does make sense to use an environment over a constant here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we then remove the constant and getter for VARNISH_VERSION? I prefer to keep them, as it’s consistent this way with the other config params. I agree that you’ll typically set the Varnish version through an env var. By the way, testing against multiple Varnish versions is most useful for library/framework code; and less relevant for users testing an app against their caching setup.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am not removing anything except wrong documentation. i added documentation for the environment variable and keep the getter that is using the env variable. we use the env variable both in VarnishTestCase and tests/bootstrap.php

we could alternatively look at both or have tests/bootstrap.php copy the env variable into a constant. but that all feels a bit clumsy and just as confusing.

``VARNISH_CACHE_DIR`` ``getCacheDir()`` ``sys_get_temp_dir()`` + ``/foshttpcache-varnish`` directory to use for cache
``VARNISH_VERSION`` ``getVarnishVersion()`` ``4`` installed varnish application version
``WEB_SERVER_HOSTNAME`` ``getHostName()`` hostname your application can be reached at
======================= ========================= ================================================== ===========================================

The Varnish version is controlled by an environment variable (in case you want
to test both Varnish 3 and 4 on a continuous integration system). See the
``.travis.yml`` of the FOSHttpCache git repository for an example.

==================== ========================= ======= ===========================================
Environment Variable Getter Default Description
==================== ========================= ======= ===========================================
``VARNISH_VERSION`` ``getVarnishVersion()`` ``4`` version of varnish application that is used
==================== ========================= ======= ===========================================

See ``tests/bootstrap.php`` for an example how this repository uses the version
information to set the right ``VARNISH_FILE`` constant.

Enable Assertions
'''''''''''''''''

Expand Down
Loading