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 5 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
9 changes: 6 additions & 3 deletions src/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ final class CachePlugin implements Plugin
*
* @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 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 @@ -77,7 +78,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
}

return $next($request)->then(function (ResponseInterface $response) use ($cacheItem) {
if ($this->isCacheable($response)) {
if ($this->isCacheable($response) && ($maxAge = $this->getMaxAge($response)) > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this from another PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Err, oops...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this from the github online editor, lol.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😝

$bodyStream = $response->getBody();
$body = $bodyStream->__toString();
if ($bodyStream->isSeekable()) {
Expand All @@ -87,7 +88,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
}

$cacheItem->set(['response' => $response, 'body' => $body])
->expiresAfter($this->getMaxAge($response));
->expiresAfter($maxAge);
$this->pool->save($cacheItem);
}

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());
}
}