refactor: s1_abdata & s1_tdata caching implemented

#313
This commit is contained in:
Martin Ambrus
2017-11-20 23:46:45 +01:00
parent 3792fd5ed5
commit bc2eaa1380
3 changed files with 95 additions and 27 deletions
+1
View File
@@ -1227,6 +1227,7 @@ class Automation {
$database->getEnforceVillage($vilIDs, 0); $database->getEnforceVillage($vilIDs, 0);
$database->getMovement(34, $vilIDs, 1); $database->getMovement(34, $vilIDs, 1);
$database->getPrisoners($vilIDs, 0); $database->getPrisoners($vilIDs, 0);
$database->getABTech($vilIDs);
// calculate battles // calculate battles
foreach($dataarray as $data) { foreach($dataarray as $data) {
+9
View File
@@ -308,6 +308,15 @@ class Battle {
$DefendersAll = (!is_null($defReinforcements) ? $database->getEnforceVillage($DefenderWref,0) : $defReinforcements); $DefendersAll = (!is_null($defReinforcements) ? $database->getEnforceVillage($DefenderWref,0) : $defReinforcements);
if(!empty($DefendersAll) && $DefenderWref>0){ if(!empty($DefendersAll) && $DefenderWref>0){
// preload village IDs
$vilIDs = [];
foreach($DefendersAll as $defenders) {
$vilIDs[$defenders['from']] = true;
$vilIDs[$defenders['to']] = true;
}
$vilIDs = array_keys($vilIDs);
$database->getABTech($vilIDs);
foreach($DefendersAll as $defenders) { foreach($DefendersAll as $defenders) {
for ($i=1;$i<=50;$i++) {$def_ab[$i]=0;} for ($i=1;$i<=50;$i++) {$def_ab[$i]=0;}
$fromvillage = $defenders['from']; $fromvillage = $defenders['from'];
+67 -9
View File
@@ -5733,19 +5733,68 @@ References: User ID/Message ID, Mode
} }
function getABTech($vid, $use_cache = true) { function getABTech($vid, $use_cache = true) {
list($vid) = $this->escape_input((int) $vid); $array_passed = is_array($vid);
if (!$array_passed) {
$vid = [(int) $vid];
} else {
foreach ($vid as $index => $ivdValue) {
$vid[$index] = (int) $ivdValue;
}
}
if (!count($vid)) {
return [];
}
// first of all, check if we should be using cache and whether the field // first of all, check if we should be using cache and whether the field
// required is already cached // required is already cached
if ($use_cache && ($cachedValue = self::returnCachedContent(self::$abTechCache, $vid)) && !is_null($cachedValue)) { if ($use_cache && !$array_passed && isset(self::$abTechCache[$vid[0]]) && is_array(self::$abTechCache[$vid[0]]) && !count(self::$abTechCache[$vid[0]])) {
return self::$abTechCache[$vid[0]];
} else if ($use_cache && $array_passed) {
// check what we can return from cache
$newVIDs = [];
foreach ($vid as $key) {
if (!isset(self::$abTechCache[$key])) {
$newVIDs [] = $key;
}
}
// everything's cached, just return the cache
if (!count($newVIDs)) {
return self::$abTechCache;
} else {
// update remaining IDs to select and cache
$vid = $newVIDs;
}
} else if ($use_cache && !$array_passed && ($cachedValue = self::returnCachedContent(self::$abTechCache, $vid[0])) && !is_null($cachedValue)) {
// special case when we have empty arrays cached for this cache only
return $cachedValue; return $cachedValue;
} }
$q = "SELECT * FROM " . TB_PREFIX . "abdata where vref = $vid"; $q = "SELECT * FROM " . TB_PREFIX . "abdata where vref IN(".implode(', ', $vid).")";
$result = mysqli_query($this->dblink,$q); $result = $this->mysqli_fetch_all(mysqli_query($this->dblink,$q));
self::$abTechCache[$vid] = mysqli_fetch_assoc($result); // return a single value
return self::$abTechCache[$vid]; if (!$array_passed) {
self::$abTechCache[$vid[0]] = $result;
} else {
if ($result && count($result)) {
foreach ( $result as $record ) {
self::$abTechCache[ $record['vref']] = $record;
}
}
// check for any missing IDs and fill them in with blanks,
// since no reinforcements were found for these villages
foreach ($vid as $key) {
if (!isset(self::$abTechCache[$key])) {
self::$abTechCache[$key] = [];
}
}
}
return ($array_passed ? self::$abTechCache : self::$abTechCache[$vid[0]]);
} }
function addResearch($vid, $tech, $time) { function addResearch($vid, $tech, $time) {
@@ -5781,13 +5830,22 @@ References: User ID/Message ID, Mode
return self::$isResearchedCache[$vref][$unit]; return self::$isResearchedCache[$vref][$unit];
} }
// no need to cache this method
function getTech($vid) { function getTech($vid) {
list($vid) = $this->escape_input((int) $vid); // this is a somewhat non-ideal, externally non-changeable way of caching
// but since we're only ever going to be calling this from Village constructor
// for our current village, this will more than suffice
static $cachedData = [];
$vid = (int) $vid;
if (isset($cachedData[$vid])) {
return $cachedData[$vid];
}
$q = "SELECT * from " . TB_PREFIX . "tdata where vref = $vid"; $q = "SELECT * from " . TB_PREFIX . "tdata where vref = $vid";
$result = mysqli_query($this->dblink,$q); $result = mysqli_query($this->dblink,$q);
return mysqli_fetch_assoc($result); $cachedData[$vid] = mysqli_fetch_assoc($result);
return $cachedData[$vid];
} }
// no need to cache this method // no need to cache this method