Skip to content

Use sha1 to reduce the risk of collisions #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions spec/CachePluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
Expand Down Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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());
}
}