Replace GetEbooksFromFilesystem with a DB implementation

This commit is contained in:
Mike Colagrosso 2024-07-13 16:49:48 -06:00 committed by Alex Cabal
parent 364a2ca78f
commit f4a19a2d12
3 changed files with 6 additions and 75 deletions

View file

@ -78,12 +78,13 @@ class Library{
/**
* @return array<Ebook>
* @throws Exceptions\AppException
*/
public static function GetEbooks(): array{
// Get all ebooks, unsorted.
/** @var array<Ebook> */
return self::GetFromApcu('ebooks');
return Db::Query('
SELECT *
from Ebooks
', [], Ebook::class);
}
/**
@ -386,76 +387,6 @@ class Library{
return $artworks;
}
/**
* @return array<mixed>
* @throws Exceptions\AppException
*/
private static function GetFromApcu(string $variable): array{
$results = [];
try{
$results = apcu_fetch($variable);
}
catch(Safe\Exceptions\ApcuException $ex){
try{
// If we can't fetch this variable, rebuild the whole cache.
apcu_fetch('is-cache-fresh');
}
catch(Safe\Exceptions\ApcuException $ex){
Library::RebuildCache();
try{
$results = apcu_fetch($variable);
}
catch(Safe\Exceptions\ApcuException){
// We can get here if the cache is currently rebuilding from a different process.
// Nothing we can do but wait, so wait 20 seconds before retrying
sleep(20);
try{
$results = apcu_fetch($variable);
}
catch(Safe\Exceptions\ApcuException){
// Cache STILL rebuilding... give up silently for now
}
}
}
}
if(!is_array($results)){
$results = [$results];
}
return $results;
}
/**
* @return array<Ebook>
*/
public static function GetEbooksFromFilesystem(?string $webRoot = WEB_ROOT): array{
$ebooks = [];
$contentFiles = explode("\n", trim(shell_exec('find ' . escapeshellarg($webRoot . '/ebooks/') . ' -name "content.opf" | sort')));
foreach($contentFiles as $path){
if($path == '')
continue;
$ebookWwwFilesystemPath = '';
try{
$ebookWwwFilesystemPath = preg_replace('|/content\.opf|ius', '', $path);
$ebooks[] = Ebook::FromFilesystem($ebookWwwFilesystemPath);
}
catch(\Exception){
// An error in a book isn't fatal; just carry on.
}
}
return $ebooks;
}
private static function FillBulkDownloadObject(string $dir, string $downloadType, string $urlRoot): stdClass{
$obj = new stdClass();

View file

@ -104,7 +104,7 @@ function CreateZip(string $filePath, array $ebooks, string $type, string $webRoo
}
// Iterate over all ebooks and arrange them by publication month
foreach(Library::GetEbooksFromFilesystem($webRoot) as $ebook){
foreach(Library::GetEbooks() as $ebook){
$timestamp = $ebook->EbookCreated->format('Y-m');
$updatedTimestamp = $ebook->EbookUpdated->getTimestamp();

View file

@ -89,7 +89,7 @@ foreach($dirs as $dir){
}
// Iterate over all ebooks to build the various feeds.
foreach(Library::GetEbooksFromFilesystem($webRoot) as $ebook){
foreach(Library::GetEbooks() as $ebook){
$allEbooks[] = $ebook;
$newestEbooks[] = $ebook;