Skip to content

Commit 9f3e2f0

Browse files
committed
Merge remote-tracking branch 'origin/master' into pr-515
2 parents c219ef8 + 62824ff commit 9f3e2f0

33 files changed

+1023
-291
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ git:
66
language: php
77

88
php:
9-
- '5.6'
10-
- '7.0'
9+
- '7.1'
10+
- '7.2'
1111

1212
cache:
1313
directories:

PRedis.php

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Enqueue\Redis;
46

7+
use Predis\Client;
58
use Predis\ClientInterface;
69
use Predis\Response\ServerException as PRedisServerException;
710

811
class PRedis implements Redis
912
{
13+
/**
14+
* @var array
15+
*/
16+
private $config;
17+
1018
/**
1119
* @var ClientInterface
1220
*/
@@ -15,18 +23,32 @@ class PRedis implements Redis
1523
/**
1624
* @param ClientInterface $redis
1725
*/
18-
public function __construct(ClientInterface $redis)
26+
public function __construct(array $config)
1927
{
20-
$this->redis = $redis;
28+
$this->config = $this->config = array_replace([
29+
'host' => null,
30+
'port' => null,
31+
'pass' => null,
32+
'user' => null,
33+
'timeout' => null,
34+
'reserved' => null,
35+
'retry_interval' => null,
36+
'persisted' => false,
37+
'database' => 0,
38+
], $config);
39+
40+
// Predis client wants the key to be named "password"
41+
$this->config['password'] = $this->config['pass'];
42+
unset($this->config['pass']);
2143
}
2244

2345
/**
2446
* {@inheritdoc}
2547
*/
26-
public function lpush($key, $value)
48+
public function lpush(string $key, string $value): int
2749
{
2850
try {
29-
$this->redis->lpush($key, [$value]);
51+
return $this->redis->lpush($key, [$value]);
3052
} catch (PRedisServerException $e) {
3153
throw new ServerException('lpush command has failed', null, $e);
3254
}
@@ -35,12 +57,14 @@ public function lpush($key, $value)
3557
/**
3658
* {@inheritdoc}
3759
*/
38-
public function brpop($key, $timeout)
60+
public function brpop(array $keys, int $timeout): ?RedisResult
3961
{
4062
try {
41-
if ($result = $this->redis->brpop([$key], $timeout)) {
42-
return $result[1];
63+
if ($result = $this->redis->brpop($keys, $timeout)) {
64+
return new RedisResult($result[0], $result[1]);
4365
}
66+
67+
return null;
4468
} catch (PRedisServerException $e) {
4569
throw new ServerException('brpop command has failed', null, $e);
4670
}
@@ -49,10 +73,14 @@ public function brpop($key, $timeout)
4973
/**
5074
* {@inheritdoc}
5175
*/
52-
public function rpop($key)
76+
public function rpop(string $key): ?RedisResult
5377
{
5478
try {
55-
return $this->redis->rpop($key);
79+
if ($message = $this->redis->rpop($key)) {
80+
return new RedisResult($key, $message);
81+
}
82+
83+
return null;
5684
} catch (PRedisServerException $e) {
5785
throw new ServerException('rpop command has failed', null, $e);
5886
}
@@ -61,23 +89,32 @@ public function rpop($key)
6189
/**
6290
* {@inheritdoc}
6391
*/
64-
public function connect()
92+
public function connect(): void
6593
{
94+
if ($this->redis) {
95+
return;
96+
}
97+
98+
$this->redis = new Client($this->config, ['exceptions' => true]);
99+
100+
// No need to pass "auth" here because Predis already handles
101+
// this internally
102+
66103
$this->redis->connect();
67104
}
68105

69106
/**
70107
* {@inheritdoc}
71108
*/
72-
public function disconnect()
109+
public function disconnect(): void
73110
{
74111
$this->redis->disconnect();
75112
}
76113

77114
/**
78115
* {@inheritdoc}
79116
*/
80-
public function del($key)
117+
public function del(string $key): void
81118
{
82119
$this->redis->del([$key]);
83120
}

PhpRedis.php

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public function __construct(array $config)
2222
$this->config = array_replace([
2323
'host' => null,
2424
'port' => null,
25-
'timeout' => null,
25+
'pass' => null,
26+
'user' => null,
27+
'timeout' => .0,
2628
'reserved' => null,
2729
'retry_interval' => null,
2830
'persisted' => false,
@@ -33,65 +35,73 @@ public function __construct(array $config)
3335
/**
3436
* {@inheritdoc}
3537
*/
36-
public function lpush($key, $value)
38+
public function lpush(string $key, string $value): int
3739
{
38-
if (false == $this->redis->lPush($key, $value)) {
39-
throw new ServerException($this->redis->getLastError());
40-
}
40+
return $this->redis->lPush($key, $value);
4141
}
4242

4343
/**
4444
* {@inheritdoc}
4545
*/
46-
public function brpop($key, $timeout)
46+
public function brpop(array $keys, int $timeout): ?RedisResult
4747
{
48-
if ($result = $this->redis->brPop([$key], $timeout)) {
49-
return $result[1];
48+
if ($result = $this->redis->brPop($keys, $timeout)) {
49+
return new RedisResult($result[0], $result[1]);
5050
}
51+
52+
return null;
5153
}
5254

5355
/**
5456
* {@inheritdoc}
5557
*/
56-
public function rpop($key)
58+
public function rpop(string $key): ?RedisResult
5759
{
58-
return $this->redis->rPop($key);
60+
if ($message = $this->redis->rPop($key)) {
61+
return new RedisResult($key, $message);
62+
}
63+
64+
return null;
5965
}
6066

6167
/**
6268
* {@inheritdoc}
6369
*/
64-
public function connect()
70+
public function connect(): void
6571
{
66-
if (false == $this->redis) {
67-
$this->redis = new \Redis();
68-
69-
if ($this->config['persisted']) {
70-
$this->redis->pconnect(
71-
$this->config['host'],
72-
$this->config['port'],
73-
$this->config['timeout']
74-
);
75-
} else {
76-
$this->redis->connect(
77-
$this->config['host'],
78-
$this->config['port'],
79-
$this->config['timeout'],
80-
$this->config['reserved'],
81-
$this->config['retry_interval']
82-
);
83-
}
84-
85-
$this->redis->select($this->config['database']);
72+
if ($this->redis) {
73+
return;
74+
}
75+
76+
$this->redis = new \Redis();
77+
78+
if ($this->config['persisted']) {
79+
$this->redis->pconnect(
80+
$this->config['host'],
81+
$this->config['port'],
82+
$this->config['timeout']
83+
);
84+
} else {
85+
$this->redis->connect(
86+
$this->config['host'],
87+
$this->config['port'],
88+
$this->config['timeout'],
89+
$this->config['reserved'],
90+
$this->config['retry_interval']
91+
);
92+
}
93+
94+
if ($this->config['pass']) {
95+
$this->redis->auth($this->config['pass']);
8696
}
8797

88-
return $this->redis;
98+
$this->redis->select($this->config['database']);
8999
}
90100

91101
/**
92102
* {@inheritdoc}
93103
*/
94-
public function disconnect()
104+
public function disconnect(): void
95105
{
96106
if ($this->redis) {
97107
$this->redis->close();
@@ -101,7 +111,7 @@ public function disconnect()
101111
/**
102112
* {@inheritdoc}
103113
*/
104-
public function del($key)
114+
public function del(string $key): void
105115
{
106116
$this->redis->del($key);
107117
}

Redis.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Enqueue\Redis;
46

57
interface Redis
@@ -10,29 +12,29 @@ interface Redis
1012
*
1113
* @return int length of the list
1214
*/
13-
public function lpush($key, $value);
15+
public function lpush(string $key, string $value): int;
1416

1517
/**
16-
* @param string $key
17-
* @param int $timeout in seconds
18+
* @param string[] $keys
19+
* @param int $timeout in seconds
1820
*
19-
* @return string|null
21+
* @return RedisResult|null
2022
*/
21-
public function brpop($key, $timeout);
23+
public function brpop(array $keys, int $timeout): ?RedisResult;
2224

2325
/**
2426
* @param string $key
2527
*
26-
* @return string|null
28+
* @return RedisResult|null
2729
*/
28-
public function rpop($key);
30+
public function rpop(string $key): ?RedisResult;
2931

30-
public function connect();
32+
public function connect(): void;
3133

32-
public function disconnect();
34+
public function disconnect(): void;
3335

3436
/**
3537
* @param string $key
3638
*/
37-
public function del($key);
39+
public function del(string $key): void;
3840
}

0 commit comments

Comments
 (0)