Skip to content

Commit

Permalink
[Settings] Cache settings after first retrieval (#9216)
Browse files Browse the repository at this point in the history
There are a number of settings that we use frequently in LORIS such as "base" to get the base path. When accessed multiple times in a request at different places in LORIS, each one currently requires a query against the database.

This caches any settings that are used in NDB_Config so that future calls to getSetting() can return the cached value and not make another round trip to the DB.
  • Loading branch information
driusan committed Apr 25, 2024
1 parent bff9931 commit 44d6da5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
21 changes: 20 additions & 1 deletion php/libraries/NDB_Config.class.inc
Expand Up @@ -387,6 +387,18 @@ class NDB_Config
throw new ConfigurationException("No setting $name in config.xml");
}

protected $settingCache = [];

/**
* Clears the settings cache.
*
* @return void
*/
public function clearCache()
{
$this->settingCache = [];
}

/**
* Gets a setting by name
*
Expand All @@ -396,10 +408,14 @@ class NDB_Config
*/
function getSetting(string $name)
{
if (isset($this->settingCache[$name])) {
return $this->settingCache[$name];
}
try {
$XMLValue = $this->getSettingFromXML($name);

if ($XMLValue !== null) {
$this->settingCache[$name] = $XMLValue;
return $XMLValue;
}
} catch (ConfigurationException $e) {
Expand All @@ -411,7 +427,10 @@ class NDB_Config
// nothing in the config file, so get the value from the DB
// This will throw a ConfigurationException if it does not
// exist.
return $this->getSettingFromDB($name);
$DBVal = $this->getSettingFromDB($name);
$this->settingCache[$name] = $DBVal;
return $DBVal;

}

/**
Expand Down
1 change: 1 addition & 0 deletions test/unittests/NDB_ConfigTest.php
Expand Up @@ -96,6 +96,7 @@ protected function setUp(): void
{
parent::setUp();
$this->_config = FakeConfig::singleton();
$this->_config->clearCache();

$configMock = $this->getMockBuilder('NDB_Config')->getMock();
$dbMock = $this->getMockBuilder('Database')->getMock();
Expand Down

0 comments on commit 44d6da5

Please sign in to comment.