Added: Cache::Exists

Change: Cache::Get can return NULL
This commit is contained in:
the-djmaze 2024-03-11 19:54:53 +01:00
parent 705907e816
commit 56ede16833
10 changed files with 52 additions and 40 deletions

View file

@ -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;
}

View file

@ -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;

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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;
}
}

View file

@ -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);

View file

@ -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()) {

View file

@ -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);
}