diff --git a/src-annotation/RedisDsnOptions.php b/src-annotation/RedisDsnOptions.php new file mode 100644 index 00000000..52c310a4 --- /dev/null +++ b/src-annotation/RedisDsnOptions.php @@ -0,0 +1,14 @@ + */ final class RedisDsnProvider implements ProviderInterface { - /** @param string $redisDsn Redis */ + /** + * @param string $dns Redis DNS + * @param array|null> $options Redis DNS Options + */ public function __construct( #[RedisDsn] - private string $redisDsn, + private string $dns, + #[RedisDsnOptions] + private array $options, ) { } public function get(): Redis|RedisArray|RedisCluster|ClientInterface|Relay { - return RedisAdapter::createConnection($this->redisDsn); + return RedisAdapter::createConnection($this->dns, $this->options); } } diff --git a/src/StorageRedisDsnModule.php b/src/StorageRedisDsnModule.php index 8056f66e..8e5a2bcd 100644 --- a/src/StorageRedisDsnModule.php +++ b/src/StorageRedisDsnModule.php @@ -5,6 +5,7 @@ namespace BEAR\QueryRepository; use BEAR\RepositoryModule\Annotation\RedisDsn; +use BEAR\RepositoryModule\Annotation\RedisDsnOptions; use Psr\Cache\CacheItemPoolInterface; use Ray\Di\AbstractModule; use Ray\Di\ProviderInterface; @@ -38,8 +39,15 @@ */ final class StorageRedisDsnModule extends AbstractModule { + /** + * Redis configuration DSN + * + * @param string $dns Redis DNS + * @param array|null> $options Redis DNS Options + */ public function __construct( - private readonly string $dsn, + private readonly string $dns, + private readonly array $options = [], AbstractModule|null $module = null, ) { parent::__construct($module); @@ -48,7 +56,8 @@ public function __construct( /** @throws ReflectionException */ protected function configure(): void { - $this->bind()->annotatedWith(RedisDsn::class)->toInstance($this->dsn); + $this->bind()->annotatedWith(RedisDsn::class)->toInstance($this->dns); + $this->bind()->annotatedWith(RedisDsnOptions::class)->toInstance($this->options); $this->bind(CacheItemPoolInterface::class)->annotatedWith(Local::class)->toConstructor(ApcuAdapter::class, ['namespace' => CacheNamespace::class])->in(Scope::SINGLETON); $this->bind(CacheItemPoolInterface::class)->annotatedWith(Shared::class)->toConstructor(RedisAdapter::class, [ 'redisProvider' => 'redis', diff --git a/tests-pecl-ext/StorageRedisDsnModuleTest.php b/tests-pecl-ext/StorageRedisDsnModuleTest.php index c18e7590..f69acd80 100644 --- a/tests-pecl-ext/StorageRedisDsnModuleTest.php +++ b/tests-pecl-ext/StorageRedisDsnModuleTest.php @@ -46,7 +46,8 @@ public function testNew(): void { // @see https://symfony.com/doc/current/components/cache/adapters/redis_adapter.html $dsn = 'redis://localhost:6379'; - $cache = (new Injector(new StorageRedisDsnModule($dsn), __DIR__ . '/tmp'))->getInstance(CacheItemPoolInterface::class, Shared::class); + $options = ['timeout' => 10]; + $cache = (new Injector(new StorageRedisDsnModule($dsn, $options), __DIR__ . '/tmp'))->getInstance(CacheItemPoolInterface::class, Shared::class); $this->assertInstanceOf(RedisAdapter::class, $cache); } }