From 612661c9f056b372603cbc431d6c0e015dc788f9 Mon Sep 17 00:00:00 2001 From: Martin Ambrus Date: Wed, 15 Nov 2017 23:20:14 +0100 Subject: [PATCH] refactor: units DB values caching --- GameEngine/Database.php | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/GameEngine/Database.php b/GameEngine/Database.php index 27f6ec40..7ed5e567 100755 --- a/GameEngine/Database.php +++ b/GameEngine/Database.php @@ -184,6 +184,11 @@ class MYSQLi_DB implements IDbConnection { */ $coordsCache = [], + /** + * @var array Cache of units in a village. + */ + $unitsCache = [], + /** * @var array Cache of messages to be sent out to players, * so we can collect them and send them out together @@ -4588,24 +4593,30 @@ class MYSQLi_DB implements IDbConnection { return mysqli_query($this->dblink,$q); } - function getUnit($vid) { + function getUnit($vid, $use_cache = true) { list($vid) = $this->escape_input((int) $vid); + // first of all, check if we should be using cache and whether the field + // required is already cached + if ($use_cache && ($cachedValue = self::returnCachedContent(self::$unitsCache, $vid)) && !is_null($cachedValue)) { + return $cachedValue; + } + $q = "SELECT * from " . TB_PREFIX . "units where vref = $vid"; $result = mysqli_query($this->dblink,$q); if (!empty($result)) { - return mysqli_fetch_assoc($result); + self::$unitsCache[$vid] = mysqli_fetch_assoc($result); } else { - return NULL; + self::$unitsCache[$vid] = null; } + + return self::$unitsCache[$vid]; } - function getUnitsNumber($vid) { + function getUnitsNumber($vid, $use_cache = false) { list($vid) = $this->escape_input((int) $vid); - $q = "SELECT * from " . TB_PREFIX . "units where vref = $vid"; - $result = mysqli_query($this->dblink,$q); - $dbarray = mysqli_fetch_assoc($result); + $dbarray = $this->getUnit($vid); $totalunits = 0; $movingunits = $this->getVillageMovement($vid); for($i=1;$i<=50;$i++){