Skip to content

Commit

Permalink
Use atomic cache writing. (#1186)
Browse files Browse the repository at this point in the history
Create a temp file, write the content and finally rename the file to the destination.
Because several concurrent processes accessing the cache are running into an error.
  • Loading branch information
PATROMO committed May 25, 2020
1 parent dedde5a commit 10bc24b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@ mPDF 8.1.x
* Add Page Number Myanmar Language Support
* new `Mpdf\Exception\FontException` extending base `MpdfException` was introduced and is thrown on Font manipulation
* A bit cleaner exception messages for font-related errors
* Use atomicity cache writing. Create a temp file, write the content and finally rename the file to the destination.

mPDF 8.0.x
===========================
Expand Down
6 changes: 4 additions & 2 deletions src/Cache.php
Expand Up @@ -70,9 +70,11 @@ public function load($filename)

public function write($filename, $data)
{
$path = $this->getFilePath($filename);
$tempFile = tempnam($this->basePath, 'cache_tmp_');
file_put_contents($tempFile, $data);

file_put_contents($path, $data);
$path = $this->getFilePath($filename);
rename($tempFile, $path);

return $path;
}
Expand Down
4 changes: 1 addition & 3 deletions src/Fonts/FontCache.php
Expand Up @@ -53,9 +53,7 @@ public function write($filename, $data)

public function binaryWrite($filename, $data)
{
$handle = fopen($this->tempFilename($filename), 'wb');
fwrite($handle, $data);
fclose($handle);
return $this->cache->write($filename, $data);
}

public function jsonWrite($filename, $data)
Expand Down

0 comments on commit 10bc24b

Please sign in to comment.