From 0c7f8f3d062fb88463df124e20de66091a2078c3 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Thu, 17 Apr 2025 17:39:56 +0300 Subject: [PATCH] Getting rid of config_update_cache, moving it into ConfigManager --- cache.inc.php | 31 +++------------------------- config.inc.php | 56 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 36 deletions(-) diff --git a/cache.inc.php b/cache.inc.php index a97f93f..8b68317 100644 --- a/cache.inc.php +++ b/cache.inc.php @@ -332,6 +332,7 @@ class ConfigCacheFileMap class ConfigCacheUpdateResult { public ConfigDirFiles $affected_files; + public array $affected_entries = array(); /** @var array */ public array $errors = array(); @@ -354,6 +355,8 @@ class ConfigCacheUpdateParams public bool $verbose = false; public ?int $max_workers = null; public bool $check_junk = true; + public int $max_errors_num = 15; + public bool $return_affected_entries = false; function __construct(ConfigGlobals $globals, ConfigDirFiles $affected_files, bool $verbose = false, ?int $max_workers = null) @@ -422,34 +425,6 @@ function _config_cache_update(ConfigCacheUpdateParams $params) : ConfigCacheUpda return $result; } -function config_cache_update(ConfigCacheUpdateParams $params) : ConfigCacheUpdateResult -{ - if($params->affected_files->isEmpty()) - return new ConfigCacheUpdateResult($params); - - $t = microtime(true); - - $result = _config_cache_update($params); - - if($result->errors) - { - $errors = array(); - foreach($result->errors as $file => $error) - { - $errors[] = (count($errors) + 1) . ") Error in file '$file': $error"; - if(count($errors) > 15) - break; - } - - throw new Exception(implode("\n", $errors)); - } - - if($params->verbose) - config_log("Update cache: " . round(microtime(true) - $t,2) . " sec."); - - return $result; -} - function _config_merge_update_results(ConfigCacheUpdateParams $params, array $results_by_job) : ConfigCacheUpdateResult { $result = new ConfigCacheUpdateResult($params); diff --git a/config.inc.php b/config.inc.php index 68e314f..3880f8c 100644 --- a/config.inc.php +++ b/config.inc.php @@ -117,16 +117,17 @@ class ConfigManager { if($this->file_map === null) { - $map = $this->_tryLoadMap(); - if($map === null) - $map = self::_makeMap($files ?? $this->scanFiles(extension: '.js')); - $this->file_map = $map; - $this->_saveFileMap(); + $this->file_map = $this->_tryLoadMap(); + if($this->file_map === null) + { + $this->file_map = self::_makeMap($files ?? $this->scanFiles(extension: '.js')); + $this->_saveFileMap(); + } } return $this->file_map; } - function updateCache(ConfigUpdateRequest $req, bool $verbose = false) : ConfigCacheUpdateResult + function updateCache(ConfigUpdateRequest $req, bool $return_entries = false, bool $verbose = false) : ConfigCacheUpdateResult { config_log("Updating cache, mode '{$req->mode->name}'..."); @@ -150,15 +151,52 @@ class ConfigManager verbose: $verbose, max_workers: $this->workers_num ); - $update_result = config_cache_update($update_params); + $update_result = self::_updateCache($update_params); $this->cache->clear(); $this->_updateFileMap($req, $affected_files, $added_files, $removed_files); + if($return_entries) + { + foreach($affected_files as $file) + { + $entry = $this->cache->getOrLoadByPath($file); + $update_result->affected_entries[] = $entry; + } + } + return $update_result; } + private function _updateCache(ConfigCacheUpdateParams $params) : ConfigCacheUpdateResult + { + if($params->affected_files->isEmpty()) + return new ConfigCacheUpdateResult($params); + + $t = microtime(true); + + $result = _config_cache_update($params); + + if($result->errors) + { + $errors = array(); + foreach($result->errors as $file => $error) + { + $errors[] = (count($errors) + 1) . ") Error in file '$file': $error"; + if(count($errors) > $params->max_errors_num) + break; + } + + throw new Exception(implode("\n", $errors)); + } + + if($params->verbose) + config_log("Update cache: " . round(microtime(true) - $t,2) . " sec."); + + return $result; + } + private function _checkFileMap(ConfigUpdateRequest $req, array &$added_files, array &$removed_files) { $fs_cache_map = $this->getFileMap(); @@ -293,7 +331,9 @@ class ConfigManager private function _saveFileMap() { - ensure_write($this->_getMapPath(), ConfigCacheFileMap::serialize($this->getFileMap())); + $data = ConfigCacheFileMap::serialize($this->getFileMap()); + config_log("Saving file map: " . kb($data)); + ensure_write($this->_getMapPath(), $data); } }