Skip to content

Commit 741b799

Browse files
committed
Rework hash file cache logic to avoid generating new file on each container regeneration
1 parent c34e0fc commit 741b799

2 files changed

Lines changed: 24 additions & 13 deletions

File tree

src/Symfony/SymfonyContainerResultCacheMetaExtension.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPStan\Analyser\ResultCache\ResultCacheMetaExtension;
66
use function array_map;
7+
use function basename;
78
use function file_get_contents;
89
use function file_put_contents;
910
use function hash;
@@ -44,23 +45,27 @@ public function getHash(): string
4445
if ($xmlHash === false) {
4546
throw new XmlContainerNotExistsException(sprintf('Container %s does not exist', $this->containerXmlPath));
4647
}
47-
$hashFile = sprintf('%s/%s-%s.hash', $this->tmpDir, $this->getKey(), $xmlHash);
48-
$hash = @file_get_contents($hashFile);
49-
if ($hash !== false) {
50-
return $hash;
48+
$xmlHashFile = sprintf('%s/%s-%s.hash', $this->tmpDir, $this->getKey(), basename($this->containerXmlPath));
49+
$storedXmlHash = @file_get_contents($xmlHashFile);
50+
51+
$hashForResultCacheFile = sprintf('%s/%s-%s-result-cache-meta.hash', $this->tmpDir, $this->getKey(), basename($this->containerXmlPath));
52+
$storedHashForResultCache = @file_get_contents($hashForResultCacheFile);
53+
if ($storedXmlHash === $xmlHash && $storedHashForResultCache !== false) {
54+
return $storedHashForResultCache;
5155
}
5256
}
5357

54-
$hash = $this->calculateHash();
58+
$hashForResultCache = $this->calculateHash();
5559

5660
if ($this->containerXmlPath !== null) {
57-
file_put_contents($hashFile, $hash);
61+
file_put_contents($hashForResultCacheFile, $hashForResultCache);
62+
file_put_contents($xmlHashFile, $xmlHash);
5863
}
5964

60-
return $hash;
65+
return $hashForResultCache;
6166
}
6267

63-
public function calculateHash(): string
68+
private function calculateHash(): string
6469
{
6570
$services = $parameters = [];
6671

tests/Symfony/SymfonyContainerResultCacheMetaExtensionTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,22 @@ public function testHashIsCalculatedAndWrittenToCacheFileOnCacheMiss(): void
5454

5555
$hash = $extension->getHash();
5656

57-
$cacheFile = sprintf('%s/symfonyDiContainer-c55d6ac45b535d6ecc9402cbb93825c38ec7b11b03f66577d0d3549b3d9ef75f.hash', $this->tmpDir);
58-
self::assertFileExists($cacheFile);
59-
self::assertSame($hash, file_get_contents($cacheFile));
57+
$resultCacheHashFile = sprintf('%s/symfonyDiContainer-container.xml-result-cache-meta.hash', $this->tmpDir);
58+
self::assertFileExists($resultCacheHashFile);
59+
self::assertSame($hash, file_get_contents($resultCacheHashFile));
60+
61+
$xmlHashFile = sprintf('%s/symfonyDiContainer-container.xml.hash', $this->tmpDir);
62+
self::assertFileExists($xmlHashFile);
63+
self::assertSame('c55d6ac45b535d6ecc9402cbb93825c38ec7b11b03f66577d0d3549b3d9ef75f', file_get_contents($xmlHashFile));
6064
}
6165

6266
public function testCachedHashIsReturnedOnCacheHit(): void
6367
{
6468
$containerXmlPath = __DIR__ . '/container.xml';
65-
$cacheFile = sprintf('%s/symfonyDiContainer-c55d6ac45b535d6ecc9402cbb93825c38ec7b11b03f66577d0d3549b3d9ef75f.hash', $this->tmpDir);
66-
file_put_contents($cacheFile, 'pre-computed-hash');
69+
$xmlHashFile = sprintf('%s/symfonyDiContainer-container.xml.hash', $this->tmpDir);
70+
file_put_contents($xmlHashFile, 'c55d6ac45b535d6ecc9402cbb93825c38ec7b11b03f66577d0d3549b3d9ef75f');
71+
$resultCacheHashFile = sprintf('%s/symfonyDiContainer-container.xml-result-cache-meta.hash', $this->tmpDir);
72+
file_put_contents($resultCacheHashFile, 'pre-computed-hash');
6773

6874
$extension = new SymfonyContainerResultCacheMetaExtension(
6975
new DefaultParameterMap([]),

0 commit comments

Comments
 (0)