Getting rid of config_update_cache, moving it into ConfigManager

This commit is contained in:
Pavel Shevaev 2025-04-17 17:39:56 +03:00
parent fc0aa1636e
commit 0c7f8f3d06
2 changed files with 51 additions and 36 deletions

View File

@ -332,6 +332,7 @@ class ConfigCacheFileMap
class ConfigCacheUpdateResult class ConfigCacheUpdateResult
{ {
public ConfigDirFiles $affected_files; public ConfigDirFiles $affected_files;
public array $affected_entries = array();
/** @var array<string, string> */ /** @var array<string, string> */
public array $errors = array(); public array $errors = array();
@ -354,6 +355,8 @@ class ConfigCacheUpdateParams
public bool $verbose = false; public bool $verbose = false;
public ?int $max_workers = null; public ?int $max_workers = null;
public bool $check_junk = true; public bool $check_junk = true;
public int $max_errors_num = 15;
public bool $return_affected_entries = false;
function __construct(ConfigGlobals $globals, ConfigDirFiles $affected_files, function __construct(ConfigGlobals $globals, ConfigDirFiles $affected_files,
bool $verbose = false, ?int $max_workers = null) bool $verbose = false, ?int $max_workers = null)
@ -422,34 +425,6 @@ function _config_cache_update(ConfigCacheUpdateParams $params) : ConfigCacheUpda
return $result; 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 function _config_merge_update_results(ConfigCacheUpdateParams $params, array $results_by_job) : ConfigCacheUpdateResult
{ {
$result = new ConfigCacheUpdateResult($params); $result = new ConfigCacheUpdateResult($params);

View File

@ -117,16 +117,17 @@ class ConfigManager
{ {
if($this->file_map === null) if($this->file_map === null)
{ {
$map = $this->_tryLoadMap(); $this->file_map = $this->_tryLoadMap();
if($map === null) if($this->file_map === null)
$map = self::_makeMap($files ?? $this->scanFiles(extension: '.js')); {
$this->file_map = $map; $this->file_map = self::_makeMap($files ?? $this->scanFiles(extension: '.js'));
$this->_saveFileMap(); $this->_saveFileMap();
} }
}
return $this->file_map; 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}'..."); config_log("Updating cache, mode '{$req->mode->name}'...");
@ -150,15 +151,52 @@ class ConfigManager
verbose: $verbose, verbose: $verbose,
max_workers: $this->workers_num max_workers: $this->workers_num
); );
$update_result = config_cache_update($update_params); $update_result = self::_updateCache($update_params);
$this->cache->clear(); $this->cache->clear();
$this->_updateFileMap($req, $affected_files, $added_files, $removed_files); $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; 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) private function _checkFileMap(ConfigUpdateRequest $req, array &$added_files, array &$removed_files)
{ {
$fs_cache_map = $this->getFileMap(); $fs_cache_map = $this->getFileMap();
@ -293,7 +331,9 @@ class ConfigManager
private function _saveFileMap() 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);
} }
} }