Skip to content

Commit

Permalink
[media] Caching instrument names (#8055)
Browse files Browse the repository at this point in the history
This adds a test name cache in the getInstance function of the media provisioner to load each instrument only once instead of for each row.

Resolves #8033
  • Loading branch information
xlecours committed Mar 24, 2022
1 parent 173e217 commit f4168e3
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions modules/media/php/mediafileprovisioner.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ class MediaFileProvisioner extends \LORIS\Data\Provisioners\DBRowProvisioner
{
protected $lorisinstance;

/**
* Cache of instrument->getFullname returned value.
* Initializing empty keys to prevent instanciation of empty testnames.
*/
private $_instrumentnames = [
null => '',
'' => ''
];

/**
* Create a MediaFileProvisioner, which gets files for the meida
* menu table.
Expand Down Expand Up @@ -80,18 +89,17 @@ class MediaFileProvisioner extends \LORIS\Data\Provisioners\DBRowProvisioner
*/
public function getInstance($row) : \LORIS\Data\DataInstance
{
if (!is_null($row['testName'])) {
try {
$instrumentName = \NDB_BVL_Instrument::factory(
$this->lorisinstance,
$row['testName'],
)->getFullname();
$row['fullName'] = $instrumentName;
} catch (\Exception $e) {
}
}
$testname = $row['testName'] ?? '';
unset($row['testName']);

if (!isset($this->_instrumentnames[$testname])) {
$this->_instrumentnames[$testname] = \NDB_BVL_Instrument::factory(
$this->lorisinstance,
$testname,
)->getFullname();
}
$row['fullName'] = $this->_instrumentnames[$testname] ?? null;

return new MediaFile($row);
}
}

0 comments on commit f4168e3

Please # to comment.