cache loaded configuration

Reading the YAML files is fairly expensive and slows down the BDD tests
significantly. Therefore cache the results from reading the file.
This commit is contained in:
Sarah Hoffmann
2022-03-20 11:30:03 +01:00
parent 2f266d946b
commit e65913d376

View File

@@ -18,7 +18,7 @@ from dotenv import dotenv_values
from nominatim.errors import UsageError from nominatim.errors import UsageError
LOG = logging.getLogger() LOG = logging.getLogger()
CONFIG_CACHE = {}
def flatten_config_list(content, section=''): def flatten_config_list(content, section=''):
""" Flatten YAML configuration lists that contain include sections """ Flatten YAML configuration lists that contain include sections
@@ -181,15 +181,20 @@ class Configuration:
""" """
configfile = self.find_config_file(filename, config) configfile = self.find_config_file(filename, config)
if str(configfile) in CONFIG_CACHE:
return CONFIG_CACHE[str(configfile)]
if configfile.suffix in ('.yaml', '.yml'): if configfile.suffix in ('.yaml', '.yml'):
return self._load_from_yaml(configfile) result = self._load_from_yaml(configfile)
elif configfile.suffix == '.json':
if configfile.suffix == '.json':
with configfile.open('r') as cfg: with configfile.open('r') as cfg:
return json.load(cfg) result = json.load(cfg)
else:
raise UsageError(f"Config file '{configfile}' has unknown format.") raise UsageError(f"Config file '{configfile}' has unknown format.")
CONFIG_CACHE[str(configfile)] = result
return result
def find_config_file(self, filename, config=None): def find_config_file(self, filename, config=None):
""" Resolve the location of a configuration file given a filename and """ Resolve the location of a configuration file given a filename and