Permalink
Browse files

Fix cache management

  • Loading branch information...
1 parent 6ae679d commit 1e6adfaa27f46d50973e6f14150e0585f876dd1e @rande rande committed Feb 21, 2014
Showing with 19 additions and 9 deletions.
  1. +5 −8 Cache/HttpCacheHandler.php
  2. +14 −1 Tests/Cache/HttpCacheHandlerTest.php
View
@@ -24,17 +24,14 @@ class HttpCacheHandler implements HttpCacheHandlerInterface
*/
public function alterResponse(Response $response)
{
- if ($response->getTtl() !== null) {
+ if (!$response->isCacheable()) {
+ // the controller flags the response as private so we keep it private!
return;
}
- if ($this->currentTtl === null) {
- return;
- }
-
- if ($this->currentTtl === 0) {
- $response->setPrivate();
- } else {
+ // a block has a lower ttl that the current response, so we update the ttl to match
+ // the one provided in the block
+ if ($this->currentTtl < $response->getTtl()) {
$response->setTtl($this->currentTtl);
}
}
@@ -19,7 +19,7 @@
*/
class HttpCacheHandlerTest extends \PHPUnit_Framework_TestCase
{
- public function testComputeTtl()
+ public function testComputeTtlWithPrivateResponse()
{
$handler = new HttpCacheHandler();
$handler->updateMetadata(Response::create()->setTtl(60));
@@ -29,6 +29,19 @@ public function testComputeTtl()
$handler->alterResponse($response = Response::create());
+ $this->assertEquals(0, $response->getTtl());
+ }
+
+ public function testComputeTtlWithPublicResponse()
+ {
+ $handler = new HttpCacheHandler();
+ $handler->updateMetadata(Response::create()->setTtl(60));
+ $handler->updateMetadata(Response::create()->setTtl(55));
+ $handler->updateMetadata(Response::create()->setTtl(42));
+ $handler->updateMetadata(Response::create()->setTtl(55));
+
+ $handler->alterResponse($response = Response::create()->setTtl(84));
+
$this->assertEquals(42, $response->getTtl());
}
}

0 comments on commit 1e6adfa

Please sign in to comment.