Skip to content

Commit 2109df4

Browse files
committed
feat: allow configuring the reverse_proxy_ttl with cache control directives
1 parent b9a80ac commit 2109df4

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

Resources/doc/reference/configuration/headers.rst

+24-2
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,16 @@ keeping previously set Vary options:
315315
``reverse_proxy_ttl``
316316
"""""""""""""""""""""
317317

318-
**type**: ``integer``
318+
**type**: ``integer`` or ``array``
319319

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

325+
326+
If defined as an array, it follows logic of ``cache_control`` for supported directives.
327+
325328
.. warning::
326329

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

353+
Or as an array:
354+
355+
.. code-block:: yaml
356+
357+
# app/config/config.yml
358+
fos_http_cache:
359+
cache_control:
360+
rules:
361+
-
362+
headers:
363+
reverse_proxy_ttl:
364+
public: true
365+
max_age: 3600
366+
cache_control:
367+
public: true
368+
max_age: 0
369+
370+
This example adds the header ``X-Reverse-Proxy-TTL: max-age=3600, public`` to your responses.
371+
350372
``ttl_header``
351373
--------------
352374

src/DependencyInjection/Configuration.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,9 @@ private function addCacheControlSection(ArrayNodeDefinition $rootNode): void
333333
->end()
334334
->info('Set a default last modified timestamp if none is set yet. Value must be parseable by DateTime')
335335
->end()
336-
->scalarNode('reverse_proxy_ttl')
336+
->scalarNode('reverse_proxy_ttl') // TODO: make it handle scalar int (BC) or array (new)
337337
->defaultNull()
338-
->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.')
338+
->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.')
339339
->end()
340340
->arrayNode('vary')
341341
->beforeNormalization()->ifString()->then(function ($v) {

src/EventListener/CacheControlListener.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,16 @@ public function onKernelResponse(ResponseEvent $event): void
120120
&& null !== $options['reverse_proxy_ttl']
121121
&& !$response->headers->has($this->ttlHeader)
122122
) {
123-
$response->headers->set($this->ttlHeader, $options['reverse_proxy_ttl'], false);
123+
$reverseProxyTtl = $options['reverse_proxy_ttl'];
124+
125+
if (is_int($reverseProxyTtl)) { // BC layer: simple mode as integer
126+
$response->headers->set($this->ttlHeader, $reverseProxyTtl, false);
127+
} elseif (is_array($reverseProxyTtl)) { // config mode: similar to cache_control
128+
// only handle normal directives, no extra
129+
$directives = array_intersect_key($reverseProxyTtl, $this->supportedDirectives);
130+
$directives = array_map(function ($key) {return str_replace('_', '-', $key);}, $directives);
131+
$response->headers->set($this->ttlHeader, implode(' ', $directives), false);
132+
}
124133
}
125134

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

0 commit comments

Comments
 (0)