diff --git a/spec/CachePluginSpec.php b/spec/CachePluginSpec.php index a46327e..808806d 100644 --- a/spec/CachePluginSpec.php +++ b/spec/CachePluginSpec.php @@ -42,7 +42,7 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i $response->getHeader('Cache-Control')->willReturn(array()); $response->getHeader('Expires')->willReturn(array()); - $pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item); + $pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item); $item->isHit()->willReturn(false); $item->set(['response' => $response, 'body' => $httpBody])->willReturn($item)->shouldBeCalled(); $item->expiresAfter(60)->willReturn($item)->shouldBeCalled(); @@ -63,7 +63,7 @@ function it_doesnt_store_failed_responses(CacheItemPoolInterface $pool, CacheIte $response->getHeader('Cache-Control')->willReturn(array()); $response->getHeader('Expires')->willReturn(array()); - $pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item); + $pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item); $item->isHit()->willReturn(false); $next = function (RequestInterface $request) use ($response) { @@ -101,7 +101,7 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI $response->getHeader('Age')->willReturn(array('15')); $response->getHeader('Expires')->willReturn(array()); - $pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item); + $pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item); $item->isHit()->willReturn(false); // 40-15 should be 25 diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 956be5b..f0bdbd4 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -38,7 +38,8 @@ final class CachePlugin implements Plugin * @param array $config { * * @var bool $respect_cache_headers Whether to look at the cache directives or ignore them - * @var int $default_ttl If we do not respect cache headers or can't calculate a good ttl, use this value. + * @var int $default_ttl If we do not respect cache headers or can't calculate a good ttl, use this value + * @var string $hash_algo The hashing algorithm to use when generating cache keys. * } */ public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) @@ -150,7 +151,7 @@ private function getCacheControlDirective(ResponseInterface $response, $name) */ private function createCacheKey(RequestInterface $request) { - return md5($request->getMethod().' '.$request->getUri()); + return hash($this->config['hash_algo'], $request->getMethod().' '.$request->getUri()); } /** @@ -196,9 +197,11 @@ private function configureOptions(OptionsResolver $resolver) $resolver->setDefaults([ 'default_ttl' => null, 'respect_cache_headers' => true, + 'hash_algo' => 'sha1', ]); $resolver->setAllowedTypes('default_ttl', ['int', 'null']); $resolver->setAllowedTypes('respect_cache_headers', 'bool'); + $resolver->setAllowedValues('hash_algo', hash_algos()); } }