8000 Use xxh128 hash algorithm for PHP ^8.1 by alexander-schranz · Pull Request #44 · Toflar/psr6-symfony-http-cache-store · GitHub
[go: up one dir, main page]

Skip to content

Use xxh128 hash algorithm for PHP ^8.1 #44

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 1 commit into from
Dec 20, 2022
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
7 changes: 5 additions & 2 deletions src/Psr6Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class Psr6Store implements Psr6StoreInterface, ClearableInterface
*/
private array $locks = [];

private string $hashAlgorithm;

/**
* When creating a Psr6Store you can configure a number options.
* See the README for a list of all available options and their description.
Expand Down Expand Up @@ -98,6 +100,7 @@ public function __construct(array $options = [])
$this->options = $resolver->resolve($options);
$this->cache = $this->options['cache'];
$this->lockFactory = $this->options['lock_factory'];
$this->hashAlgorithm = version_compare(PHP_VERSION, '8.1.0', '>=') ? 'xxh128' : 'sha256';
}

public function lookup(Request $request): ?Response
Expand Down Expand Up @@ -323,7 +326,7 @@ public function getCacheKey(Request $request): string
$uri = $request->getUri();
$uri = substr($uri, \strlen($request->getScheme().'://'));

return 'md'.hash('sha256', $uri);
return 'md'.hash($this->hashAlgorithm, $uri);
}

/**
Expand All @@ -339,7 +342,7 @@ public function generateContentDigest(Response $response): ?string
return null;
}

return 'en'.hash('sha256', $response->getContent());
return 'en'.hash($this->hashAlgorithm, $response->getContent());
}

private function getVaryKey(array $vary, Request $request): string
Expand Down
34 changes: 29 additions & 5 deletions tests/Psr6StoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,12 @@ public function testVaryResponseDropsNonVaryingOne(): void
public function testRegularCacheKey(): void
{
$request = Request::create('https://foobar.com/');
$expected = 'md'.hash('sha256', 'foobar.com/');
$expected = 'md'.hash(
version_compare(PHP_VERSION, '8.1.0', '>=')
? 'xxh128'
: 'sha256',
'foobar.com/'
);
$this->assertSame($expected, $this->store->getCacheKey($request));
}

Expand Down Expand Up @@ -375,7 +380,11 @@ public function testRegularLookup(): void
$this->assertSame('hello world', $result->getContent());
$this->assertSame('whatever', $result->headers->get('Foobar'));

$this->assertSame('enb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9', $result->headers->get('X-Content-Digest'));
$this->assertSame(
version_compare(PHP_VERSION, '8.1.0', '>=')
? 'endf8d09e93f874900a99b8775cc15b6c7' // xxh128
: 'enb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9' // sha256
, $result->headers->get('X-Content-Digest'));
}

public function testRegularLookupWithContentDigestsDisabled(): void
Expand Down Expand Up @@ -851,10 +860,25 @@ public function testContentDigestExpiresCorrectly(array $responseHeaders, $expec
->expects($this->exactly(3))
->method('getItem')
->withConsecutive(
['enc3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2'], // content digest
['md390aa862a7f27c16d72dd40967066969e7eb4b102c6215478a275766bf046665'], // meta
[
// content digest
version_compare(PHP_VERSION, '8.1.0', '>=')
? 'en3c9e102628997f44ac87b0b131c6992d' // xxh128
: 'enc3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2' // sha256
],
[
// meta
version_compare(PHP_VERSION, '8.1.0', '>=')
? 'md0d10c3ce367c3309e789ed924fa6b183' // xxh128
: 'md390aa862a7f27c16d72dd40967066969e7eb4b102c6215478a275766bf046665' // sha256
],
[Psr6Store::COUNTER_KEY], // write counter
['md390aa862a7f27c16d72dd40967066969e7eb4b102c6215478a275766bf046665'] // meta again
[
// meta again
version_compare(PHP_VERSION, '8.1.0', '>=')
? 'md0d10c3ce367c3309e789ed924fa6b183' // xxh128
: 'md390aa862a7f27c16d72dd40967066969e7eb4b102c6215478a275766bf046665' // sha256
]
)
->willReturnOnConsecutiveCalls($contentDigestCacheItem, $cacheItem, $cacheItem, $cacheItem);

Expand Down
0