Optimisation no 4: cache device totals for 5min
This commit is contained in:
@@ -478,22 +478,35 @@ function PiaToggleArpScan() {
|
|||||||
// Query total numbers of Devices by status
|
// Query total numbers of Devices by status
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
function getDevicesTotals() {
|
function getDevicesTotals() {
|
||||||
global $db;
|
|
||||||
|
|
||||||
// combined query
|
$resultJSON = "";
|
||||||
$result = $db->query(
|
|
||||||
'SELECT
|
|
||||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('all').') as devices,
|
|
||||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('connected').') as connected,
|
|
||||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('favorites').') as favorites,
|
|
||||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('new').') as new,
|
|
||||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('down').') as down,
|
|
||||||
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('archived').') as archived
|
|
||||||
');
|
|
||||||
|
|
||||||
$row = $result -> fetchArray (SQLITE3_NUM);
|
if(getCache("getDevicesTotals") != "")
|
||||||
|
{
|
||||||
|
$resultJSON = getCache("getDevicesTotals");
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
global $db;
|
||||||
|
|
||||||
echo (json_encode (array ($row[0], $row[1], $row[2], $row[3], $row[4], $row[5])));
|
// combined query
|
||||||
|
$result = $db->query(
|
||||||
|
'SELECT
|
||||||
|
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('all').') as devices,
|
||||||
|
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('connected').') as connected,
|
||||||
|
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('favorites').') as favorites,
|
||||||
|
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('new').') as new,
|
||||||
|
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('down').') as down,
|
||||||
|
(SELECT COUNT(*) FROM Devices '. getDeviceCondition ('archived').') as archived
|
||||||
|
');
|
||||||
|
|
||||||
|
$row = $result -> fetchArray (SQLITE3_NUM);
|
||||||
|
$resultJSON = json_encode (array ($row[0], $row[1], $row[2], $row[3], $row[4], $row[5]));
|
||||||
|
|
||||||
|
// save to cache
|
||||||
|
setCache("getDevicesTotals", $resultJSON );
|
||||||
|
}
|
||||||
|
|
||||||
|
echo ($resultJSON);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -798,4 +811,21 @@ function getDeviceCondition ($deviceStatus) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
// Simple cookie cache
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
function getCache($key) {
|
||||||
|
if( isset($_COOKIE[$key]))
|
||||||
|
{
|
||||||
|
return $_COOKIE[$key];
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function setCache($key, $value) {
|
||||||
|
setcookie($key, $value, time()+300, "/","", 0); // 5min cache
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
Reference in New Issue
Block a user