From df8b0a1fc599d94afc0038b564650aa3a8064fee Mon Sep 17 00:00:00 2001 From: Brown Date: Thu, 1 Nov 2018 17:42:48 -0400 Subject: [PATCH] =?UTF-8?q?Bust=20cache=20when=20config=20changes,=20don?= =?UTF-8?q?=E2=80=99t=20rely=20on=20>=20modified=20time?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Psalm/Config.php | 6 +-- .../ClassLikeStorageCacheProvider.php | 2 +- .../Provider/FileReferenceCacheProvider.php | 49 ++++++++++++++++++- .../Provider/FileStorageCacheProvider.php | 2 +- 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index ff69e1b24..f2ec6d94a 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -297,9 +297,9 @@ class Config public $exit_functions = []; /** - * @var int + * @var string */ - public $modified_time = 0; + public $hash = ''; /** @var string|null */ public $error_baseline = null; @@ -388,7 +388,7 @@ class Config try { $config = self::loadFromXML($base_dir, $file_contents); - $config->modified_time = filemtime($file_path); + $config->hash = sha1($file_contents); } catch (ConfigException $e) { throw new ConfigException( 'Problem parsing ' . $file_path . ":\n" . ' ' . $e->getMessage() diff --git a/src/Psalm/Provider/ClassLikeStorageCacheProvider.php b/src/Psalm/Provider/ClassLikeStorageCacheProvider.php index 88cd5a4ed..acf8ebb1f 100644 --- a/src/Psalm/Provider/ClassLikeStorageCacheProvider.php +++ b/src/Psalm/Provider/ClassLikeStorageCacheProvider.php @@ -43,7 +43,7 @@ class ClassLikeStorageCacheProvider $this->modified_timestamps .= ' ' . filemtime($dependent_file_path); } - $this->modified_timestamps .= PSALM_VERSION . $this->config->modified_time; + $this->modified_timestamps .= PSALM_VERSION . $this->config->hash; } /** diff --git a/src/Psalm/Provider/FileReferenceCacheProvider.php b/src/Psalm/Provider/FileReferenceCacheProvider.php index 1f06f321e..b9f19d8e1 100644 --- a/src/Psalm/Provider/FileReferenceCacheProvider.php +++ b/src/Psalm/Provider/FileReferenceCacheProvider.php @@ -34,15 +34,23 @@ class FileReferenceCacheProvider const CLASS_METHOD_CACHE_NAME = 'class_method_references'; const ISSUES_CACHE_NAME = 'issues'; const FILE_MAPS_CACHE_NAME = 'file_maps'; + const CONFIG_HASH_CACHE_NAME = 'config'; /** * @var Config */ private $config; + /** + * @var bool + */ + public $config_changed; + public function __construct(Config $config) { $this->config = $config; + $this->config_changed = $config->hash !== $this->getConfigHashCache(); + $this->setConfigHashCache($config->hash); } /** @@ -191,7 +199,7 @@ class FileReferenceCacheProvider if ($cache_directory && file_exists($correct_methods_cache_location) - && filemtime($correct_methods_cache_location) > $this->config->modified_time + && !$this->config_changed ) { /** @var array> */ return unserialize(file_get_contents($correct_methods_cache_location)); @@ -229,7 +237,7 @@ class FileReferenceCacheProvider if ($cache_directory && file_exists($file_maps_cache_location) - && filemtime($file_maps_cache_location) > $this->config->modified_time + && !$this->config_changed ) { /** @var array */ $file_maps_cache = unserialize(file_get_contents($file_maps_cache_location)); @@ -256,4 +264,41 @@ class FileReferenceCacheProvider ); } } + + /** + * @return string|false + */ + public function getConfigHashCache() + { + $cache_directory = $this->config->getCacheDirectory(); + + $config_hash_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::CONFIG_HASH_CACHE_NAME; + + if ($cache_directory + && file_exists($config_hash_cache_location) + ) { + /** @var string */ + $file_maps_cache = unserialize(file_get_contents($config_hash_cache_location)); + return $file_maps_cache; + } + + return false; + } + + /** + * @return void + */ + public function setConfigHashCache(string $hash) + { + $cache_directory = Config::getInstance()->getCacheDirectory(); + + if ($cache_directory) { + $config_hash_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::CONFIG_HASH_CACHE_NAME; + + file_put_contents( + $config_hash_cache_location, + serialize($hash) + ); + } + } } diff --git a/src/Psalm/Provider/FileStorageCacheProvider.php b/src/Psalm/Provider/FileStorageCacheProvider.php index 50b69d469..0bca2e0b9 100644 --- a/src/Psalm/Provider/FileStorageCacheProvider.php +++ b/src/Psalm/Provider/FileStorageCacheProvider.php @@ -44,7 +44,7 @@ class FileStorageCacheProvider $this->modified_timestamps .= ' ' . filemtime($dependent_file_path); } - $this->modified_timestamps .= PSALM_VERSION . $this->config->modified_time; + $this->modified_timestamps .= PSALM_VERSION . $this->config->hash; } /**