Implement caching for config file parsing to improve performance

This commit is contained in:
crazywhalecc
2026-04-07 17:10:33 +08:00
parent 8e91e02806
commit baa21d6e94
5 changed files with 111 additions and 14 deletions

View File

@@ -50,12 +50,20 @@ class PackageConfig
if ($content === false) {
throw new WrongUsageException("Failed to read package config file: {$file}");
}
// judge extension
$data = match (pathinfo($file, PATHINFO_EXTENSION)) {
'json' => json_decode($content, true),
'yml', 'yaml' => Yaml::parse($content),
default => throw new WrongUsageException("Unsupported package config file format: {$file}"),
};
// judge extension — use cache to skip redundant parsing
$data = ConfigCache::get($content);
if ($data !== null) {
logger()->debug("Config cache hit: {$file}");
} else {
$data = match (pathinfo($file, PATHINFO_EXTENSION)) {
'json' => json_decode($content, true),
'yml', 'yaml' => extension_loaded('yaml') ? yaml_parse($content) : Yaml::parse($content),
default => throw new WrongUsageException("Unsupported package config file format: {$file}"),
};
if (is_array($data)) {
ConfigCache::set($content, $data);
}
}
ConfigValidator::validateAndLintPackages(basename($file), $data);
foreach ($data as $pkg_name => $config) {
self::$package_configs[$pkg_name] = $config;