]> git.openstreetmap.org Git - nominatim.git/commitdiff
cache loaded configuration
authorSarah Hoffmann <lonvia@denofr.de>
Sun, 20 Mar 2022 10:30:03 +0000 (11:30 +0100)
committerSarah Hoffmann <lonvia@denofr.de>
Sun, 20 Mar 2022 10:30:03 +0000 (11:30 +0100)
Reading the YAML files is fairly expensive and slows down the BDD tests
significantly. Therefore cache the results from reading the file.

nominatim/config.py

index 785f4acda14d0f0c7f344f79718a8d150bc56001..13d9cd8a0d502e4b1f1b5ab58ca7e0761772cfaf 100644 (file)
@@ -18,7 +18,7 @@ from dotenv import dotenv_values
 from nominatim.errors import UsageError
 
 LOG = logging.getLogger()
-
+CONFIG_CACHE = {}
 
 def flatten_config_list(content, section=''):
     """ Flatten YAML configuration lists that contain include sections
@@ -181,14 +181,19 @@ class Configuration:
         """
         configfile = self.find_config_file(filename, config)
 
-        if configfile.suffix in ('.yaml', '.yml'):
-            return self._load_from_yaml(configfile)
+        if str(configfile) in CONFIG_CACHE:
+            return CONFIG_CACHE[str(configfile)]
 
-        if configfile.suffix == '.json':
+        if configfile.suffix in ('.yaml', '.yml'):
+            result = self._load_from_yaml(configfile)
+        elif configfile.suffix == '.json':
             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):