Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cef86a3

Browse filesBrowse files
committed
[HttpKernel] added a way to change the ESI cache strategy
1 parent fb00539 commit cef86a3
Copy full SHA for cef86a3

File tree

Expand file treeCollapse file tree

5 files changed

+127
-32
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+127
-32
lines changed

‎src/Symfony/Bundle/FrameworkBundle/HttpCache/HttpCache.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/HttpCache/HttpCache.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Symfony\Component\HttpKernel\HttpKernelInterface;
66
use Symfony\Component\HttpKernel\HttpCache\HttpCache as BaseHttpCache;
77
use Symfony\Component\HttpKernel\HttpCache\Esi;
8+
use Symfony\Component\HttpKernel\HttpCache\EsiResponseCacheStrategy;
89
use Symfony\Component\HttpKernel\HttpCache\Store;
910
use Symfony\Component\HttpFoundation\Request;
1011
use Symfony\Component\HttpFoundation\Response;

‎src/Symfony/Component/HttpKernel/HttpCache/Esi.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/HttpCache/Esi.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ public function __construct(array $contentTypes = array('text/html', 'text/xml',
4141
$this->contentTypes = $contentTypes;
4242
}
4343

44+
/**
45+
* Returns a new cache strategy instance.
46+
*
47+
* @return EsiResponseCacheStrategyInterface A EsiResponseCacheStrategyInterface instance
48+
*/
49+
public function createCacheStrategy()
50+
{
51+
return new EsiResponseCacheStrategy();
52+
}
53+
4454
/**
4555
* Checks that at least one surrogate has ESI/1.0 capability.
4656
*
+68Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien.potencier@symfony-project.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\HttpCache;
17+
18+
use Symfony\Component\HttpFoundation\Response;
19+
20+
/**
21+
* EsiResponseCacheStrategy knows how to compute the Response cache HTTP header
22+
* based on the different ESI response cache headers.
23+
*
24+
* This implementation changes the master response TTL to the smallest TTL received
25+
* or force validation if one of the ESI has validation cache strategy.
26+
*
27+
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
28+
*/
29+
class EsiResponseCacheStrategy implements EsiResponseCacheStrategyInterface
30+
{
31+
protected $cacheable = true;
32+
protected $ttls = array();
33+
protected $maxAges = array();
34+
35+
/**
36+
* Adds a Response.
37+
*
38+
* @param Response $response
39+
*/
40+
public function add(Response $response)
41+
{
42+
if ($response->isValidateable()) {
43+
$this->cacheable = false;
44+
} else {
45+
$this->ttls[] = $response->getTtl();
46+
$this->maxAges[] = $response->getMaxAge();
47+
}
48+
}
49+
50+
/**
51+
* Updates the Response HTTP headers based on the embedded Responses.
52+
*
53+
* @param Response $response
54+
*/
55+
public function update(Response $response)
56+
{
57+
if (!$this->cacheable) {
58+
$response->headers->set('Cache-Control', 'no-cache, must-revalidate');
59+
60+
return;
61+
}
62+
63+
$maxAge = min($this->maxAges);
64+
$response->setSharedMaxAge($maxAge);
65+
$response->setMaxAge(0);
66+
$response->headers->set('Age', $maxAge - min($this->ttls));
67+
}
68+
}
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien.potencier@symfony-project.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\HttpCache;
17+
18+
use Symfony\Component\HttpFoundation\Response;
19+
20+
/**
21+
* EsiResponseCacheStrategyInterface implementations know how to compute the
22+
* Response cache HTTP header based on the different ESI response cache headers.
23+
*
24+
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
25+
*/
26+
interface EsiResponseCacheStrategyInterface
27+
{
28+
/**
29+
* Adds a Response.
30+
*
31+
* @param Response $response
32+
*/
33+
function add(Response $response);
34+
35+
/**
36+
* Updates the Response HTTP headers based on the embedded Responses.
37+
*
38+
* @param Response $response
39+
*/
40+
function update(Response $response);
41+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
+7-32Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class HttpCache implements HttpKernelInterface
3131
protected $store;
3232
protected $request;
3333
protected $esi;
34-
protected $esiTtls;
34+
protected $esiCacheStrategy;
3535

3636
/**
3737
* Constructor.
@@ -137,7 +137,9 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
137137
if (HttpKernelInterface::MASTER_REQUEST === $type) {
138138
$this->traces = array();
139139
$this->request = $request;
140-
$this->esiTtls = array();
140+
if (null !== $this->esi) {
141+
$this->esiCacheStrategy = $this->esi->createCacheStrategy();
142+
}
141143
}
142144

143145
$path = $request->getPathInfo();
@@ -163,43 +165,16 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
163165
}
164166

165167
if (null !== $this->esi) {
166-
$this->addEsiTtl($response);
168+
$this->esiCacheStrategy->add($response);
167169

168-
if ($request === $this->request) {
169-
$this->updateResponseCacheControl($response);
170+
if (HttpKernelInterface::MASTER_REQUEST === $type) {
171+
$this->esiCacheStrategy->update($response);
170172
}
171173
}
172174

173175
return $response;
174176
}
175177

176-
/**
177-
* Stores the response's TTL locally.
178-
*
179-
* @param Response $response
180-
*/
181-
protected function addEsiTtl(Response $response)
182-
{
183-
$this->esiTtls[] = $response->isValidateable() ? -1 : $response->getTtl();
184-
}
185-
186-
/**
187-
* Changes the master response TTL to the smallest TTL received or force validation if
188-
* one of the ESI has validation cache strategy.
189-
*
190-
* @param Response $response
191-
*/
192-
protected function updateResponseCacheControl(Response $response)
193-
{
194-
$ttl = min($this->esiTtls);
195-
if (-1 === $ttl) {
196-
$response->headers->set('Cache-Control', 'no-cache, must-revalidate');
197-
} else {
198-
$response->setSharedMaxAge($ttl);
199-
$response->setMaxAge(0);
200-
}
201-
}
202-
203178
/**
204179
* Forwards the Request to the backend without storing the Response in the cache.
205180
*

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.