refactor(redis): refine connection handling and pool singleton initialization

- Mark `RedisConnection::$connected` as read-only with `private(set)`.
- Simplify authentication and database selection logic in `RedisConnection`.
- Comment out DI container singleton registration in `RedisPoolInitializer`.
- Annotate `RedisConnectionPool` with `#[Singleton]` attribute for improved clarity.
This commit is contained in:
2025-11-04 02:00:47 +01:00
parent e8f6b239c6
commit e68c25f004
3 changed files with 8 additions and 8 deletions

View File

@@ -14,7 +14,7 @@ final class RedisConnection implements RedisConnectionInterface
{ {
private Redis $client; private Redis $client;
private bool $connected = false; private(set) bool $connected = false;
public function __construct( public function __construct(
private readonly RedisConfig $config, private readonly RedisConfig $config,
@@ -89,15 +89,14 @@ final class RedisConnection implements RedisConnectionInterface
} }
// Authenticate if a password is provided // Authenticate if a password is provided
if ($this->config->password) {
if (! $this->client->auth($this->config->password)) { if ($this->client->auth($this->config->password) === false) {
throw new RedisConnectionException("Redis authentication failed"); throw new RedisConnectionException("Redis authentication failed");
}
} }
// Select database // Select database
if ($this->config->database > 0) { if ($this->config->database > 0) {
if (! $this->client->select($this->config->database)) { if ($this->client->select($this->config->database) === false) {
throw new RedisConnectionException("Failed to select Redis database {$this->config->database}"); throw new RedisConnectionException("Failed to select Redis database {$this->config->database}");
} }
} }

View File

@@ -4,12 +4,14 @@ declare(strict_types=1);
namespace App\Framework\Redis; namespace App\Framework\Redis;
use App\Framework\Attributes\Singleton;
use InvalidArgumentException; use InvalidArgumentException;
/** /**
* Redis connection pool that manages multiple named connections * Redis connection pool that manages multiple named connections
* for different purposes (cache, queue, sessions, etc.) * for different purposes (cache, queue, sessions, etc.)
*/ */
#[Singleton]
final class RedisConnectionPool final class RedisConnectionPool
{ {
/** @var array<string, RedisConnectionInterface> */ /** @var array<string, RedisConnectionInterface> */

View File

@@ -14,7 +14,6 @@ use App\Framework\DI\Initializer;
final readonly class RedisPoolInitializer final readonly class RedisPoolInitializer
{ {
public function __construct( public function __construct(
private Container $container,
private Environment $environment, private Environment $environment,
) { ) {
} }
@@ -46,7 +45,7 @@ final readonly class RedisPoolInitializer
// Remove dynamic configuration loading since we're removing Configuration.php // Remove dynamic configuration loading since we're removing Configuration.php
// Register pool as singleton in DI container // Register pool as singleton in DI container
$this->container->singleton(RedisConnectionPool::class, $pool); #$this->container->singleton(RedisConnectionPool::class, $pool);
return $pool; return $pool;
} }