Skip to content

Commit 209292a

Browse files
committed
Cache foreign key display values
Avoid repeated sql queries for foreighn keys, speed things up when duplicate key values are in the result set to display. Signed-off-by: Maximilian Krög <maxi_kroeg@web.de>
1 parent 4c90722 commit 209292a

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

libraries/classes/Display/Results.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use function __;
3636
use function _pgettext;
3737
use function array_filter;
38+
use function array_key_exists;
3839
use function array_keys;
3940
use function array_merge;
4041
use function array_shift;
@@ -249,6 +250,9 @@ class Results
249250
/** @var Template */
250251
public $template;
251252

253+
/** @var array<string, string|null> */
254+
private $foreignKeyDisplayCache = [];
255+
252256
/**
253257
* @param string $db the database name
254258
* @param string $table the table name
@@ -4363,22 +4367,25 @@ private function handleNonPrintableContents(
43634367
*/
43644368
private function getFromForeign(array $fieldInfo, string $whereComparison): ?string
43654369
{
4370+
$key = json_encode([$fieldInfo[0], $fieldInfo[1], $fieldInfo[2], $fieldInfo[3], $whereComparison]);
4371+
if (array_key_exists($key, $this->foreignKeyDisplayCache)) {
4372+
return $this->foreignKeyDisplayCache[$key];
4373+
}
4374+
43664375
$dispsql = 'SELECT ' . Util::backquote($fieldInfo[2])
43674376
. ' FROM ' . Util::backquote($fieldInfo[3]) . '.' . Util::backquote($fieldInfo[0])
43684377
. ' WHERE ' . Util::backquote($fieldInfo[1]) . $whereComparison
43694378
. ' LIMIT 1';
43704379

43714380
$dispval = $this->dbi->fetchValue($dispsql);
43724381
if ($dispval === false) {
4373-
return __('Link not found!');
4374-
}
4375-
4376-
if ($dispval === null) {
4377-
return null;
4382+
$dispval = __('Link not found!');
4383+
} elseif ($dispval !== null) {
4384+
// Truncate values that are too long, see: #17902
4385+
[, $dispval] = $this->getPartialText($dispval);
43784386
}
43794387

4380-
// Truncate values that are too long, see: #17902
4381-
[, $dispval] = $this->getPartialText($dispval);
4388+
$this->foreignKeyDisplayCache[$key] = $dispval;
43824389

43834390
return $dispval;
43844391
}

0 commit comments

Comments
 (0)