Skip to content

Commit 3dfdebb

Browse files
committed
Allowing db configuration in connection
Signed-off-by: RJ Garcia <[email protected]>
1 parent b549b27 commit 3dfdebb

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/RedisConnection.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ final class RedisConnection implements MetricsRepository
1010
private $queue;
1111
private $connectParams;
1212
private $blockingTimeout;
13+
private $configureConnection;
1314
private $isConnected;
1415

15-
public function __construct(Redis $redis, string $queue, array $connectParams = ['127.0.0.1', 6379], int $blockingTimeout = 30) {
16+
public function __construct(Redis $redis, string $queue, array $connectParams = ['127.0.0.1', 6379], int $blockingTimeout = 30, callable $configureConnection = null) {
1617
$this->redis = $redis;
1718
$this->queue = $queue;
1819
$this->connectParams = $connectParams;
1920
$this->blockingTimeout = $blockingTimeout;
21+
$this->configureConnection = $configureConnection;
2022
$this->isConnected = false;
2123
}
2224

@@ -36,7 +38,13 @@ public static function fromDsn(string $dsn, array $options = []): self {
3638
new Redis(),
3739
$options['queue'] ?? $query['queue'],
3840
[$parsedUrl['host'] ?? '127.0.0.1', intval($parsedUrl['port'] ?? 6379)],
39-
$options['blocking_timeout'] ?? $query['blocking_timeout'] ?? 30
41+
$options['blocking_timeout'] ?? $query['blocking_timeout'] ?? 30,
42+
function(Redis $conn) use ($query, $options) {
43+
$db = $options['db'] ?? $query['db'] ?? null;
44+
if ($db !== null) {
45+
$conn->select($db);
46+
}
47+
}
4048
);
4149
}
4250

@@ -75,6 +83,9 @@ private function connect(): void {
7583

7684
$this->isConnected = true;
7785
$this->redis->connect(...$this->connectParams);
86+
if ($configureConnection = $this->configureConnection) {
87+
$configureConnection($this->redis);
88+
}
7889
}
7990

8091
private function clearMessageFromProcessingQueue(Redis $redis, string $message) {

0 commit comments

Comments
 (0)