Improve caching

This commit is contained in:
eraseyourknees 2022-10-22 01:20:13 +02:00
parent d089aa698c
commit 2cfa0422c1
2 changed files with 37 additions and 43 deletions

View File

@ -94,21 +94,15 @@ function uupFetchUpd(
$res = "api-fetch-$arch-$ring-$flight-$build-$minor-$sku-$type"; $res = "api-fetch-$arch-$ring-$flight-$build-$minor-$sku-$type";
$cache = new UupDumpCache($res); $cache = new UupDumpCache($res);
$out = $cache->get(); $fromCache = $cache->get();
$cached = ($out !== false); if($fromCache !== false) return $fromCache;
if(!$cached) { consoleLogger('Fetching information from the server...');
consoleLogger('Fetching information from the server...'); $postData = composeFetchUpdRequest(uupDevice(), uupEncryptedData(), $arch, $flight, $ring, $build, $sku, $type);
$postData = composeFetchUpdRequest(uupDevice(), uupEncryptedData(), $arch, $flight, $ring, $build, $sku, $type); $out = sendWuPostRequest('https://fe3cr.delivery.mp.microsoft.com/ClientWebService/client.asmx', $postData);
$out = sendWuPostRequest('https://fe3cr.delivery.mp.microsoft.com/ClientWebService/client.asmx', $postData);
$out = html_entity_decode($out); $out = html_entity_decode($out);
consoleLogger('Information has been successfully fetched.'); consoleLogger('Information has been successfully fetched.');
if($cacheRequests == 1) {
$cache->put($out, 120);
}
}
preg_match_all('/<UpdateInfo>.*?<\/UpdateInfo>/', $out, $updateInfos); preg_match_all('/<UpdateInfo>.*?<\/UpdateInfo>/', $out, $updateInfos);
$updateInfo = preg_grep('/<IsLeaf>true<\/IsLeaf>/', $updateInfos[0]); $updateInfo = preg_grep('/<IsLeaf>true<\/IsLeaf>/', $updateInfos[0]);
@ -141,7 +135,7 @@ function uupFetchUpd(
return array('error' => 'EMPTY_FILELIST'); return array('error' => 'EMPTY_FILELIST');
} }
return array( $data = [
'apiVersion' => uupApiVersion(), 'apiVersion' => uupApiVersion(),
'updateId' => $updateArray[0]['updateId'], 'updateId' => $updateArray[0]['updateId'],
'updateTitle' => $updateArray[0]['updateTitle'], 'updateTitle' => $updateArray[0]['updateTitle'],
@ -149,7 +143,13 @@ function uupFetchUpd(
'arch' => $updateArray[0]['arch'], 'arch' => $updateArray[0]['arch'],
'fileWrite' => $updateArray[0]['fileWrite'], 'fileWrite' => $updateArray[0]['fileWrite'],
'updateArray' => $updateArray, 'updateArray' => $updateArray,
); ];
if($cacheRequests == 1) {
$cache->put($data, 120);
}
return $data;
} }
function parseFetchUpdate($updateInfo, $out, $arch, $ring, $flight, $build, $sku, $type) { function parseFetchUpdate($updateInfo, $out, $arch, $ring, $flight, $build, $sku, $type) {

50
get.php
View File

@ -47,6 +47,12 @@ function uupGetFiles(
return array('error' => 'INCORRECT_ID'); return array('error' => 'INCORRECT_ID');
} }
$edition = is_array($desiredEdition) ? implode('_', $desiredEdition) : $desiredEdition;
$res = "api-get-${updateId}_${usePack}_${edition}_${requestType}";
$cache = new UupDumpCache($res);
$fromCache = $cache->get();
if($fromCache !== false) return $fromCache;
$info = uupApiReadFileinfo($updateId); $info = uupApiReadFileinfo($updateId);
if(empty($info)) { if(empty($info)) {
$info = array( $info = array(
@ -155,7 +161,7 @@ function uupGetFiles(
} }
if($requestType < 2) { if($requestType < 2) {
$filesInfoList = uupGetOnlineFiles($updateId, $rev, $info, $requestType, $type); $filesInfoList = uupGetOnlineFiles($updateId, $rev, $info, $type);
} else { } else {
$filesInfoList = uupGetOfflineFiles($info); $filesInfoList = uupGetOfflineFiles($info);
} }
@ -336,7 +342,7 @@ function uupGetFiles(
consoleLogger('Successfully parsed the information.'); consoleLogger('Successfully parsed the information.');
return array( $data = [
'apiVersion' => uupApiVersion(), 'apiVersion' => uupApiVersion(),
'updateName' => $updateName, 'updateName' => $updateName,
'arch' => $updateArch, 'arch' => $updateArch,
@ -344,25 +350,22 @@ function uupGetFiles(
'sku' => $updateSku, 'sku' => $updateSku,
'hasUpdates' => $hasUpdates, 'hasUpdates' => $hasUpdates,
'files' => $files, 'files' => $files,
); ];
if($requestType > 0) {
$cacheData = $data;
$cache->put($cacheData, 90);
}
return $data;
} }
function uupGetOnlineFiles($updateId, $rev, $info, $cacheRequests, $type) { function uupGetOnlineFiles($updateId, $rev, $info, $type) {
$res = "api-get-${updateId}_rev.$rev"; $fetchTime = time();
$cache = new UupDumpCache($res); consoleLogger('Fetching information from the server...');
$fromCache = $cache->get(); $postData = composeFileGetRequest($updateId, uupDevice(), $info, $rev, $type);
$cached = ($fromCache !== false); $out = sendWuPostRequest('https://fe3cr.delivery.mp.microsoft.com/ClientWebService/client.asmx/secured', $postData);
consoleLogger('Information has been successfully fetched.');
if($cached) {
$out = $fromCache['out'];
$fetchTime = $fromCache['fetchTime'];
} else {
$fetchTime = time();
consoleLogger('Fetching information from the server...');
$postData = composeFileGetRequest($updateId, uupDevice(), $info, $rev, $type);
$out = sendWuPostRequest('https://fe3cr.delivery.mp.microsoft.com/ClientWebService/client.asmx/secured', $postData);
consoleLogger('Information has been successfully fetched.');
}
consoleLogger('Parsing information...'); consoleLogger('Parsing information...');
$xmlOut = @simplexml_load_string($out); $xmlOut = @simplexml_load_string($out);
@ -462,15 +465,6 @@ function uupGetOnlineFiles($updateId, $rev, $info, $cacheRequests, $type) {
} }
} }
if($cacheRequests == 1 && $cached == 0) {
$cacheData = [
'out' => $out,
'fetchTime' => $fetchTime,
];
$cache->put($cacheData, 90);
}
return $files; return $files;
} }