From e840edf79ba7b24ff20e38205d4855bb0cd6c204 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Sat, 19 Dec 2015 15:27:22 +0100 Subject: [PATCH 1/2] improve isCacheable --- src/CachePlugin.php | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 01c1a56..a7f5bf6 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -28,13 +28,17 @@ class CachePlugin implements Plugin private $defaultTtl; /** - * Look at the cache headers to know how long this response is going to be cached. + * Look at the cache headers to know whether this response may be cached and to + * decide how it can be cached. * - * @var bool + * @var bool Defaults to true */ private $respectCacheHeaders; /** + * Available options are + * - respect_cache_headers: Whether to look at the cache directives or ignore them. + * * @param CacheItemPoolInterface $pool * @param array $options */ @@ -86,21 +90,26 @@ public function handleRequest(RequestInterface $request, callable $next, callabl */ protected function isCacheable(ResponseInterface $response) { - $cachableCodes = [200, 203, 300, 301, 302, 404, 410]; - $privateHeaders = $this->getCacheControlDirective($response, 'no-store') || $this->getCacheControlDirective($response, 'private'); - - // If http status code is cachable and if we respect the headers, make sure there is no private cache headers. - return in_array($response->getStatusCode(), $cachableCodes) && !($this->respectCacheHeaders && $privateHeaders); + if (!in_array($response->getStatusCode(), [200, 203, 300, 301, 302, 404, 410])) { + return false; + } + if (!$this->respectCacheHeaders) { + return true; + } + if ($this->getCacheControlDirective($response, 'no-store') || $this->getCacheControlDirective($response, 'private')) { + return false; + } + + return true; } /** - * Returns the value of a parameter in the cache control header. If not found we return false. If found with no - * value return true. + * Get the value of a parameter in the cache control header. * * @param ResponseInterface $response - * @param string $name + * @param string $name The field of Cache-Control to fetch * - * @return bool|string + * @return bool|string The value of the directive, true if directive without value, false if directive not present. */ private function getCacheControlDirective(ResponseInterface $response, $name) { From f1fbc9ce97f77acee93faa512aeb5d1ff2f5f563 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Sat, 19 Dec 2015 09:45:35 -0500 Subject: [PATCH 2/2] Applied fixes from StyleCI --- src/CachePlugin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index a7f5bf6..4e07342 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -96,10 +96,10 @@ protected function isCacheable(ResponseInterface $response) if (!$this->respectCacheHeaders) { return true; } - if ($this->getCacheControlDirective($response, 'no-store') || $this->getCacheControlDirective($response, 'private')) { + if ($this->getCacheControlDirective($response, 'no-store') || $this->getCacheControlDirective($response, 'private')) { return false; } - + return true; }