Skip to content

Commit

Permalink
Compat: Optimise game count fetching
Browse files Browse the repository at this point in the history
Implement write to and read from cache as these values are static and we 
know exactly when they change, as opposed to running 2 SQL count queries 
on every page load
  • Loading branch information
AniLeo committed Dec 21, 2023
1 parent 05d0df2 commit 62e5751
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 43 deletions.
46 changes: 5 additions & 41 deletions cachers.php
Original file line number Diff line number Diff line change
Expand Up @@ -666,49 +666,13 @@ function cacheStatusModules() : void
}


function cacheStatusCount() : void
function cacheGameCount() : void
{
$db = getDatabase();

$a_cache = array();

// Fetch general count per status
$q_status = mysqli_query($db, "SELECT `status`+0 AS `sid`, count(*) AS `c`
FROM `game_list`
WHERE `network` = 0
OR (`network` = 1 && `status` <= 2)
GROUP BY `status`;");

mysqli_close($db);

if (is_bool($q_status))
return;

$a_cache[0][0] = 0;

while ($row = mysqli_fetch_object($q_status))
{
// This should be unreachable unless the database structure is damaged
if (!property_exists($row, "sid") ||
!property_exists($row, "c"))
{
return;
}

$a_cache[0][$row->sid] = (int) $row->c;
$a_cache[0][0] += (int) $row->c;
}

$a_cache[1] = $a_cache[0];

$f_count = fopen(__DIR__.'/cache/a_count.json', 'w');
$json_data = json_encode($a_cache);

if (!$f_count || !$json_data)
return;
// count_game_entry_all
file_put_contents(__DIR__.'/cache/count_game_entry_all.txt', (string) count_game_entry_all(true));

fwrite($f_count, $json_data);
fclose($f_count);
// count_game_id_all
file_put_contents(__DIR__.'/cache/count_game_id_all.txt', (string) count_game_id_all(true));
}


Expand Down
4 changes: 4 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@
'title' => "Update Status Module",
'success' => "Forced update on status modules"
),
'cacheGameCount' => array(
'title' => "Update Game Count",
'success' => "Forced update on game count"
),
'cacheWikiIDs' => array(
'title' => "Update Wiki IDs Cache",
'success' => "Forced update on Wiki IDs cache"
Expand Down
24 changes: 22 additions & 2 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,18 @@ function countGames(mysqli $db, string $query = "") : array
}


function count_game_entry_all() : int
function count_game_entry_all(bool $ignore_cache = false) : int
{
// If we don't ignore cache, try to retrieve the value from cache
if (!$ignore_cache)
{
$count = file_get_contents(__DIR__.'/cache/count_game_entry_all.txt');

// Return value from cache only if available
if ($count !== false)
return (int) $count;
}

$db = getDatabase();
$ret = 0;

Expand Down Expand Up @@ -437,8 +447,18 @@ function count_game_entry_all() : int
}


function count_game_id_all() : int
function count_game_id_all(bool $ignore_cache = false) : int
{
// If we don't ignore cache, try to retrieve the value from cache
if (!$ignore_cache)
{
$count = file_get_contents(__DIR__.'/cache/count_game_id_all.txt');

// Return value from cache only if available
if ($count !== false)
return (int) $count;
}

$db = getDatabase();
$ret = 0;

Expand Down
4 changes: 4 additions & 0 deletions includes/inc.panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,8 @@ function compatibilityUpdater() : void
cacheInitials();
// Recache status modules
cacheStatusModules();
// Recache game count
cacheGameCount();
}
else
{
Expand Down Expand Up @@ -810,6 +812,8 @@ function mergeGames() : void

// Recache status modules
cacheStatusModules();
// Recache game count
cacheGameCount();

echo "<b>Games successfully merged!</b><br>";
}
Expand Down

0 comments on commit 62e5751

Please sign in to comment.