Use the new cache where needed

This commit is contained in:
eraseyourknees@gmail.com 2022-08-25 17:46:16 +02:00
parent c14287dcbd
commit 99f6e8d940
2 changed files with 20 additions and 52 deletions

View File

@ -17,6 +17,7 @@ limitations under the License.
require_once dirname(__FILE__).'/shared/main.php'; require_once dirname(__FILE__).'/shared/main.php';
require_once dirname(__FILE__).'/shared/requests.php'; require_once dirname(__FILE__).'/shared/requests.php';
require_once dirname(__FILE__).'/shared/cache.php';
require_once dirname(__FILE__).'/listid.php'; require_once dirname(__FILE__).'/listid.php';
function uupFetchUpd( function uupFetchUpd(
@ -90,23 +91,10 @@ function uupFetchUpd(
$type = 'Production'; $type = 'Production';
} }
$cacheHash = hash('sha256', strtolower("api-fetch-$arch-$ring-$flight-$build-$minor-$sku-$type")); $res = "api-fetch-$arch-$ring-$flight-$build-$minor-$sku-$type";
$cached = 0; $cache = new UupDumpCache($res);
$out = $cache->get();
if(file_exists('cache/'.$cacheHash.'.json.gz') && $cacheRequests == 1) { $cached = ($out !== false);
$cache = @gzdecode(@file_get_contents('cache/'.$cacheHash.'.json.gz'));
$cache = json_decode($cache, 1);
if(!empty($cache['content']) && ($cache['expires'] > time())) {
consoleLogger('Using cached response...');
$out = $cache['content'];
$cached = 1;
} else {
$cached = 0;
}
unset($cache);
}
if(!$cached) { if(!$cached) {
consoleLogger('Fetching information from the server...'); consoleLogger('Fetching information from the server...');
@ -117,15 +105,7 @@ function uupFetchUpd(
consoleLogger('Information has been successfully fetched.'); consoleLogger('Information has been successfully fetched.');
if($cacheRequests == 1) { if($cacheRequests == 1) {
$cache = array( $cache->put($out, 120);
'expires' => time()+120,
'content' => $out,
);
if(!file_exists('cache')) mkdir('cache');
@file_put_contents('cache/'.$cacheHash.'.json.gz', gzencode(json_encode($cache)."\n"));
unset($cache);
} }
} }

38
get.php
View File

@ -18,6 +18,7 @@ limitations under the License.
require_once dirname(__FILE__).'/shared/main.php'; require_once dirname(__FILE__).'/shared/main.php';
require_once dirname(__FILE__).'/shared/requests.php'; require_once dirname(__FILE__).'/shared/requests.php';
require_once dirname(__FILE__).'/shared/packs.php'; require_once dirname(__FILE__).'/shared/packs.php';
require_once dirname(__FILE__).'/shared/cache.php';
/* /*
$updateId = Update Identifier $updateId = Update Identifier
@ -348,26 +349,15 @@ function uupGetFiles(
} }
function uupGetOnlineFiles($updateId, $rev, $info, $cacheRequests, $type) { function uupGetOnlineFiles($updateId, $rev, $info, $cacheRequests, $type) {
$cacheHash = hash('sha256', strtolower("api-get-${updateId}_rev.$rev")); $res = "api-get-${updateId}_rev.$rev";
$cached = 0; $cache = new UupDumpCache($res);
$fromCache = $cache->get();
$cached = ($fromCache !== false);
if(file_exists('cache/'.$cacheHash.'.json.gz') && $cacheRequests == 1) { if($cached) {
$cache = @gzdecode(@file_get_contents('cache/'.$cacheHash.'.json.gz')); $out = $fromCache['out'];
$cache = json_decode($cache, 1); $fetchTime = $fromCache['fetchTime'];
if(!empty($cache['content']) && ($cache['expires'] > time())) {
consoleLogger('Using cached response...');
$out = $cache['content'];
$fetchTime = $cache['fetchTime'];
$cached = 1;
} else { } else {
$cached = 0;
}
unset($cache);
}
if(!$cached) {
$fetchTime = time(); $fetchTime = time();
consoleLogger('Fetching information from the server...'); consoleLogger('Fetching information from the server...');
$postData = composeFileGetRequest($updateId, uupDevice(), $info, $rev, $type); $postData = composeFileGetRequest($updateId, uupDevice(), $info, $rev, $type);
@ -378,7 +368,7 @@ function uupGetOnlineFiles($updateId, $rev, $info, $cacheRequests, $type) {
consoleLogger('Parsing information...'); consoleLogger('Parsing information...');
$xmlOut = @simplexml_load_string($out); $xmlOut = @simplexml_load_string($out);
if($xmlOut === false) { if($xmlOut === false) {
@unlink('cache/'.$cacheHash.'.json.gz'); $cache->delete();
return array('error' => 'XML_PARSE_ERROR'); return array('error' => 'XML_PARSE_ERROR');
} }
@ -474,14 +464,12 @@ function uupGetOnlineFiles($updateId, $rev, $info, $cacheRequests, $type) {
} }
if($cacheRequests == 1 && $cached == 0) { if($cacheRequests == 1 && $cached == 0) {
$cache = array( $cacheData = [
'expires' => time()+90, 'out' => $out,
'content' => $out,
'fetchTime' => $fetchTime, 'fetchTime' => $fetchTime,
); ];
if(!file_exists('cache')) mkdir('cache'); $cache->put($cacheData, 90);
@file_put_contents('cache/'.$cacheHash.'.json.gz', gzencode(json_encode($cache)."\n"));
} }
return $files; return $files;