Skip to content

Commit 47ac710

Browse files
author
Till Hildebrandt
committed
Added blacklisted_paths option
- adds a list of strings (regular expressions) to describe patterns of paths not to be cached.
1 parent 0f75407 commit 47ac710

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## 1.6.1 - 2019-11-25
4+
5+
### Added
6+
7+
* 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.
8+
39
## 1.6.0 - 2019-01-23
410

511
### Added

src/CachePlugin.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ final class CachePlugin implements Plugin
6060
* we have to store the cache for a longer time than the server originally says it is valid for.
6161
* We store a cache item for $cache_lifetime + max age of the response.
6262
* @var array $methods list of request methods which can be cached
63+
* @var array $blacklisted_paths list of regex patterns of paths explicitly not to be cached.
6364
* @var array $respect_response_cache_directives list of cache directives this plugin will respect while caching responses
6465
* @var CacheKeyGenerator $cache_key_generator an object to generate the cache key. Defaults to a new instance of SimpleGenerator
6566
* @var CacheListener[] $cache_listeners an array of objects to act on the response based on the results of the cache check.
@@ -183,7 +184,7 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
183184
return $this->handleCacheListeners($request, $this->createResponseFromCacheItem($cacheItem), true, $cacheItem);
184185
}
185186

186-
if ($this->isCacheable($response)) {
187+
if ($this->isCacheable($request, $response)) {
187188
$bodyStream = $response->getBody();
188189
$body = $bodyStream->__toString();
189190
if ($bodyStream->isSeekable()) {
@@ -246,16 +247,23 @@ private function calculateResponseExpiresAt($maxAge)
246247
/**
247248
* Verify that we can cache this response.
248249
*
250+
* @param RequestInterface $request
249251
* @param ResponseInterface $response
250252
*
251253
* @return bool
252254
*/
253-
protected function isCacheable(ResponseInterface $response)
255+
protected function isCacheable(RequestInterface $request, ResponseInterface $response)
254256
{
255257
if (!in_array($response->getStatusCode(), [200, 203, 300, 301, 302, 404, 410])) {
256258
return false;
257259
}
258260

261+
foreach ($this->config['blacklisted_paths'] as $not_to_cache_path) {
262+
if (1 === preg_match('/'.$not_to_cache_path.'/', $request->getRequestTarget())) {
263+
return false;
264+
}
265+
}
266+
259267
$nocacheDirectives = array_intersect($this->config['respect_response_cache_directives'], $this->noCacheFlags);
260268
foreach ($nocacheDirectives as $nocacheDirective) {
261269
if ($this->getCacheControlDirective($response, $nocacheDirective)) {
@@ -353,13 +361,15 @@ private function configureOptions(OptionsResolver $resolver)
353361
'respect_response_cache_directives' => ['no-cache', 'private', 'max-age', 'no-store'],
354362
'cache_key_generator' => null,
355363
'cache_listeners' => [],
364+
'blacklisted_paths' => [], // restricted for
356365
]);
357366

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

0 commit comments

Comments
 (0)