Skip to content

Commit

Permalink
feat: allow configuring the with cache control directives
Browse files Browse the repository at this point in the history
  • Loading branch information
94noni committed Jan 30, 2025
1 parent b9a80ac commit 1457c2b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
26 changes: 24 additions & 2 deletions Resources/doc/reference/configuration/headers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,16 @@ keeping previously set Vary options:
``reverse_proxy_ttl``
"""""""""""""""""""""

**type**: ``integer``
**type**: ``integer`` or ``array``

Set a custom TTL header for reverse proxy time-outs not driven by
If defined as integer, set a custom TTL header for reverse proxy time-outs not driven by
``s-maxage``. This keeps your ``s-maxage`` free for use with reverse proxies
not under your control. The default header name is ``X-Reverse-Proxy-TTL`` but
you can customize the header name as explained in the next section.


If defined as an array, it follows logic of ``cache_control`` for supported directives.

.. warning::

This is a custom header. You need to set up your caching proxy to respect
Expand All @@ -347,6 +350,25 @@ section:
This example adds the header ``X-Reverse-Proxy-TTL: 3600`` to your responses.

Or as an array:

.. code-block:: yaml
# app/config/config.yml
fos_http_cache:
cache_control:
rules:
-
headers:
reverse_proxy_ttl:
public: true
max_age: 3600
cache_control:
public: true
max_age: 0
This example adds the header ``X-Reverse-Proxy-TTL: max-age=3600, public`` to your responses.

``ttl_header``
--------------

Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,9 @@ private function addCacheControlSection(ArrayNodeDefinition $rootNode): void
->end()
->info('Set a default last modified timestamp if none is set yet. Value must be parseable by DateTime')
->end()
->scalarNode('reverse_proxy_ttl')
->scalarNode('reverse_proxy_ttl') // TODO: make it handle scalar int (BC) or array (new)
->defaultNull()
->info('Specify a custom time to live in seconds for your caching proxy. This value is sent in the custom header configured in cache_control.ttl_header.')
->info('Specify a custom time to live in seconds for your caching proxy. This value is sent in the custom header configured in cache_control.ttl_header. You can also specify a map like cache_control.')
->end()
->arrayNode('vary')
->beforeNormalization()->ifString()->then(function ($v) {
Expand Down
11 changes: 10 additions & 1 deletion src/EventListener/CacheControlListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,16 @@ public function onKernelResponse(ResponseEvent $event): void
&& null !== $options['reverse_proxy_ttl']
&& !$response->headers->has($this->ttlHeader)
) {
$response->headers->set($this->ttlHeader, $options['reverse_proxy_ttl'], false);
$reverseProxyTtl = $options['reverse_proxy_ttl'];

if (is_int($reverseProxyTtl)) { // BC layer: simple mode as integer
$response->headers->set($this->ttlHeader, $reverseProxyTtl, false);

Check failure on line 126 in src/EventListener/CacheControlListener.php

View workflow job for this annotation

GitHub Actions / PHPStan src

Parameter #2 $values of method Symfony\Component\HttpFoundation\ResponseHeaderBag::set() expects array<string>|string|null, int given.
} elseif (is_array($reverseProxyTtl)) { // config mode: similar to cache_control
// only handle normal directives, no extra
$directives = array_intersect_key($reverseProxyTtl, $this->supportedDirectives);
$directives = array_map(function ($key) {return str_replace('_', '-', $key);}, $directives);
$response->headers->set($this->ttlHeader, implode(' ', $directives), false);
}
}

if (!empty($options['vary'])) {
Expand Down

0 comments on commit 1457c2b

Please sign in to comment.