Skip to content

Commit

Permalink
always use lock when writing/reading cache data to/from file
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Aug 11, 2022
1 parent 1ef3851 commit 8ca594a
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 119 deletions.
11 changes: 6 additions & 5 deletions src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php
Expand Up @@ -3,13 +3,13 @@
namespace Psalm\Internal\Provider;

use Psalm\Config;
use Psalm\Internal\Provider\Providers;
use Psalm\Storage\ClassLikeStorage;
use UnexpectedValueException;

use function array_merge;
use function dirname;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function filemtime;
use function get_class;
Expand All @@ -25,6 +25,7 @@
use function unserialize;

use const DIRECTORY_SEPARATOR;
use const LOCK_EX;
use const PHP_VERSION_ID;

/**
Expand Down Expand Up @@ -86,9 +87,9 @@ public function writeToCache(ClassLikeStorage $storage, string $file_path, strin

$cache_location = $this->getCacheLocationForClass($fq_classlike_name_lc, $file_path, true);
if ($this->config->use_igbinary) {
file_put_contents($cache_location, igbinary_serialize($storage));
file_put_contents($cache_location, igbinary_serialize($storage), LOCK_EX);
} else {
file_put_contents($cache_location, serialize($storage));
file_put_contents($cache_location, serialize($storage), LOCK_EX);
}
}

Expand Down Expand Up @@ -132,7 +133,7 @@ private function loadFromCache(string $fq_classlike_name_lc, ?string $file_path)

if (file_exists($cache_location)) {
if ($this->config->use_igbinary) {
$storage = igbinary_unserialize((string)file_get_contents($cache_location));
$storage = igbinary_unserialize(Providers::safeFileGetContents($cache_location));

if ($storage instanceof ClassLikeStorage) {
return $storage;
Expand All @@ -141,7 +142,7 @@ private function loadFromCache(string $fq_classlike_name_lc, ?string $file_path)
return null;
}

$storage = unserialize((string)file_get_contents($cache_location));
$storage = unserialize(Providers::safeFileGetContents($cache_location));

if ($storage instanceof ClassLikeStorage) {
return $storage;
Expand Down

0 comments on commit 8ca594a

Please sign in to comment.