From 56ede1683369ab448c02f635ec052a86313cd5a5 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Mon, 11 Mar 2024 19:54:53 +0100 Subject: [PATCH] Added: Cache::Exists Change: Cache::Get can return NULL --- .../libraries/MailSo/Cache/CacheClient.php | 16 ++++++------ .../MailSo/Cache/DriverInterface.php | 4 ++- .../libraries/MailSo/Cache/Drivers/APCU.php | 9 +++++-- .../libraries/MailSo/Cache/Drivers/File.php | 12 ++++++--- .../MailSo/Cache/Drivers/Memcache.php | 11 +++++--- .../libraries/MailSo/Cache/Drivers/Redis.php | 9 +++++-- .../app/libraries/RainLoop/Actions/Admin.php | 2 +- .../app/libraries/RainLoop/ServiceActions.php | 2 +- .../v/0.0.0/app/libraries/snappymail/dns.php | 2 +- .../app/libraries/snappymail/repository.php | 25 +++++-------------- 10 files changed, 52 insertions(+), 40 deletions(-) diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Cache/CacheClient.php b/snappymail/v/0.0.0/app/libraries/MailSo/Cache/CacheClient.php index 5144c1296..d42b9f09b 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Cache/CacheClient.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Cache/CacheClient.php @@ -49,18 +49,20 @@ class CacheClient return '1' === $this->Get($sKey.'/LOCK'); } - public function Get(string $sKey, bool $bClearAfterGet = false) + public function Exists(string $sKey) : bool { - $sValue = ''; + return $this->oDriver && $this->oDriver->Exists($sKey.$this->sCacheIndex); + } + public function Get(string $sKey, bool $bClearAfterGet = false) : ?string + { + $sValue = null; if ($this->oDriver) { $sValue = $this->oDriver->Get($sKey.$this->sCacheIndex); + if ($bClearAfterGet) { + $this->Delete($sKey); + } } - - if ($bClearAfterGet) { - $this->Delete($sKey); - } - return $sValue; } diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Cache/DriverInterface.php b/snappymail/v/0.0.0/app/libraries/MailSo/Cache/DriverInterface.php index ae7fa478f..06a73ace0 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Cache/DriverInterface.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Cache/DriverInterface.php @@ -19,7 +19,9 @@ interface DriverInterface { public function Set(string $sKey, string $sValue) : bool; - public function Get(string $sKey) : string; + public function Exists(string $sKey) : bool; + + public function Get(string $sKey) : ?string; public function Delete(string $sKey) : void; diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Cache/Drivers/APCU.php b/snappymail/v/0.0.0/app/libraries/MailSo/Cache/Drivers/APCU.php index 090aba857..09162268c 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Cache/Drivers/APCU.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Cache/Drivers/APCU.php @@ -32,10 +32,15 @@ class APCU implements \MailSo\Cache\DriverInterface return \apcu_store($this->generateCachedKey($sKey), (string) $sValue); } - public function Get(string $sKey) : string + public function Exists(string $sKey) : bool + { + return \apcu_exists($this->generateCachedKey($sKey)); + } + + public function Get(string $sKey) : ?string { $sValue = \apcu_fetch($this->generateCachedKey($sKey)); - return \is_string($sValue) ? $sValue : ''; + return \is_string($sValue) ? $sValue : null; } public function Delete(string $sKey) : void diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Cache/Drivers/File.php b/snappymail/v/0.0.0/app/libraries/MailSo/Cache/Drivers/File.php index aa7de2783..a2d27b159 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Cache/Drivers/File.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Cache/Drivers/File.php @@ -46,14 +46,20 @@ class File implements \MailSo\Cache\DriverInterface return '' === $sPath ? false : false !== \file_put_contents($sPath, $sValue); } - public function Get(string $sKey) : string + public function Exists(string $sKey) : bool { - $sValue = ''; + $sPath = $this->generateCachedFileName($sKey); + return '' !== $sPath && \file_exists($sPath); + } + + public function Get(string $sKey) : ?string + { + $sValue = null; $sPath = $this->generateCachedFileName($sKey); if ('' !== $sPath && \file_exists($sPath)) { $sValue = \file_get_contents($sPath); } - return \is_string($sValue) ? $sValue : ''; + return \is_string($sValue) ? $sValue : null; } public function Delete(string $sKey) : void diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Cache/Drivers/Memcache.php b/snappymail/v/0.0.0/app/libraries/MailSo/Cache/Drivers/Memcache.php index b5e2a5252..60ab3a667 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Cache/Drivers/Memcache.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Cache/Drivers/Memcache.php @@ -46,10 +46,15 @@ class Memcache implements \MailSo\Cache\DriverInterface return $this->oMem ? $this->oMem->set($this->generateCachedKey($sKey), $sValue, 0, $this->iExpire) : false; } - public function Get(string $sKey) : string + public function Exists(string $sKey) : bool { - $sValue = $this->oMem ? $this->oMem->get($this->generateCachedKey($sKey)) : ''; - return \is_string($sValue) ? $sValue : ''; + return $this->oMem && false !== $this->oMem->get($this->generateCachedKey($sKey)); + } + + public function Get(string $sKey) : ?string + { + $sValue = $this->oMem ? $this->oMem->get($this->generateCachedKey($sKey)) : null; + return \is_string($sValue) ? $sValue : null; } public function Delete(string $sKey) : void diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Cache/Drivers/Redis.php b/snappymail/v/0.0.0/app/libraries/MailSo/Cache/Drivers/Redis.php index 9271f0cb0..2c41a4209 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Cache/Drivers/Redis.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Cache/Drivers/Redis.php @@ -68,10 +68,15 @@ class Redis implements \MailSo\Cache\DriverInterface return $sValue === true || $sValue == 'OK'; } - public function Get(string $sKey) : string + public function Exists(string $sKey) : bool + { + return $this->oRedis && $this->oRedis->exists($this->generateCachedKey($sKey)); + } + + public function Get(string $sKey) : ?string { $sValue = $this->oRedis ? $this->oRedis->get($this->generateCachedKey($sKey)) : ''; - return \is_string($sValue) ? $sValue : ''; + return \is_string($sValue) ? $sValue : null; } public function Delete(string $sKey) : void diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Admin.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Admin.php index 7e31d78b1..970703a77 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Admin.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Admin.php @@ -15,7 +15,7 @@ trait Admin { if ($this->Config()->Get('security', 'allow_admin_panel', true)) { $sAdminKey = $this->getAdminAuthKey(); - if ($sAdminKey && '' !== $this->Cacher(null, true)->Get(KeyPathHelper::SessionAdminKey($sAdminKey), '')) { + if ($sAdminKey && $this->Cacher(null, true)->Get(KeyPathHelper::SessionAdminKey($sAdminKey))) { return true; } } diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php b/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php index f156b61cd..667a01473 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php @@ -389,7 +389,7 @@ class ServiceActions $sResult = $this->Cacher()->Get($sCacheFileName); } - if (!\strlen($sResult)) { + if (!$sResult) { $sResult = $this->oActions->compileLanguage($sLanguage, $bAdmin); if ($sCacheFileName) { $this->Cacher()->Set($sCacheFileName, $sResult); diff --git a/snappymail/v/0.0.0/app/libraries/snappymail/dns.php b/snappymail/v/0.0.0/app/libraries/snappymail/dns.php index 7802b84a5..ee0ca7517 100644 --- a/snappymail/v/0.0.0/app/libraries/snappymail/dns.php +++ b/snappymail/v/0.0.0/app/libraries/snappymail/dns.php @@ -14,7 +14,7 @@ abstract class DNS $selector = \trim($selector) ?: 'default'; $oCache = \RainLoop\Api::Actions()->Cacher(); $sCacheKey = "dns-bimi-{$domain}-{$selector}"; - $BIMI = $oCache->Get($sCacheKey) ?: null; + $BIMI = $oCache->Get($sCacheKey); if ($BIMI) { $BIMI = \json_decode($BIMI); if ($BIMI[1] < \time()) { diff --git a/snappymail/v/0.0.0/app/libraries/snappymail/repository.php b/snappymail/v/0.0.0/app/libraries/snappymail/repository.php index 89499e55d..2a36360b0 100644 --- a/snappymail/v/0.0.0/app/libraries/snappymail/repository.php +++ b/snappymail/v/0.0.0/app/libraries/snappymail/repository.php @@ -67,40 +67,27 @@ abstract class Repository $bReal = false; $aRep = null; - $sRep = ''; $sRepoFile = 'packages.json'; - $iRepTime = 0; $oCache = \RainLoop\Api::Actions()->Cacher(); $sCacheKey = '/RepositoryCache/Repo/' . static::BASE_URL . '/File/' . $sRepoFile; $sRep = $oCache->Get($sCacheKey); - if ('' !== $sRep) - { - $iRepTime = $oCache->GetTimer($sCacheKey); - } + $iRepTime = $sRep ? $oCache->GetTimer($sCacheKey) : 0; - if ('' === $sRep || 0 === $iRepTime || \time() - 3600 > $iRepTime) - { + if (!$sRep || !$iRepTime || \time() - 3600 > $iRepTime) { $sRep = static::get($sRepoFile); - if ($sRep) - { + if ($sRep) { $aRep = \json_decode($sRep); $bReal = \is_array($aRep) && \count($aRep); - - if ($bReal) - { + if ($bReal) { $oCache->Set($sCacheKey, $sRep); $oCache->SetTimer($sCacheKey); } - } - else - { + } else { throw new \Exception('Cannot read remote repository file: '.$sRepoFile); } - } - else if ('' !== $sRep) - { + } else if ($sRep) { $aRep = \json_decode($sRep, false, 10); $bReal = \is_array($aRep) && \count($aRep); }