From f4168e3b3455540d219819d844015789374b73f1 Mon Sep 17 00:00:00 2001 From: Xavier Lecours Date: Thu, 24 Mar 2022 11:28:51 -0400 Subject: [PATCH] [media] Caching instrument names (#8055) 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 --- .../media/php/mediafileprovisioner.class.inc | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/modules/media/php/mediafileprovisioner.class.inc b/modules/media/php/mediafileprovisioner.class.inc index 012cdfd313a..8d161dc5a5e 100644 --- a/modules/media/php/mediafileprovisioner.class.inc +++ b/modules/media/php/mediafileprovisioner.class.inc @@ -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. @@ -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); } }