Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## 1.6.1 - 2019-11-25

### Added

* Added `blacklisted_paths` option, which takes an array of `strings` (regular expressions) and allows to define paths, that shall not be cached in any case.

## 1.6.0 - 2019-01-23

### Added
Expand Down
19 changes: 13 additions & 6 deletions src/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ final class CachePlugin implements Plugin
* we have to store the cache for a longer time than the server originally says it is valid for.
* We store a cache item for $cache_lifetime + max age of the response.
* @var array $methods list of request methods which can be cached
* @var array $blacklisted_paths list of regex patterns of paths explicitly not to be cached
* @var array $respect_response_cache_directives list of cache directives this plugin will respect while caching responses
* @var CacheKeyGenerator $cache_key_generator an object to generate the cache key. Defaults to a new instance of SimpleGenerator
* @var CacheListener[] $cache_listeners an array of objects to act on the response based on the results of the cache check.
Expand All @@ -72,10 +73,7 @@ public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamF
$this->streamFactory = $streamFactory;

if (isset($config['respect_cache_headers']) && isset($config['respect_response_cache_directives'])) {
throw new \InvalidArgumentException(
'You can\'t provide config option "respect_cache_headers" and "respect_response_cache_directives". '.
'Use "respect_response_cache_directives" instead.'
);
throw new \InvalidArgumentException('You can\'t provide config option "respect_cache_headers" and "respect_response_cache_directives". '.'Use "respect_response_cache_directives" instead.');
}

$optionsResolver = new OptionsResolver();
Expand Down Expand Up @@ -183,7 +181,7 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
return $this->handleCacheListeners($request, $this->createResponseFromCacheItem($cacheItem), true, $cacheItem);
}

if ($this->isCacheable($response)) {
if ($this->isCacheable($request, $response)) {
$bodyStream = $response->getBody();
$body = $bodyStream->__toString();
if ($bodyStream->isSeekable()) {
Expand Down Expand Up @@ -246,16 +244,23 @@ private function calculateResponseExpiresAt($maxAge)
/**
* Verify that we can cache this response.
*
* @param RequestInterface $request
* @param ResponseInterface $response
*
* @return bool
*/
protected function isCacheable(ResponseInterface $response)
protected function isCacheable(RequestInterface $request, ResponseInterface $response)
{
if (!in_array($response->getStatusCode(), [200, 203, 300, 301, 302, 404, 410])) {
return false;
}

foreach ($this->config['blacklisted_paths'] as $not_to_cache_path) {
if (1 === preg_match('/'.$not_to_cache_path.'/', $request->getRequestTarget())) {
return false;
}
}

$nocacheDirectives = array_intersect($this->config['respect_response_cache_directives'], $this->noCacheFlags);
foreach ($nocacheDirectives as $nocacheDirective) {
if ($this->getCacheControlDirective($response, $nocacheDirective)) {
Expand Down Expand Up @@ -353,13 +358,15 @@ private function configureOptions(OptionsResolver $resolver)
'respect_response_cache_directives' => ['no-cache', 'private', 'max-age', 'no-store'],
'cache_key_generator' => null,
'cache_listeners' => [],
'blacklisted_paths' => [], // restricted for
]);

$resolver->setAllowedTypes('cache_lifetime', ['int', 'null']);
$resolver->setAllowedTypes('default_ttl', ['int', 'null']);
$resolver->setAllowedTypes('respect_cache_headers', ['bool', 'null']);
$resolver->setAllowedTypes('methods', 'array');
$resolver->setAllowedTypes('cache_key_generator', ['null', 'Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator']);
$resolver->setAllowedTypes('blacklisted_paths', 'array');
$resolver->setAllowedValues('hash_algo', hash_algos());
$resolver->setAllowedValues('methods', function ($value) {
/* RFC7230 sections 3.1.1 and 3.2.6 except limited to uppercase characters. */
Expand Down