Skip to content

Commit

Permalink
Add support for Redis DSN options
Browse files Browse the repository at this point in the history
This commit introduces the capability to specify additional options for Redis DSN through the `RedisDsnOptions` annotation. The `StorageRedisDsnModule`, `RedisDsnProvider`, and associated tests have been updated to accommodate these options, allowing for more flexible Redis connection configurations.
  • Loading branch information
koriym committed Sep 4, 2024
1 parent a86d295 commit 3d2a788
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
14 changes: 14 additions & 0 deletions src-annotation/RedisDsnOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace BEAR\RepositoryModule\Annotation;

use Attribute;
use Ray\Di\Di\Qualifier;

#[Attribute(Attribute::TARGET_PARAMETER)]
#[Qualifier]
final class RedisDsnOptions
{
}
12 changes: 9 additions & 3 deletions src/RedisDsnProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace BEAR\QueryRepository;

use BEAR\RepositoryModule\Annotation\RedisDsn;
use BEAR\RepositoryModule\Annotation\RedisDsnOptions;
use Predis\ClientInterface;
use Ray\Di\ProviderInterface;
use Redis;
Expand All @@ -16,15 +17,20 @@
/** @implements ProviderInterface<Redis|RedisArray|RedisCluster|ClientInterface|Relay> */
final class RedisDsnProvider implements ProviderInterface

Check failure on line 18 in src/RedisDsnProvider.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

UndefinedDocblockClass: Docblock-defined class, interface or enum named Relay\Relay does not exist

Check failure on line 18 in src/RedisDsnProvider.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

UndefinedDocblockClass: Docblock-defined class, interface or enum named Relay\Relay does not exist
{
/** @param string $redisDsn Redis */
/**
* @param string $dns Redis DNS
* @param array<string, bool|int|string|array<string, mixed>|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

Check failure on line 32 in src/RedisDsnProvider.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

UndefinedClass: Class, interface or enum named Relay\Relay does not exist

Check failure on line 32 in src/RedisDsnProvider.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

UndefinedClass: Class, interface or enum named Relay\Relay does not exist
{
return RedisAdapter::createConnection($this->redisDsn);
return RedisAdapter::createConnection($this->dns, $this->options);

Check failure on line 34 in src/RedisDsnProvider.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

UndefinedClass: Class, interface or enum named Relay\Relay does not exist

Check failure on line 34 in src/RedisDsnProvider.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

UndefinedClass: Class, interface or enum named Relay\Relay does not exist
}
}
13 changes: 11 additions & 2 deletions src/StorageRedisDsnModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,8 +39,15 @@
*/
final class StorageRedisDsnModule extends AbstractModule
{
/**
* Redis configuration DSN
*
* @param string $dns Redis DNS
* @param array<string, bool|int|string|array<string, mixed>|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);
Expand All @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion tests-pecl-ext/StorageRedisDsnModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 3d2a788

Please sign in to comment.