From 84ddfda9f9cd3ddb118c82b82fadc846ae353cc1 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Mon, 1 Aug 2016 23:04:58 +0100 Subject: [PATCH 1/3] The max age must not be null --- src/CachePlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 956be5b..d66c369 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -158,7 +158,7 @@ private function createCacheKey(RequestInterface $request) * * @param ResponseInterface $response * - * @return int|null + * @return int */ private function getMaxAge(ResponseInterface $response) { From 74c822af2439cf8bd20241014424b5e3ec31463a Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Mon, 1 Aug 2016 23:12:10 +0100 Subject: [PATCH 2/3] Ensure everything is an integer --- src/CachePlugin.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index d66c369..95165cd 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -163,7 +163,7 @@ private function createCacheKey(RequestInterface $request) private function getMaxAge(ResponseInterface $response) { if (!$this->config['respect_cache_headers']) { - return $this->config['default_ttl']; + return (int) $this->config['default_ttl']; } // check for max age in the Cache-Control header @@ -171,7 +171,7 @@ private function getMaxAge(ResponseInterface $response) if (!is_bool($maxAge)) { $ageHeaders = $response->getHeader('Age'); foreach ($ageHeaders as $age) { - return $maxAge - ((int) $age); + return ((int) $maxAge) - ((int) $age); } return (int) $maxAge; @@ -183,7 +183,7 @@ private function getMaxAge(ResponseInterface $response) return (new \DateTime($header))->getTimestamp() - (new \DateTime())->getTimestamp(); } - return $this->config['default_ttl']; + return (int) $this->config['default_ttl']; } /** From 37ee627518c4939756cb5c0fe68dbb4cd5678e1e Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Tue, 2 Aug 2016 00:12:56 +0100 Subject: [PATCH 3/3] Only cache if the max age is positive --- src/CachePlugin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 95165cd..fd71cb9 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -77,7 +77,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl } return $next($request)->then(function (ResponseInterface $response) use ($cacheItem) { - if ($this->isCacheable($response)) { + if ($this->isCacheable($response) && ($maxAge = $this->getMaxAge($response)) > 0) { $bodyStream = $response->getBody(); $body = $bodyStream->__toString(); if ($bodyStream->isSeekable()) { @@ -87,7 +87,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl } $cacheItem->set(['response' => $response, 'body' => $body]) - ->expiresAfter($this->getMaxAge($response)); + ->expiresAfter($maxAge); $this->pool->save($cacheItem); }