Skip to content
This repository was archived by the owner on Jun 2, 2023. It is now read-only.

Commit

Permalink
add possibility to use password protected redis servers (#5)
Browse files Browse the repository at this point in the history
Co-authored-by: Ruslan Zhosan <ruslan.zhosan@lamoda.ru>
  • Loading branch information
josanr and Ruslan Zhosan authored Feb 8, 2022
1 parent 868def8 commit 694b465
Showing 3 changed files with 55 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/RedisConfig.php
Original file line number Diff line number Diff line change
@@ -21,6 +21,9 @@ final class RedisConfig
/** @var string|null */
private $connectionName;

/** @var string|null */
private $password;

public function __construct(array $options = [])
{
$options = array_merge([
@@ -29,13 +32,15 @@ public function __construct(array $options = [])
'protocol' => 'tcp',
'dbIndex' => 0,
'connectionName' => null,
'password' => null,
], $options);

$this->host = $options['host'];
$this->port = (int) $options['port'];
$this->protocol = $options['protocol'];
$this->dbIndex = (int) $options['dbIndex'];
$this->connectionName = $options['connectionName'];
$this->password = $options['password'];
}

public function getHost(): string
@@ -62,4 +67,9 @@ public function getConnectionName(): ?string
{
return $this->connectionName;
}

public function getPassword(): ?string
{
return $this->password;
}
}
14 changes: 13 additions & 1 deletion src/RedisLocator.php
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@
namespace Lamoda\RedisSentinel;

use PSRedis\Client;
use PSRedis\Client\Adapter\Predis\PredisClientCreator;
use PSRedis\Client\Adapter\PredisClientAdapter;
use PSRedis\MasterDiscovery;

class RedisLocator
@@ -59,10 +61,20 @@ private function initMasterDiscovery(): ?MasterDiscovery
}
$host = $url['host'] ?? $url['path'];
$port = $url['port'] ?? 26379;
$sentinel = new Client($host, $port);

$redisClientAdapter = $this->getRedisClientAdapter();
$sentinel = new Client($host, $port, $redisClientAdapter);

$masterDiscovery->addSentinel($sentinel);
}

return $masterDiscovery;
}

private function getRedisClientAdapter(): PredisClientAdapter
{
$clientFactory = new WithPasswordClientFactoryDecorator($this->redisConfig, new PredisClientCreator());

return new PredisClientAdapter($clientFactory, Client::TYPE_SENTINEL);
}
}
32 changes: 32 additions & 0 deletions src/WithPasswordClientFactoryDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Lamoda\RedisSentinel;

use PSRedis\Client;
use PSRedis\Client\Adapter\Predis\PredisClientFactory;

final class WithPasswordClientFactoryDecorator implements PredisClientFactory
{
/** @var array */
private $config;

/** @var PredisClientFactory */
private $decorated;

public function __construct(array $config, PredisClientFactory $decorated)
{
$this->config = $config;
$this->decorated = $decorated;
}

public function createClient($clientType, array $parameters = []): \Predis\Client
{
if ($clientType === Client::TYPE_REDIS && isset($this->config['password'])) {
$parameters['password'] = $this->config['password'];
}

return $this->decorated->createClient($clientType, $parameters);
}
}

0 comments on commit 694b465

Please sign in to comment.