@@ -60,6 +60,7 @@ final class CachePlugin implements Plugin
60
60
* we have to store the cache for a longer time than the server originally says it is valid for.
61
61
* We store a cache item for $cache_lifetime + max age of the response.
62
62
* @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.
63
64
* @var array $respect_response_cache_directives list of cache directives this plugin will respect while caching responses
64
65
* @var CacheKeyGenerator $cache_key_generator an object to generate the cache key. Defaults to a new instance of SimpleGenerator
65
66
* @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
183
184
return $ this ->handleCacheListeners ($ request , $ this ->createResponseFromCacheItem ($ cacheItem ), true , $ cacheItem );
184
185
}
185
186
186
- if ($ this ->isCacheable ($ response )) {
187
+ if ($ this ->isCacheable ($ request , $ response )) {
187
188
$ bodyStream = $ response ->getBody ();
188
189
$ body = $ bodyStream ->__toString ();
189
190
if ($ bodyStream ->isSeekable ()) {
@@ -246,16 +247,23 @@ private function calculateResponseExpiresAt($maxAge)
246
247
/**
247
248
* Verify that we can cache this response.
248
249
*
250
+ * @param RequestInterface $request
249
251
* @param ResponseInterface $response
250
252
*
251
253
* @return bool
252
254
*/
253
- protected function isCacheable (ResponseInterface $ response )
255
+ protected function isCacheable (RequestInterface $ request , ResponseInterface $ response )
254
256
{
255
257
if (!in_array ($ response ->getStatusCode (), [200 , 203 , 300 , 301 , 302 , 404 , 410 ])) {
256
258
return false ;
257
259
}
258
260
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
+
259
267
$ nocacheDirectives = array_intersect ($ this ->config ['respect_response_cache_directives ' ], $ this ->noCacheFlags );
260
268
foreach ($ nocacheDirectives as $ nocacheDirective ) {
261
269
if ($ this ->getCacheControlDirective ($ response , $ nocacheDirective )) {
@@ -353,13 +361,15 @@ private function configureOptions(OptionsResolver $resolver)
353
361
'respect_response_cache_directives ' => ['no-cache ' , 'private ' , 'max-age ' , 'no-store ' ],
354
362
'cache_key_generator ' => null ,
355
363
'cache_listeners ' => [],
364
+ 'blacklisted_paths ' => [], // restricted for
356
365
]);
357
366
358
367
$ resolver ->setAllowedTypes ('cache_lifetime ' , ['int ' , 'null ' ]);
359
368
$ resolver ->setAllowedTypes ('default_ttl ' , ['int ' , 'null ' ]);
360
369
$ resolver ->setAllowedTypes ('respect_cache_headers ' , ['bool ' , 'null ' ]);
361
370
$ resolver ->setAllowedTypes ('methods ' , 'array ' );
362
371
$ resolver ->setAllowedTypes ('cache_key_generator ' , ['null ' , 'Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator ' ]);
372
+ $ resolver ->setAllowedTypes ('blacklisted_paths ' , 'array ' );
363
373
$ resolver ->setAllowedValues ('hash_algo ' , hash_algos ());
364
374
$ resolver ->setAllowedValues ('methods ' , function ($ value ) {
365
375
/* RFC7230 sections 3.1.1 and 3.2.6 except limited to uppercase characters. */
0 commit comments