Skip to content

Commit b7d9338

Browse files
committed
set s-maxage only if all responses are cacheable
1 parent 0451003 commit b7d9338

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

src/Symfony/Component/HttpKernel/HttpCache/EsiResponseCacheStrategy.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class EsiResponseCacheStrategy implements EsiResponseCacheStrategyInterface
3232
private $embeddedResponses = 0;
3333
private $ttls = array();
3434
private $maxAges = array();
35+
private $isNotCacheableResponseEmbedded = false;
3536

3637
/**
3738
* {@inheritdoc}
@@ -41,8 +42,13 @@ public function add(Response $response)
4142
if ($response->isValidateable()) {
4243
$this->cacheable = false;
4344
} else {
45+
$maxAge = $response->getMaxAge();
4446
$this->ttls[] = $response->getTtl();
45-
$this->maxAges[] = $response->getMaxAge();
47+
$this->maxAges[] = $maxAge;
48+
49+
if (null === $maxAge) {
50+
$this->isNotCacheableResponseEmbedded = true;
51+
}
4652
}
4753

4854
++$this->embeddedResponses;
@@ -76,7 +82,9 @@ public function update(Response $response)
7682
$this->ttls[] = $response->getTtl();
7783
$this->maxAges[] = $response->getMaxAge();
7884

79-
if (null !== $maxAge = min($this->maxAges)) {
85+
if ($this->isNotCacheableResponseEmbedded) {
86+
$response->headers->removeCacheControlDirective('s-maxage');
87+
} elseif (null !== $maxAge = min($this->maxAges)) {
8088
$response->setSharedMaxAge($maxAge);
8189
$response->headers->set('Age', $maxAge - min($this->ttls));
8290
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* This code is partially based on the Rack-Cache library by Ryan Tomayko,
9+
* which is released under the MIT license.
10+
* (based on commit 02d2b48d75bcb63cf1c0c7149c077ad256542801)
11+
*
12+
* For the full copyright and license information, please view the LICENSE
13+
* file that was distributed with this source code.
14+
*/
15+
16+
namespace Symfony\Component\HttpKernel\Tests\HttpCache;
17+
18+
use Symfony\Component\HttpFoundation\Response;
19+
use Symfony\Component\HttpKernel\HttpCache\EsiResponseCacheStrategy;
20+
21+
class EsiResponseCacheStrategyTest extends \PHPUnit_Framework_TestCase
22+
{
23+
public function testMinimumSharedMaxAgeWins()
24+
{
25+
$cacheStrategy = new EsiResponseCacheStrategy();
26+
27+
$response1 = new Response();
28+
$response1->setSharedMaxAge(60);
29+
$cacheStrategy->add($response1);
30+
31+
$response2 = new Response();
32+
$response2->setSharedMaxAge(3600);
33+
$cacheStrategy->add($response2);
34+
35+
$response = new Response();
36+
$response->setSharedMaxAge(86400);
37+
$cacheStrategy->update($response);
38+
39+
$this->assertSame('60', $response->headers->getCacheControlDirective('s-maxage'));
40+
}
41+
42+
public function testSharedMaxAgeNotSetIfNotSetInAnyEmbeddedRequest()
43+
{
44+
$cacheStrategy = new EsiResponseCacheStrategy();
45+
46+
$response1 = new Response();
47+
$response1->setSharedMaxAge(60);
48+
$cacheStrategy->add($response1);
49+
50+
$response2 = new Response();
51+
$cacheStrategy->add($response2);
52+
53+
$response = new Response();
54+
$response->setSharedMaxAge(86400);
55+
$cacheStrategy->update($response);
56+
57+
$this->assertFalse($response->headers->hasCacheControlDirective('s-maxage'));
58+
}
59+
60+
public function testSharedMaxAgeNotSetIfNotSetInMasterRequest()
61+
{
62+
$cacheStrategy = new EsiResponseCacheStrategy();
63+
64+
$response1 = new Response();
65+
$response1->setSharedMaxAge(60);
66+
$cacheStrategy->add($response1);
67+
68+
$response2 = new Response();
69+
$response2->setSharedMaxAge(3600);
70+
$cacheStrategy->add($response2);
71+
72+
$response = new Response();
73+
$cacheStrategy->update($response);
74+
75+
$this->assertFalse($response->headers->hasCacheControlDirective('s-maxage'));
76+
}
77+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy