Skip to content

Commit

Permalink
#5 If "no-cache" stale the cache entry
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevinrob committed Jun 30, 2015
1 parent e8f5418 commit 60902f4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/AbstractPrivateCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ protected function getCacheObject(ResponseInterface $response)
return null;
}

if (in_array("no-cache", $cacheControlDirectives)) {
// Stale response (RFC 7234 5.2.1.4)
$entry = new CacheEntry($response, new \DateTime('-1 seconds'));
return $entry->hasValidationInformation() ? $entry : null;
}

foreach ($cacheControlDirectives as $directive) {
$matches = [];

Expand Down
8 changes: 8 additions & 0 deletions src/CacheEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,12 @@ public function serveStaleIfError()
return (bool)$this->staleIfError;
}

/**
* @return bool
*/
public function hasValidationInformation()
{
return $this->response->hasHeader("Etag") || $this->response->hasHeader("Last-Modified");
}

}
18 changes: 18 additions & 0 deletions tests/HeaderCacheControlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public function setUp()
(new Response())
->withAddedHeader("Cache-Control", "no-store")
);
case '/no-cache':
if ($request->getHeaderLine("If-None-Match") == "TheHash") {
return new FulfilledPromise(new Response(304));
}

return new FulfilledPromise(
(new Response())
->withAddedHeader("Cache-Control", "no-cache")
->withAddedHeader("Etag", "TheHash")
);
}

throw new \InvalidArgumentException();
Expand Down Expand Up @@ -68,4 +78,12 @@ public function testNoStoreHeader()
$this->assertEquals("", $response->getHeaderLine("X-Cache"));
}

public function testNoCacheHeader()
{
$this->client->get("http://test.com/no-cache");

$response = $this->client->get("http://test.com/no-cache");
$this->assertEquals("HIT with validation", $response->getHeaderLine("X-Cache"));
}

}

0 comments on commit 60902f4

Please sign in to comment.