Fix FCM auth token not being cached persistently if APC is used in PHP-CLI

This commit is contained in:
Kijin Sung 2024-04-23 23:37:57 +09:00
parent 5297232a75
commit 1abd6a04c9
2 changed files with 59 additions and 10 deletions

View file

@ -2,6 +2,7 @@
namespace Rhymix\Framework\Helpers;
use Rhymix\Framework\Drivers\CacheInterface;
use Psr\Cache\CacheItemInterface;
/**
@ -15,15 +16,17 @@ class CacheItemHelper implements CacheItemInterface
public $key = '';
public $value = null;
public $expires = null;
protected $_driver = null;
/**
* Constructor.
*
* @param string $key
*/
public function __construct(string $key)
public function __construct(string $key, CacheInterface $driver)
{
$this->key = $key;
$this->_driver = $driver;
}
/**
@ -45,7 +48,7 @@ class CacheItemHelper implements CacheItemInterface
{
if ($this->value === null)
{
$this->value = \Rhymix\Framework\Cache::get($this->key);
$this->value = $this->_driver->get($this->key);
}
return $this->value;
}
@ -57,7 +60,7 @@ class CacheItemHelper implements CacheItemInterface
*/
public function isHit()
{
return \Rhymix\Framework\Cache::exists($this->key);
return $this->_driver->exists($this->key);
}
/**

View file

@ -11,8 +11,17 @@ use Psr\Cache\InvalidArgumentException;
*/
class CacheItemPoolHelper implements CacheItemPoolInterface
{
/**
* Cache the driver instance here.
*
* @var \Rhymix\Framework\Drivers\CacheInterface
*/
protected $_driver;
/**
* Force persistence even if caching is disabled.
*
* @var bool
*/
public $force = false;
@ -23,7 +32,15 @@ class CacheItemPoolHelper implements CacheItemPoolInterface
*/
public function __construct(bool $force = false)
{
$this->force = $force;
$this->_driver = \Rhymix\Framework\Cache::getDriverInstance();
if ($this->_driver === null || (\PHP_SAPI === 'cli' && $this->_driver instanceof \Rhymix\Framework\Drivers\Cache\APC))
{
$this->_driver = \Rhymix\Framework\Drivers\Cache\Dummy::getInstance([]);
}
if ($this->_driver instanceof \Rhymix\Framework\Drivers\Cache\Dummy)
{
$this->force = $force;
}
}
/**
@ -39,7 +56,10 @@ class CacheItemPoolHelper implements CacheItemPoolInterface
{
throw new InvalidArgumentException;
}
return new CacheItemHelper((string)$key);
$key = $this->_getRealKey($key);
var_dump($key);
return new CacheItemHelper($key, $this->_driver);
}
/**
@ -58,7 +78,9 @@ class CacheItemPoolHelper implements CacheItemPoolInterface
{
throw new InvalidArgumentException;
}
$result[(string)$key] = new CacheItemHelper((string)$key);
$key = $this->_getRealKey($key);
$result[$key] = new CacheItemHelper($key, $this->_driver);
}
return $result;
}
@ -82,7 +104,7 @@ class CacheItemPoolHelper implements CacheItemPoolInterface
*/
public function clear()
{
\Rhymix\Framework\Cache::clearAll();
return $this->_driver->clear();
}
/**
@ -98,7 +120,9 @@ class CacheItemPoolHelper implements CacheItemPoolInterface
{
throw new InvalidArgumentException;
}
return \Rhymix\Framework\Cache::delete((string)$key);
$key = $this->_getRealKey($key);
return $this->_driver->delete($key);
}
/**
@ -117,7 +141,9 @@ class CacheItemPoolHelper implements CacheItemPoolInterface
{
throw new InvalidArgumentException;
}
if (!\Rhymix\Framework\Cache::delete((string)$key))
$key = $this->_getRealKey($key);
if (!$this->_driver->delete($key))
{
$result = false;
}
@ -134,7 +160,7 @@ class CacheItemPoolHelper implements CacheItemPoolInterface
public function save(CacheItemInterface $item)
{
$ttl = $item->expires ? max(0, min(30 * 86400, $item->expires - time())) : 0;
return \Rhymix\Framework\Cache::set($item->key, $item->value, $ttl, $this->force);
return $this->_driver->set($item->key, $item->value, $ttl, $this->force);
}
/**
@ -157,4 +183,24 @@ class CacheItemPoolHelper implements CacheItemPoolInterface
{
return true;
}
/**
* Get the real key in a way that is mostly compatible with R\F\Cache.
*
* @param string
* @return string
*/
protected function _getRealKey($key): string
{
$key = preg_replace_callback('/[^\x21-\x7E]/', function($match) {
return rawurlencode($match[0]);
}, $key);
if (preg_match('/^([^:]+):(.+)$/i', $key, $matches))
{
$key = $matches[1] . '#0:' . $matches[2];
}
return \Rhymix\Framework\Cache::getPrefix() . $key;
}
}