4
4
5
5
use Http \Client \Common \Plugin ;
6
6
use Http \Client \Common \Plugin \Exception \RewindStreamException ;
7
+ use Http \Client \Common \Plugin \Cache \Generator \CacheKeyGenerator ;
8
+ use Http \Client \Common \Plugin \Cache \Generator \SimpleGenerator ;
7
9
use Http \Message \StreamFactory ;
8
10
use Http \Promise \FulfilledPromise ;
9
11
use Psr \Cache \CacheItemInterface ;
@@ -55,7 +57,8 @@ final class CachePlugin implements Plugin
55
57
* we have to store the cache for a longer time than the server originally says it is valid for.
56
58
* We store a cache item for $cache_lifetime + max age of the response.
57
59
* @var array $methods list of request methods which can be cached
58
- * @var array $respect_response_cache_directives list of cache directives this plugin will respect while caching responses.
60
+ * @var array $respect_response_cache_directives list of cache directives this plugin will respect while caching responses
61
+ * @var CacheKeyGenerator $cache_key_generator a class to generate the cache key. Defaults to SimpleGenerator
59
62
* }
60
63
*/
61
64
public function __construct (CacheItemPoolInterface $ pool , StreamFactory $ streamFactory , array $ config = [])
@@ -73,6 +76,10 @@ public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamF
73
76
$ optionsResolver = new OptionsResolver ();
74
77
$ this ->configureOptions ($ optionsResolver );
75
78
$ this ->config = $ optionsResolver ->resolve ($ config );
79
+
80
+ if (null === $ this ->config ['cache_key_generator ' ]) {
81
+ $ this ->config ['cache_key_generator ' ] = new SimpleGenerator ();
82
+ }
76
83
}
77
84
78
85
/**
@@ -282,12 +289,9 @@ private function getCacheControlDirective(ResponseInterface $response, $name)
282
289
*/
283
290
private function createCacheKey (RequestInterface $ request )
284
291
{
285
- $ body = (string ) $ request ->getBody ();
286
- if (!empty ($ body )) {
287
- $ body = ' ' .$ body ;
288
- }
292
+ $ key = $ this ->config ['cache_key_generator ' ]->generate ($ request );
289
293
290
- return hash ($ this ->config ['hash_algo ' ], $ request -> getMethod (). ' ' . $ request -> getUri (). $ body );
294
+ return hash ($ this ->config ['hash_algo ' ], $ key );
291
295
}
292
296
293
297
/**
@@ -338,12 +342,14 @@ private function configureOptions(OptionsResolver $resolver)
338
342
'hash_algo ' => 'sha1 ' ,
339
343
'methods ' => ['GET ' , 'HEAD ' ],
340
344
'respect_response_cache_directives ' => ['no-cache ' , 'private ' , 'max-age ' , 'no-store ' ],
345
+ 'cache_key_generator ' => null ,
341
346
]);
342
347
343
348
$ resolver ->setAllowedTypes ('cache_lifetime ' , ['int ' , 'null ' ]);
344
349
$ resolver ->setAllowedTypes ('default_ttl ' , ['int ' , 'null ' ]);
345
350
$ resolver ->setAllowedTypes ('respect_cache_headers ' , 'bool ' );
346
351
$ resolver ->setAllowedTypes ('methods ' , 'array ' );
352
+ $ resolver ->setAllowedTypes ('cache_key_generator ' , ['null ' , 'Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator ' ]);
347
353
$ resolver ->setAllowedValues ('hash_algo ' , hash_algos ());
348
354
$ resolver ->setAllowedValues ('methods ' , function ($ value ) {
349
355
/* RFC7230 sections 3.1.1 and 3.2.6 except limited to uppercase characters. */
0 commit comments