Skip to content

Commit b8c4d3d

Browse files
committed
[redis] Improve redis config, use enqueue/dsn
1 parent 4025d32 commit b8c4d3d

6 files changed

+176
-177
lines changed

Diff for: PRedis.php

+26-39
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,42 @@ class PRedis implements Redis
1313
/**
1414
* @var array
1515
*/
16-
private $config;
16+
private $parameters;
17+
18+
/**
19+
* @var array
20+
*/
21+
private $options;
1722

1823
/**
1924
* @var ClientInterface
2025
*/
2126
private $redis;
2227

2328
/**
24-
* @param ClientInterface $redis
29+
* @see https://github.com/nrk/predis/wiki/Client-Options
2530
*/
2631
public function __construct(array $config)
2732
{
28-
$this->config = $this->config = array_replace([
29-
'scheme' => null,
30-
'host' => null,
31-
'port' => null,
32-
'pass' => null,
33-
'user' => null,
34-
'timeout' => null,
35-
'reserved' => null,
36-
'retry_interval' => null,
37-
'persisted' => false,
38-
'database' => 0,
39-
], $config);
40-
41-
// Predis client wants the key to be named "password"
42-
$this->config['password'] = $this->config['pass'];
43-
unset($this->config['pass']);
33+
$this->options = $config['predis_options'];
34+
35+
$this->parameters = [
36+
'scheme' => $config['scheme'],
37+
'host' => $config['host'],
38+
'port' => $config['port'],
39+
'password' => $config['password'],
40+
'path' => $config['path'],
41+
'async' => $config['async'],
42+
'persistent' => $config['persistent'],
43+
'timeout' => $config['timeout'],
44+
'read_write_timeout' => $config['read_write_timeout'],
45+
];
46+
47+
if ($config['ssl']) {
48+
$this->parameters['ssl'] = $config['ssl'];
49+
}
4450
}
4551

46-
/**
47-
* {@inheritdoc}
48-
*/
4952
public function lpush(string $key, string $value): int
5053
{
5154
try {
@@ -55,9 +58,6 @@ public function lpush(string $key, string $value): int
5558
}
5659
}
5760

58-
/**
59-
* {@inheritdoc}
60-
*/
6161
public function brpop(array $keys, int $timeout): ?RedisResult
6262
{
6363
try {
@@ -71,9 +71,6 @@ public function brpop(array $keys, int $timeout): ?RedisResult
7171
}
7272
}
7373

74-
/**
75-
* {@inheritdoc}
76-
*/
7774
public function rpop(string $key): ?RedisResult
7875
{
7976
try {
@@ -87,34 +84,24 @@ public function rpop(string $key): ?RedisResult
8784
}
8885
}
8986

90-
/**
91-
* {@inheritdoc}
92-
*/
9387
public function connect(): void
9488
{
9589
if ($this->redis) {
9690
return;
9791
}
9892

99-
$this->redis = new Client($this->config, ['exceptions' => true]);
93+
$this->redis = new Client($this->parameters, $this->options);
10094

101-
// No need to pass "auth" here because Predis already handles
102-
// this internally
95+
// No need to pass "auth" here because Predis already handles this internally
10396

10497
$this->redis->connect();
10598
}
10699

107-
/**
108-
* {@inheritdoc}
109-
*/
110100
public function disconnect(): void
111101
{
112102
$this->redis->disconnect();
113103
}
114104

115-
/**
116-
* {@inheritdoc}
117-
*/
118105
public function del(string $key): void
119106
{
120107
$this->redis->del([$key]);

Diff for: PhpRedis.php

+49-59
Original file line numberDiff line numberDiff line change
@@ -15,107 +15,97 @@ class PhpRedis implements Redis
1515
private $config;
1616

1717
/**
18-
* @param array $config
18+
* @see https://github.com/phpredis/phpredis#parameters
1919
*/
2020
public function __construct(array $config)
2121
{
22-
$this->config = array_replace([
23-
'scheme' => null,
24-
'host' => null,
25-
'port' => null,
26-
'pass' => null,
27-
'user' => null,
28-
'timeout' => .0,
29-
'reserved' => null,
30-
'retry_interval' => null,
31-
'persisted' => false,
32-
'database' => 0,
33-
], $config);
22+
$this->config = $config;
3423
}
3524

36-
/**
37-
* {@inheritdoc}
38-
*/
3925
public function lpush(string $key, string $value): int
4026
{
41-
return $this->redis->lPush($key, $value);
27+
try {
28+
return $this->redis->lPush($key, $value);
29+
} catch (\RedisException $e) {
30+
throw new ServerException('lpush command has failed', null, $e);
31+
}
4232
}
4333

44-
/**
45-
* {@inheritdoc}
46-
*/
4734
public function brpop(array $keys, int $timeout): ?RedisResult
4835
{
49-
if ($result = $this->redis->brPop($keys, $timeout)) {
50-
return new RedisResult($result[0], $result[1]);
36+
try {
37+
if ($result = $this->redis->brPop($keys, $timeout)) {
38+
return new RedisResult($result[0], $result[1]);
39+
}
40+
41+
return null;
42+
} catch (\RedisException $e) {
43+
throw new ServerException('brpop command has failed', null, $e);
5144
}
52-
53-
return null;
5445
}
5546

56-
/**
57-
* {@inheritdoc}
58-
*/
5947
public function rpop(string $key): ?RedisResult
6048
{
61-
if ($message = $this->redis->rPop($key)) {
62-
return new RedisResult($key, $message);
49+
try {
50+
if ($message = $this->redis->rPop($key)) {
51+
return new RedisResult($key, $message);
52+
}
53+
54+
return null;
55+
} catch (\RedisException $e) {
56+
throw new ServerException('rpop command has failed', null, $e);
6357
}
64-
65-
return null;
6658
}
6759

68-
/**
69-
* {@inheritdoc}
70-
*/
7160
public function connect(): void
7261
{
7362
if ($this->redis) {
7463
return;
7564
}
7665

77-
if ('rediss' == $this->config['scheme']) {
78-
throw new \LogicException('The phpredis extension does not support secured connections. Try to use predis library as vendor.');
66+
$supportedSchemes = ['redis', 'tcp', 'unix'];
67+
if (false == in_array($this->config['scheme'], $supportedSchemes, true)) {
68+
throw new \LogicException(sprintf(
69+
'The given scheme protocol "%s" is not supported by php extension. It must be one of "%s"',
70+
$this->config['scheme'],
71+
implode('", "', $supportedSchemes)
72+
));
7973
}
8074

8175
$this->redis = new \Redis();
8276

83-
if ($this->config['persisted']) {
84-
$this->redis->pconnect(
85-
$this->config['host'],
86-
$this->config['port'],
87-
$this->config['timeout']
88-
);
89-
} else {
90-
$this->redis->connect(
91-
$this->config['host'],
92-
$this->config['port'],
93-
$this->config['timeout'],
94-
$this->config['reserved'],
95-
$this->config['retry_interval']
96-
);
77+
$connectionMethod = $this->config['persisted'] ? 'pconnect' : 'connect';
78+
79+
$result = call_user_func(
80+
[$this->redis, $connectionMethod],
81+
'unix' === $this->config['scheme'] ? $this->config['path'] : $this->config['host'],
82+
$this->config['port'],
83+
$this->config['timeout'],
84+
$this->config['persisted'] ? ($this->config['phpredis_persistent_id'] ?? null) : null,
85+
$this->config['phpredis_retry_interval'] ?? null,
86+
$this->config['read_write_timeout']
87+
);
88+
89+
if (false == $result) {
90+
throw new ServerException('Failed to connect.');
9791
}
9892

99-
if ($this->config['pass']) {
100-
$this->redis->auth($this->config['pass']);
93+
if ($this->config['password']) {
94+
$this->redis->auth($this->config['password']);
10195
}
10296

103-
$this->redis->select($this->config['database']);
97+
if (null !== $this->config['database']) {
98+
$this->redis->select($this->config['database']);
99+
}
104100
}
105101

106-
/**
107-
* {@inheritdoc}
108-
*/
109102
public function disconnect(): void
110103
{
111104
if ($this->redis) {
112105
$this->redis->close();
113106
}
114107
}
115108

116-
/**
117-
* {@inheritdoc}
118-
*/
119109
public function del(string $key): void
120110
{
121111
$this->redis->del($key);

Diff for: Redis.php

+11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ interface Redis
1010
* @param string $key
1111
* @param string $value
1212
*
13+
* @throws ServerException
14+
*
1315
* @return int length of the list
1416
*/
1517
public function lpush(string $key, string $value): int;
@@ -18,23 +20,32 @@ public function lpush(string $key, string $value): int;
1820
* @param string[] $keys
1921
* @param int $timeout in seconds
2022
*
23+
* @throws ServerException
24+
*
2125
* @return RedisResult|null
2226
*/
2327
public function brpop(array $keys, int $timeout): ?RedisResult;
2428

2529
/**
2630
* @param string $key
2731
*
32+
* @throws ServerException
33+
*
2834
* @return RedisResult|null
2935
*/
3036
public function rpop(string $key): ?RedisResult;
3137

38+
/**
39+
* @throws ServerException
40+
*/
3241
public function connect(): void;
3342

3443
public function disconnect(): void;
3544

3645
/**
3746
* @param string $key
47+
*
48+
* @throws ServerException
3849
*/
3950
public function del(string $key): void;
4051
}

0 commit comments

Comments
 (0)