Optimisation no 4: cache device totals for 5min

This commit is contained in:
jokob-sk
2022-08-05 15:26:28 +10:00
parent 50ccdcf0d0
commit 75c901a111

View File

@@ -478,6 +478,14 @@ function PiaToggleArpScan() {
// Query total numbers of Devices by status
//------------------------------------------------------------------------------
function getDevicesTotals() {
$resultJSON = "";
if(getCache("getDevicesTotals") != "")
{
$resultJSON = getCache("getDevicesTotals");
} else
{
global $db;
// combined query
@@ -492,8 +500,13 @@ function getDevicesTotals() {
');
$row = $result -> fetchArray (SQLITE3_NUM);
$resultJSON = json_encode (array ($row[0], $row[1], $row[2], $row[3], $row[4], $row[5]));
echo (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
}
?>