44
55use Http \Client \Common \Plugin ;
66use Http \Client \Common \Plugin \Exception \RewindStreamException ;
7+ use Http \Client \Common \Plugin \Cache \Generator \CacheKeyGenerator ;
8+ use Http \Client \Common \Plugin \Cache \Generator \SimpleGenerator ;
79use Http \Message \StreamFactory ;
810use Http \Promise \FulfilledPromise ;
911use Psr \Cache \CacheItemInterface ;
@@ -55,7 +57,8 @@ final class CachePlugin implements Plugin
5557 * we have to store the cache for a longer time than the server originally says it is valid for.
5658 * We store a cache item for $cache_lifetime + max age of the response.
5759 * @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
5962 * }
6063 */
6164 public function __construct (CacheItemPoolInterface $ pool , StreamFactory $ streamFactory , array $ config = [])
@@ -73,6 +76,10 @@ public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamF
7376 $ optionsResolver = new OptionsResolver ();
7477 $ this ->configureOptions ($ optionsResolver );
7578 $ this ->config = $ optionsResolver ->resolve ($ config );
79+
80+ if (null === $ this ->config ['cache_key_generator ' ]) {
81+ $ this ->config ['cache_key_generator ' ] = new SimpleGenerator ();
82+ }
7683 }
7784
7885 /**
@@ -282,12 +289,9 @@ private function getCacheControlDirective(ResponseInterface $response, $name)
282289 */
283290 private function createCacheKey (RequestInterface $ request )
284291 {
285- $ body = (string ) $ request ->getBody ();
286- if (!empty ($ body )) {
287- $ body = ' ' .$ body ;
288- }
292+ $ key = $ this ->config ['cache_key_generator ' ]->generate ($ request );
289293
290- return hash ($ this ->config ['hash_algo ' ], $ request -> getMethod (). ' ' . $ request -> getUri (). $ body );
294+ return hash ($ this ->config ['hash_algo ' ], $ key );
291295 }
292296
293297 /**
@@ -338,12 +342,14 @@ private function configureOptions(OptionsResolver $resolver)
338342 'hash_algo ' => 'sha1 ' ,
339343 'methods ' => ['GET ' , 'HEAD ' ],
340344 'respect_response_cache_directives ' => ['no-cache ' , 'private ' , 'max-age ' , 'no-store ' ],
345+ 'cache_key_generator ' => null ,
341346 ]);
342347
343348 $ resolver ->setAllowedTypes ('cache_lifetime ' , ['int ' , 'null ' ]);
344349 $ resolver ->setAllowedTypes ('default_ttl ' , ['int ' , 'null ' ]);
345350 $ resolver ->setAllowedTypes ('respect_cache_headers ' , 'bool ' );
346351 $ resolver ->setAllowedTypes ('methods ' , 'array ' );
352+ $ resolver ->setAllowedTypes ('cache_key_generator ' , ['null ' , 'Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator ' ]);
347353 $ resolver ->setAllowedValues ('hash_algo ' , hash_algos ());
348354 $ resolver ->setAllowedValues ('methods ' , function ($ value ) {
349355 /* RFC7230 sections 3.1.1 and 3.2.6 except limited to uppercase characters. */
0 commit comments