Skip to content

Commit

Permalink
Add two more static variables for caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
srobotta committed Nov 26, 2024
1 parent 1e17983 commit 14d549c
Showing 1 changed file with 46 additions and 35 deletions.
81 changes: 46 additions & 35 deletions classes/repository/instance_repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,25 +216,30 @@ private static function get_strings(string $type, int $subratingid, bool $throwo
private static function get_criteria_by_category_for_instance_id(int $id): array {
global $DB;

$crittab = tables::INSTANCE_CRITERION_TABLE;
$cattab = tables::INSTANCE_CATEGORY_TABLE;

$sql = "SELECT crit.*
FROM {{$crittab}} crit
JOIN {{$cattab}} cat
ON crit.categoryid = cat.id
WHERE cat.instanceid = ?";

$bycat = [];
$rs = $DB->get_recordset_sql($sql, [$id]);
foreach ($rs as $dbocriterion) {
if (!isset($bycat[$dbocriterion->categoryid])) {
$bycat[$dbocriterion->categoryid] = [];
static $bycats = [];

if (!array_key_exists($id, $bycats) || PHPUNIT_TEST) {
$crittab = tables::INSTANCE_CRITERION_TABLE;
$cattab = tables::INSTANCE_CATEGORY_TABLE;

$sql = "SELECT crit.*
FROM {{$crittab}} crit
JOIN {{$cattab}} cat
ON crit.categoryid = cat.id
WHERE cat.instanceid = ?";

$bycat = [];
$rs = $DB->get_recordset_sql($sql, [$id]);
foreach ($rs as $dbocriterion) {
if (!isset($bycat[$dbocriterion->categoryid])) {
$bycat[$dbocriterion->categoryid] = [];
}
$bycat[$dbocriterion->categoryid][$dbocriterion->id] = db_instance_criterion::to_instance_criterion($dbocriterion);
}
$bycat[$dbocriterion->categoryid][$dbocriterion->id] = db_instance_criterion::to_instance_criterion($dbocriterion);
$rs->close();
$bycats[$id] = $bycat;
}
$rs->close();
return $bycat;
return $bycats[$id];
}

/**
Expand All @@ -245,26 +250,32 @@ private static function get_criteria_by_category_for_instance_id(int $id): array
*/
private static function get_subratings_by_criterion_for_instance(int $id): array {
global $DB;
$crittab = tables::INSTANCE_CRITERION_TABLE;
$cattab = tables::INSTANCE_CATEGORY_TABLE;
$srattab = tables::INSTANCE_SUBRATING_TABLE;
$sql = "SELECT srat.*
FROM {{$srattab}} srat
JOIN {{$crittab}} crit
ON srat.criterionid = crit.id
JOIN {{$cattab}} mvic
ON crit.categoryid = mvic.id
WHERE mvic.instanceid = ?";
$rs = $DB->get_recordset_sql($sql, [$id]);
$bycrit = [];
foreach ($rs as $dbosubrating) {
if (!isset($bycrit[$dbosubrating->criterionid])) {
$bycrit[$dbosubrating->criterionid] = [];

static $bycrits = [];

if (!array_key_exists($id, $bycrits) || PHPUNIT_TEST) {
$crittab = tables::INSTANCE_CRITERION_TABLE;
$cattab = tables::INSTANCE_CATEGORY_TABLE;
$srattab = tables::INSTANCE_SUBRATING_TABLE;
$sql = "SELECT srat.*
FROM {{$srattab}} srat
JOIN {{$crittab}} crit
ON srat.criterionid = crit.id
JOIN {{$cattab}} mvic
ON crit.categoryid = mvic.id
WHERE mvic.instanceid = ?";
$rs = $DB->get_recordset_sql($sql, [$id]);
$bycrit = [];
foreach ($rs as $dbosubrating) {
if (!isset($bycrit[$dbosubrating->criterionid])) {
$bycrit[$dbosubrating->criterionid] = [];
}
$bycrit[$dbosubrating->criterionid][$dbosubrating->id] = db_instance_subrating::to_subrating($dbosubrating);
}
$bycrit[$dbosubrating->criterionid][$dbosubrating->id] = db_instance_subrating::to_subrating($dbosubrating);
$rs->close();
$bycrits[$id] = $bycrit;
}
$rs->close();
return $bycrit;
return $bycrits[$id];
}

/**
Expand Down

0 comments on commit 14d549c

Please sign in to comment.