diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 956be5b..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); } @@ -158,12 +158,12 @@ private function createCacheKey(RequestInterface $request) * * @param ResponseInterface $response * - * @return int|null + * @return int */ 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']; } /**