From b1bba4cbfbfd8d331acfac5fcf14cf9371369cb4 Mon Sep 17 00:00:00 2001 From: Matthew Baggett Date: Fri, 27 Nov 2020 11:47:52 +0100 Subject: [PATCH] Cleanup --- src/App.php | 8 ++++---- src/Redis/Redis.php | 36 +++++++++++++++++---------------- src/Services/SessionService.php | 19 +++++++++-------- 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/App.php b/src/App.php index f34cdf0..82705b5 100644 --- a/src/App.php +++ b/src/App.php @@ -265,7 +265,7 @@ class App } // If Redis is configured, add it to the pool. - if($redis->isAvailable()) { + if ($redis->isAvailable()) { $caches[] = new RedisCachePool($redis); } $caches[] = new ArrayCachePool(); @@ -290,14 +290,14 @@ class App }); $container->set(Redis::class, function (EnvironmentService $environmentService) { - return (new Redis( + return new Redis( $environmentService->get('REDIS_HOST', 'redis'), $environmentService->get('REDIS_PORT', 6379), $environmentService->get('REDIS_TIMEOUT', 0.0) - )); + ); }); - $container->set(\Redis::class, function(Redis $redis){ + $container->set(\Redis::class, function (Redis $redis) { return $redis; }); diff --git a/src/Redis/Redis.php b/src/Redis/Redis.php index 8a1f5e6..1228e2e 100644 --- a/src/Redis/Redis.php +++ b/src/Redis/Redis.php @@ -8,6 +8,9 @@ class Redis extends \Redis private int $port; private int $timeout; + /** @var Lua\AbstractLuaExtension[] */ + private array $scripts; + public function __construct($host, $port = 6379, $timeout = 0.0) { $this->host = $host; @@ -16,22 +19,10 @@ class Redis extends \Redis 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) { - \Kint::dump($name, $arguments);exit; + \Kint::dump($name, $arguments); + exit; foreach ($this->scripts as $script) { foreach ($script->getFunctionNames() as $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); $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 { - throw new \RedisException("Do not directly call connect()"); + throw new \RedisException('Do not directly call connect()'); } } diff --git a/src/Services/SessionService.php b/src/Services/SessionService.php index d05ad8e..02415a7 100644 --- a/src/Services/SessionService.php +++ b/src/Services/SessionService.php @@ -7,8 +7,8 @@ use Benzine\Redis\Redis; class SessionService implements \SessionHandlerInterface { protected Redis $redis; - private ?bool $redisIsAvailable = null; protected $oldID; + private ?bool $redisIsAvailable = null; private int $lifetime = 43200; private array $dirtyCheck = []; @@ -75,10 +75,12 @@ class SessionService implements \SessionHandlerInterface return true; } - public function useRedis() : bool { - if($this->redisIsAvailable === null){ + public function useRedis(): bool + { + if ($this->redisIsAvailable === null) { $this->redisIsAvailable = $this->redis->isAvailable(); } + return $this->redisIsAvailable; } @@ -95,7 +97,7 @@ class SessionService implements \SessionHandlerInterface } $result = ''; - if($this->useRedis()) { + if ($this->useRedis()) { $serialised = $this->redis->get("session:{$session_id}"); if (null != $serialised) { if (!empty($this->oldID)) { @@ -119,9 +121,10 @@ class SessionService implements \SessionHandlerInterface /** * @param string $session_id * @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()) { $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()); } - if($this->useAPCU()) { - apcu_store('read-' . $session_id, $session_data); + if ($this->useAPCU()) { + apcu_store('read-'.$session_id, $session_data); } return true;