Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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);
}

Expand Down Expand Up @@ -158,20 +158,20 @@ 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'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the default value is null, this will become 0. Is that a good idea? Also, we have OptionsResolver in the plugin. @Nyholm any ide why null is allowed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, null needs to become 0. null is now allowed as an expiry time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely if the age is null, we shouldn't bother caching it anyways?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. Also, I think 0/null usually means unlimited cache ttl?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've updated the implementation. I'll probably turn off caching in my production app right now until this is resolved.

}

// check for max age in the Cache-Control header
$maxAge = $this->getCacheControlDirective($response, 'max-age');
if (!is_bool($maxAge)) {
$ageHeaders = $response->getHeader('Age');
foreach ($ageHeaders as $age) {
return $maxAge - ((int) $age);
return ((int) $maxAge) - ((int) $age);
}

return (int) $maxAge;
Expand All @@ -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'];
}

/**
Expand Down