Skip to content

improve isCacheable readability #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 19, 2015
Merged
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
29 changes: 19 additions & 10 deletions src/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 (!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;
}

// 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);
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)
{
Expand Down