Skip to content
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

feat: add parallel cache support #7131

Merged
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,18 @@ public static function fromJson(string $json): self

return $cache;
}

/**
* @internal
*/
public function backfillHashes(self $oldCache): bool
{
if (!$this->getSignature()->equals($oldCache->getSignature())) {
return false;
}

$this->hashes = array_merge($oldCache->hashes, $this->hashes);

return true;
}
}
86 changes: 67 additions & 19 deletions src/Cache/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ final class FileHandler implements FileHandlerInterface
{
private string $file;

private int $fileMTime = 0;

public function __construct(string $file)
{
$this->file = $file;
Expand All @@ -41,21 +43,80 @@ public function read(): ?CacheInterface
return null;
}

$content = file_get_contents($this->file);

try {
$cache = Cache::fromJson($content);
} catch (\InvalidArgumentException $exception) {
$handle = fopen($this->file, 'r');
if (false === $handle) {
return null;
}

$cache = $this->readFromHandle($handle);
$this->fileMTime = $this->getFileCurrentMTime();

fclose($handle);

return $cache;
}

public function write(CacheInterface $cache): void
{
$content = $cache->toJson();
$this->ensureFileIsWriteable();

$handle = fopen($this->file, 'r+');
if (false === $handle) {
return;
}

if (method_exists($cache, 'backfillHashes') && $this->fileMTime < $this->getFileCurrentMTime()) {
flock($handle, LOCK_EX);
$oldCache = $this->readFromHandle($handle);
rewind($handle);

if (null !== $oldCache) {
$cache->backfillHashes($oldCache);
}
}
ftruncate($handle, 0);
fwrite($handle, $cache->toJson());
Copy link
Member Author

Choose a reason for hiding this comment

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

I wonder about putting @ to silence warning and check !== false for every method here... any better suggestion?

fflush($handle);
fsync($handle);
$this->fileMTime = $this->getFileCurrentMTime();
keradus marked this conversation as resolved.
Show resolved Hide resolved
fclose($handle);
}

private function getFileCurrentMTime(): int
{
clearstatcache(true, $this->file);

$mtime = filemtime($this->file);

if (false === $mtime) {
// cannot check mtime? OK, let's pretend file is old
$mtime = 0;
}

return $mtime;
}

/**
* @param resource $handle
*/
private function readFromHandle($handle): ?CacheInterface
{
try {
$size = @filesize($this->file);
if (false === $size || 0 === $size) {
return null;
}

$content = fread($handle, $size);

return Cache::fromJson($content);
} catch (\InvalidArgumentException $exception) {
return null;
}
}

private function ensureFileIsWriteable(): void
{
if (file_exists($this->file)) {
if (is_dir($this->file)) {
throw new IOException(
Expand Down Expand Up @@ -95,18 +156,5 @@ public function write(CacheInterface $cache): void
@touch($this->file);
@chmod($this->file, 0666);
}

$bytesWritten = @file_put_contents($this->file, $content);

if (false === $bytesWritten) {
$error = error_get_last();

throw new IOException(
sprintf('Failed to write file "%s", "%s".', $this->file, $error['message'] ?? 'no reason available'),
0,
null,
$this->file
);
}
}
}