This commit is contained in:
Greyscale 2020-11-27 11:47:52 +01:00
parent b8bb6757a8
commit b1bba4cbfb
No known key found for this signature in database
GPG key ID: C6178C19949CFFE3
3 changed files with 34 additions and 29 deletions

View file

@ -265,7 +265,7 @@ class App
} }
// If Redis is configured, add it to the pool. // If Redis is configured, add it to the pool.
if($redis->isAvailable()) { if ($redis->isAvailable()) {
$caches[] = new RedisCachePool($redis); $caches[] = new RedisCachePool($redis);
} }
$caches[] = new ArrayCachePool(); $caches[] = new ArrayCachePool();
@ -290,14 +290,14 @@ class App
}); });
$container->set(Redis::class, function (EnvironmentService $environmentService) { $container->set(Redis::class, function (EnvironmentService $environmentService) {
return (new Redis( return new Redis(
$environmentService->get('REDIS_HOST', 'redis'), $environmentService->get('REDIS_HOST', 'redis'),
$environmentService->get('REDIS_PORT', 6379), $environmentService->get('REDIS_PORT', 6379),
$environmentService->get('REDIS_TIMEOUT', 0.0) $environmentService->get('REDIS_TIMEOUT', 0.0)
)); );
}); });
$container->set(\Redis::class, function(Redis $redis){ $container->set(\Redis::class, function (Redis $redis) {
return $redis; return $redis;
}); });

View file

@ -8,6 +8,9 @@ class Redis extends \Redis
private int $port; private int $port;
private int $timeout; private int $timeout;
/** @var Lua\AbstractLuaExtension[] */
private array $scripts;
public function __construct($host, $port = 6379, $timeout = 0.0) public function __construct($host, $port = 6379, $timeout = 0.0)
{ {
$this->host = $host; $this->host = $host;
@ -16,22 +19,10 @@ class Redis extends \Redis
parent::__construct(); parent::__construct();
} }
public function isAvailable() : bool
{
try {
$this->ping('am I human?');
return true;
}catch(\RedisException $redisException){
return false;
}
}
/** @var Lua\AbstractLuaExtension[] */
private array $scripts;
public function __call($name, $arguments) public function __call($name, $arguments)
{ {
\Kint::dump($name, $arguments);exit; \Kint::dump($name, $arguments);
exit;
foreach ($this->scripts as $script) { foreach ($this->scripts as $script) {
foreach ($script->getFunctionNames() as $functionName) { foreach ($script->getFunctionNames() as $functionName) {
if (strtolower($name) == strtolower($functionName)) { if (strtolower($name) == strtolower($functionName)) {
@ -43,9 +34,20 @@ class Redis extends \Redis
} }
} }
public function get($key) public function isAvailable(): bool
{ {
if(!$this->isConnected()){ try {
$this->ping('am I human?');
return true;
} catch (\RedisException $redisException) {
return false;
}
}
public function get($key): void
{
if (!$this->isConnected()) {
parent::pconnect($this->host, $this->port, $this->timeout); parent::pconnect($this->host, $this->port, $this->timeout);
$this->initialiseExtensions(); $this->initialiseExtensions();
} }
@ -62,6 +64,6 @@ class Redis extends \Redis
public function connect($host, $port = 6379, $timeout = 0.0, $reserved = null, $retryInterval = 0, $readTimeout = 0.0): void public function connect($host, $port = 6379, $timeout = 0.0, $reserved = null, $retryInterval = 0, $readTimeout = 0.0): void
{ {
throw new \RedisException("Do not directly call connect()"); throw new \RedisException('Do not directly call connect()');
} }
} }

View file

@ -7,8 +7,8 @@ use Benzine\Redis\Redis;
class SessionService implements \SessionHandlerInterface class SessionService implements \SessionHandlerInterface
{ {
protected Redis $redis; protected Redis $redis;
private ?bool $redisIsAvailable = null;
protected $oldID; protected $oldID;
private ?bool $redisIsAvailable = null;
private int $lifetime = 43200; private int $lifetime = 43200;
private array $dirtyCheck = []; private array $dirtyCheck = [];
@ -75,10 +75,12 @@ class SessionService implements \SessionHandlerInterface
return true; return true;
} }
public function useRedis() : bool { public function useRedis(): bool
if($this->redisIsAvailable === null){ {
if ($this->redisIsAvailable === null) {
$this->redisIsAvailable = $this->redis->isAvailable(); $this->redisIsAvailable = $this->redis->isAvailable();
} }
return $this->redisIsAvailable; return $this->redisIsAvailable;
} }
@ -95,7 +97,7 @@ class SessionService implements \SessionHandlerInterface
} }
$result = ''; $result = '';
if($this->useRedis()) { if ($this->useRedis()) {
$serialised = $this->redis->get("session:{$session_id}"); $serialised = $this->redis->get("session:{$session_id}");
if (null != $serialised) { if (null != $serialised) {
if (!empty($this->oldID)) { if (!empty($this->oldID)) {
@ -119,9 +121,10 @@ class SessionService implements \SessionHandlerInterface
/** /**
* @param string $session_id * @param string $session_id
* @param string $session_data * @param string $session_data
* @return bool Always returns true. *
* @return bool always returns true
*/ */
public function write($session_id, $session_data) : bool public function write($session_id, $session_data): bool
{ {
if ($this->useAPCU()) { if ($this->useAPCU()) {
$dirty = crc32(apcu_fetch('read-'.$session_id)) != crc32($session_data); $dirty = crc32(apcu_fetch('read-'.$session_id)) != crc32($session_data);
@ -134,8 +137,8 @@ class SessionService implements \SessionHandlerInterface
$this->redis->expire("session:{$session_id}", $this->getLifetime()); $this->redis->expire("session:{$session_id}", $this->getLifetime());
} }
if($this->useAPCU()) { if ($this->useAPCU()) {
apcu_store('read-' . $session_id, $session_data); apcu_store('read-'.$session_id, $session_data);
} }
return true; return true;