refactor: s1_artefact table SELECTs optimized and cached

#313
This commit is contained in:
Martin Ambrus
2017-11-25 18:22:13 +01:00
parent ec234579cc
commit f656f631ea
+73 -26
View File
@@ -6846,20 +6846,22 @@ References: User ID/Message ID, Mode
// no need to cache this method // no need to cache this method
function getOwnArtefactInfo($vref) { function getOwnArtefactInfo($vref) {
list($vref) = $this->escape_input((int) $vref); // load the data - type is irrelevant, since the method caches all data
// then returns the one for our type
$this->getOwnArtefactInfoByType($vref, 1);
$q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE vref = $vref LIMIT 1"; // return what we've cached
$result = mysqli_query($this->dblink,$q); return (self::$artefactInfoByTypeCache[$vref]);
return mysqli_fetch_array($result);
} }
// no need to cache this method // no need to cache this method
function getOwnArtefactInfo3($uid) { function getOwnArtefactInfo3($uid) {
list($uid) = $this->escape_input((int) $uid); // load the data - type and size are irrelevant, since the method caches all data
// then returns the one for our type and size
$this->getOwnUniqueArtefactInfo($uid, 1, 1);
$q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE owner = $uid"; // return what we've cached
$result = mysqli_query($this->dblink,$q); return (self::$artefactInfoSimpleCache[$uid]);
return $this->mysqli_fetch_all($result);
} }
function getOwnArtefactInfoByType($vref, $type, $use_cache = true) { function getOwnArtefactInfoByType($vref, $type, $use_cache = true) {
@@ -6868,15 +6870,32 @@ References: User ID/Message ID, Mode
// 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::$artefactInfoByTypeCache, $vref.$type)) && !is_null($cachedValue)) { if ($use_cache && isset(self::$artefactInfoByTypeCache[$vref]) && is_array(self::$artefactInfoByTypeCache[$vref]) && !count(self::$artefactInfoByTypeCache[$vref])) {
return $cachedValue; return [];
} else if ($use_cache && ($cachedValue = self::returnCachedContent(self::$artefactInfoByTypeCache, $vref)) && !is_null($cachedValue)) {
return (isset($cachedValue[$type]) ? $cachedValue[$type] : []);
} }
$q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE vref = $vref AND type = $type order by size LIMIT 1"; $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE vref = $vref ORDER BY size";
$result = mysqli_query($this->dblink,$q); $result = $this->mysqli_fetch_all(mysqli_query($this->dblink,$q));
self::$artefactInfoByTypeCache[$vref.$type] = mysqli_fetch_array($result); // cache all types and return the requested one
return self::$artefactInfoByTypeCache[$vref.$type]; if (count($result)) {
foreach ($result as $arteInfo) {
if (!isset(self::$artefactInfoByTypeCache[$arteInfo['vref']])) {
self::$artefactInfoByTypeCache[$arteInfo['vref']] = [];
}
// we're sorting by size, thus we only need the first one per each type
if (isset(self::$artefactInfoByTypeCache[$arteInfo['vref']]) && !isset(self::$artefactInfoByTypeCache[$arteInfo['vref']][$arteInfo['type']])) {
self::$artefactInfoByTypeCache[$arteInfo['vref']][$arteInfo['type']] = $arteInfo;
}
}
} else {
self::$artefactInfoByTypeCache[$vref] = [];
}
return (isset(self::$artefactInfoByTypeCache[$vref][$type]) ? self::$artefactInfoByTypeCache[$vref][$type] : []);
} }
function getOwnArtefactInfoByType2($vref, $type, $use_cache = true) { function getOwnArtefactInfoByType2($vref, $type, $use_cache = true) {
@@ -6888,15 +6907,29 @@ References: User ID/Message ID, Mode
// 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::$artefactInfoSimpleCache, $id.$type.$size)) && !is_null($cachedValue)) { if ($use_cache && isset(self::$artefactInfoSimpleCache[$id]) && is_array(self::$artefactInfoSimpleCache[$id]) && !count(self::$artefactInfoSimpleCache[$id])) {
return $cachedValue; return [];
} else if ($use_cache && ($cachedValue = self::returnCachedContent(self::$artefactInfoSimpleCache, $id)) && !is_null($cachedValue)) {
return (isset($cachedValue[$type.$size]) ? $cachedValue[$type.$size] : []);
} }
$q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE owner = $id AND type = $type AND size=$size LIMIT 1"; $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE owner = $id";
$result = mysqli_query($this->dblink,$q); $result = $this->mysqli_fetch_all(mysqli_query($this->dblink,$q));
self::$artefactInfoSimpleCache[$id.$type.$size] = mysqli_fetch_array($result); // cache all types and return the requested one
return self::$artefactInfoSimpleCache[$id.$type.$size]; if (count($result)) {
foreach ($result as $arteInfo) {
if (!isset(self::$artefactInfoSimpleCache[$arteInfo['owner']])) {
self::$artefactInfoSimpleCache[$arteInfo['owner']] = [];
}
self::$artefactInfoSimpleCache[$arteInfo['owner']][$arteInfo['type'].$arteInfo['size']] = $arteInfo;
}
} else {
self::$artefactInfoSimpleCache[$id] = [];
}
return (isset(self::$artefactInfoSimpleCache[$id][$type.$size]) ? self::$artefactInfoSimpleCache[$id][$type.$size] : []);
} }
function getOwnUniqueArtefactInfo2($id, $type, $size, $mode, $use_cache = true) { function getOwnUniqueArtefactInfo2($id, $type, $size, $mode, $use_cache = true) {
@@ -6904,15 +6937,29 @@ References: User ID/Message ID, Mode
// 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::$artefactDataCache, $id.$type.$size.$mode)) && !is_null($cachedValue)) { if ($use_cache && isset(self::$artefactDataCache[$id.$mode]) && is_array(self::$artefactDataCache[$id.$mode]) && !count(self::$artefactDataCache[$id.$mode])) {
return $cachedValue; return [];
} else if ($use_cache && ($cachedValue = self::returnCachedContent(self::$artefactDataCache, $id.$mode)) && !is_null($cachedValue)) {
return (isset($cachedValue[$size.$type]) ? $cachedValue[$size.$type] : []);
} }
$q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE ".(!$mode ? 'owner' : 'vref')." = $id AND active = 1 AND type = $type AND size=$size"; $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE ".(!$mode ? 'owner' : 'vref')." = $id AND active = 1";
$result = mysqli_query($this->dblink,$q); $result = $this->mysqli_fetch_all(mysqli_query($this->dblink,$q));
self::$artefactDataCache[$id.$type.$size.$mode] = $this->mysqli_fetch_all($result); // cache all types and return the requested one
return self::$artefactDataCache[$id.$type.$size.$mode]; if (count($result)) {
foreach ($result as $arteInfo) {
if (!isset(self::$artefactDataCache[$arteInfo[(!$mode ? 'owner' : 'vref')] . $mode])) {
self::$artefactDataCache[$arteInfo[(!$mode ? 'owner' : 'vref')] . $mode] = [];
}
self::$artefactDataCache[$arteInfo[(!$mode ? 'owner' : 'vref')] . $mode][$arteInfo['size'].$arteInfo['type']] = $arteInfo;
}
} else {
self::$artefactDataCache[$id.$mode] = [];
}
return (isset(self::$artefactDataCache[$id.$mode][$size.$type]) ? self::$artefactDataCache[$id.$mode][$size.$type] : []);
} }
function getFoolArtefactInfo($type,$vid,$uid, $use_cache = true) { function getFoolArtefactInfo($type,$vid,$uid, $use_cache = true) {