Skip to content

Commit c811206

Browse files
Use xxh128 hash algorithm for PHP ^8.1 (#44)
1 parent 76f1e56 commit c811206

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/Psr6Store.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class Psr6Store implements Psr6StoreInterface, ClearableInterface
5757
*/
5858
private array $locks = [];
5959

60+
private string $hashAlgorithm;
61+
6062
/**
6163
* When creating a Psr6Store you can configure a number options.
6264
* See the README for a list of all available options and their description.
@@ -98,6 +100,7 @@ public function __construct(array $options = [])
98100
$this->options = $resolver->resolve($options);
99101
$this->cache = $this->options['cache'];
100102
$this->lockFactory = $this->options['lock_factory'];
103+
$this->hashAlgorithm = version_compare(PHP_VERSION, '8.1.0', '>=') ? 'xxh128' : 'sha256';
101104
}
102105

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

326-
return 'md'.hash('sha256', $uri);
329+
return 'md'.hash($this->hashAlgorithm, $uri);
327330
}
328331

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

342-
return 'en'.hash('sha256', $response->getContent());
345+
return 'en'.hash($this->hashAlgorithm, $response->getContent());
343346
}
344347

345348
private function getVaryKey(array $vary, Request $request): string

tests/Psr6StoreTest.php

+29-5
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,12 @@ public function testVaryResponseDropsNonVaryingOne(): void
333333
public function testRegularCacheKey(): void
334334
{
335335
$request = Request::create('https://foobar.com/');
336-
$expected = 'md'.hash('sha256', 'foobar.com/');
336+
$expected = 'md'.hash(
337+
version_compare(PHP_VERSION, '8.1.0', '>=')
338+
? 'xxh128'
339+
: 'sha256',
340+
'foobar.com/'
341+
);
337342
$this->assertSame($expected, $this->store->getCacheKey($request));
338343
}
339344

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

378-
$this->assertSame('enb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9', $result->headers->get('X-Content-Digest'));
383+
$this->assertSame(
384+
version_compare(PHP_VERSION, '8.1.0', '>=')
385+
? 'endf8d09e93f874900a99b8775cc15b6c7' // xxh128
386+
: 'enb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9' // sha256
387+
, $result->headers->get('X-Content-Digest'));
379388
}
380389

381390
public function testRegularLookupWithContentDigestsDisabled(): void
@@ -851,10 +860,25 @@ public function testContentDigestExpiresCorrectly(array $responseHeaders, $expec
851860
->expects($this->exactly(3))
852861
->method('getItem')
853862
->withConsecutive(
854-
['enc3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2'], // content digest
855-
['md390aa862a7f27c16d72dd40967066969e7eb4b102c6215478a275766bf046665'], // meta
863+
[
864+
// content digest
865+
version_compare(PHP_VERSION, '8.1.0', '>=')
866+
? 'en3c9e102628997f44ac87b0b131c6992d' // xxh128
867+
: 'enc3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2' // sha256
868+
],
869+
[
870+
// meta
871+
version_compare(PHP_VERSION, '8.1.0', '>=')
872+
? 'md0d10c3ce367c3309e789ed924fa6b183' // xxh128
873+
: 'md390aa862a7f27c16d72dd40967066969e7eb4b102c6215478a275766bf046665' // sha256
874+
],
856875
[Psr6Store::COUNTER_KEY], // write counter
857-
['md390aa862a7f27c16d72dd40967066969e7eb4b102c6215478a275766bf046665'] // meta again
876+
[
877+
// meta again
878+
version_compare(PHP_VERSION, '8.1.0', '>=')
879+
? 'md0d10c3ce367c3309e789ed924fa6b183' // xxh128
880+
: 'md390aa862a7f27c16d72dd40967066969e7eb4b102c6215478a275766bf046665' // sha256
881+
]
858882
)
859883
->willReturnOnConsecutiveCalls($contentDigestCacheItem, $cacheItem, $cacheItem, $cacheItem);
860884

0 commit comments

Comments
 (0)