Fix researching cache population (#358)

This commit is contained in:
Fabian
2026-07-31 06:11:35 +01:00
committed by GitHub
parent 6726f9c6e4
commit d8dfed5e33
2 changed files with 72 additions and 2 deletions
+2 -2
View File
@@ -354,8 +354,8 @@ trait DatabaseTroopQueries {
$q = "SELECT * FROM " . TB_PREFIX . "research where vref = $vid ORDER BY timestamp ASC";
$result = mysqli_query($this->dblink,$q);
$researchingCache[$vid] = $this->mysqli_fetch_all($result);
return $researchingCache[$vid];
self::$researchingCache[$vid] = $this->mysqli_fetch_all($result);
return self::$researchingCache[$vid];
}
function checkIfResearched($vref, $unit, $use_cache = true) {
@@ -0,0 +1,70 @@
<?php
if (function_exists('mysqli_query')) {
$command = escapeshellarg(PHP_BINARY)
. ' -d disable_functions=mysqli_query '
. escapeshellarg(__FILE__);
passthru($command, $status);
exit($status);
}
if (!function_exists('mysqli_query')) {
function mysqli_query($connection, $query)
{
$GLOBALS['queryCount']++;
return $query;
}
}
define('TB_PREFIX', 'test_');
require_once __DIR__ . '/../../GameEngine/Database/DatabaseTroopQueries.php';
final class ResearchingCacheHarness
{
use DatabaseTroopQueries;
public $dblink;
public $rows = [];
private static $researchingCache = [];
private static function returnCachedContent(&$cache, $key)
{
return isset($cache[$key]) && !empty($cache[$key])
? $cache[$key]
: null;
}
public function mysqli_fetch_all($result)
{
return $this->rows;
}
}
$GLOBALS['queryCount'] = 0;
$database = new ResearchingCacheHarness();
$database->rows = [
['id' => 1, 'vref' => 42, 'tech' => 't2', 'timestamp' => 1234],
];
$first = $database->getResearching(42);
$second = $database->getResearching(42);
if ($first !== $database->rows || $second !== $database->rows) {
fwrite(STDERR, "getResearching() did not return the fetched research rows.\n");
exit(1);
}
if ($GLOBALS['queryCount'] !== 1) {
fwrite(
STDERR,
"Expected one query across two cached reads; got {$GLOBALS['queryCount']}.\n"
);
exit(1);
}
fwrite(STDOUT, "getResearching cache regression test passed.\n");