Pull out apcu code into a function to improve error handling

This commit is contained in:
Alex Cabal 2022-03-23 13:49:00 -05:00
parent 0bf50873ac
commit beda605f32
4 changed files with 43 additions and 60 deletions

View file

@ -1,7 +1,6 @@
<?
// Auto-included by Composer in composer.json to satisfy PHPStan
use function Safe\define;
use function Safe\file_get_contents;
use function Safe\gmdate;
use function Safe\strtotime;

View file

@ -2,8 +2,10 @@
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use function Safe\define;
use function Safe\file_get_contents;
define('EMAIL_SMTP_USERNAME', trim(file_get_contents(POSTMARK_SECRET_FILE_PATH)) ?: '');
const EMAIL_SMTP_PASSWORD = EMAIL_SMTP_USERNAME;
class Email{
public $To = '';
@ -54,7 +56,7 @@ class Email{
$phpMailer->Port = 587;
$phpMailer->Host = EMAIL_SMTP_HOST;
$phpMailer->Username = EMAIL_SMTP_USERNAME;
$phpMailer->Password = EMAIL_SMTP_PASSWORD;
$phpMailer->Password = EMAIL_SMTP_USERNAME;
if($this->PostmarkStream !== null){
$phpMailer->addCustomHeader('X-PM-Message-Stream', $this->PostmarkStream);

View file

@ -101,64 +101,22 @@ class Library{
* @return array<Ebook>
*/
public static function GetEbooks(): array{
$ebooks = [];
// Get all ebooks, unsorted.
try{
$ebooks = apcu_fetch('ebooks');
}
catch(Safe\Exceptions\ApcuException $ex){
Library::RebuildCache();
try{
$ebooks = apcu_fetch('ebooks');
}
catch(Safe\Exceptions\ApcuException $ex){
// 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{
$ebooks = apcu_fetch('ebooks');
}
catch(Safe\Exceptions\ApcuException $ex){
// Cache STILL rebuilding... give up silently for now
}
}
}
return $ebooks;
return self::GetFromApcu('ebooks');
}
/**
* @return array<Ebook>
*/
public static function GetEbooksByAuthor(string $wwwFilesystemPath): array{
// Do we have the author's ebooks cached?
$ebooks = [];
try{
$ebooks = apcu_fetch('author-' . $wwwFilesystemPath);
}
catch(Safe\Exceptions\ApcuException $ex){
}
return $ebooks;
return self::GetFromApcu('author-' . $wwwFilesystemPath);
}
/**
* @return array<Ebook>
*/
public static function GetEbooksByTag(string $tag): array{
// Do we have the tag's ebooks cached?
$ebooks = [];
try{
$ebooks = apcu_fetch('tag-' . $tag);
}
catch(Safe\Exceptions\ApcuException $ex){
}
return $ebooks;
return self::GetFromApcu('tag-' . $tag);
}
/**
@ -166,23 +124,45 @@ class Library{
*/
public static function GetEbooksByCollection(string $collection): array{
// Do we have the tag's ebooks cached?
$ebooks = [];
try{
$ebooks = apcu_fetch('collection-' . $collection);
}
catch(Safe\Exceptions\ApcuException $ex){
}
return $ebooks;
return self::GetFromApcu('collection-' . $collection);
}
/**
* @return array<Tag>
*/
public static function GetTags(): array{
return apcu_fetch('tags');
return self::GetFromApcu('tags');
}
/**
* @return array<mixed>
*/
private static function GetFromApcu(string $variable): array{
$results = [];
try{
$results = apcu_fetch($variable);
}
catch(Safe\Exceptions\ApcuException $ex){
Library::RebuildCache();
try{
$results = apcu_fetch($variable);
}
catch(Safe\Exceptions\ApcuException $ex){
// 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 $ex){
// Cache STILL rebuilding... give up silently for now
}
}
}
return $results;
}
/**