Skip to content

Commit

Permalink
DX: store cache to file only if content will get modified (#7151)
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus authored Jul 15, 2023
1 parent 1d63a8a commit 3b398f6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/Cache/FileCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ final class FileCacheManager implements CacheManagerInterface

private int $writeCounter = 0;

private bool $signatureWasUpdated = false;

/**
* @var CacheInterface
*/
Expand All @@ -65,7 +67,9 @@ public function __construct(

public function __destruct()
{
$this->writeCache();
if (true === $this->signatureWasUpdated || 0 !== $this->writeCounter) {
$this->writeCache();
}
}

/**
Expand Down Expand Up @@ -119,6 +123,7 @@ private function readCache(): void

if (null === $cache || !$this->signature->equals($cache->getSignature())) {
$cache = new Cache($this->signature);
$this->signatureWasUpdated = true;
}

$this->cache = $cache;
Expand Down
6 changes: 3 additions & 3 deletions tests/Cache/FileCacheManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function testCreatesCacheIfCachedSignatureIsDifferent(): void
unset($manager);
}

public function testUsesCacheIfCachedSignatureIsEqual(): void
public function testUsesCacheIfCachedSignatureIsEqualAndNoFileWasUpdated(): void
{
$cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();

Expand All @@ -105,7 +105,7 @@ public function testUsesCacheIfCachedSignatureIsEqual(): void

$handlerProphecy = $this->prophesize(FileHandlerInterface::class);
$handlerProphecy->read()->shouldBeCalled()->willReturn($cache);
$handlerProphecy->write(Argument::is($cache))->shouldBeCalled();
$handlerProphecy->write(Argument::is($cache))->shouldNotBeCalled();
$handler = $handlerProphecy->reveal();

$manager = new FileCacheManager(
Expand Down Expand Up @@ -267,7 +267,7 @@ public function testSetFileSetsHashOfFileContent(): void
$handlerProphecy = $this->prophesize(FileHandlerInterface::class);
$handlerProphecy->read()->willReturn($cache);
$handlerProphecy->getFile()->willReturn($cacheFile);
$handlerProphecy->write(Argument::is($cache));
$handlerProphecy->write(Argument::is($cache))->shouldBeCalled();
$handler = $handlerProphecy->reveal();

$manager = new FileCacheManager(
Expand Down

0 comments on commit 3b398f6

Please sign in to comment.