-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathPRedis.php
142 lines (118 loc) · 3.78 KB
/
PRedis.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
declare(strict_types=1);
namespace Enqueue\Redis;
use Predis\Client;
use Predis\ClientInterface;
use Predis\Response\ServerException as PRedisServerException;
class PRedis implements Redis
{
/**
* @var array
*/
private $parameters;
/**
* @var array
*/
private $options;
/**
* @var ClientInterface
*/
private $redis;
/**
* @see https://github.com/nrk/predis/wiki/Client-Options
*/
public function __construct(array $config)
{
if (false == class_exists(Client::class)) {
throw new \LogicException('The package "predis/predis" must be installed. Please run "composer req predis/predis:^1.1" to install it');
}
$this->options = $config['predis_options'];
$this->parameters = [
'scheme' => $config['scheme'],
'host' => $config['host'],
'port' => $config['port'],
'password' => $config['password'],
'database' => $config['database'],
'path' => $config['path'],
'async' => $config['async'],
'persistent' => $config['persistent'],
'timeout' => $config['timeout'],
'read_write_timeout' => $config['read_write_timeout'],
];
if ($config['ssl']) {
$this->parameters['ssl'] = $config['ssl'];
}
}
public function eval(string $script, array $keys = [], array $args = [])
{
try {
// mixed eval($script, $numkeys, $keyOrArg1 = null, $keyOrArgN = null)
return call_user_func_array([$this->redis, 'eval'], array_merge([$script, count($keys)], $keys, $args));
} catch (PRedisServerException $e) {
throw new ServerException('eval command has failed', 0, $e);
}
}
public function zadd(string $key, string $value, float $score): int
{
try {
return $this->redis->zadd($key, [$value => $score]);
} catch (PRedisServerException $e) {
throw new ServerException('zadd command has failed', 0, $e);
}
}
public function zrem(string $key, string $value): int
{
try {
return $this->redis->zrem($key, [$value]);
} catch (PRedisServerException $e) {
throw new ServerException('zrem command has failed', 0, $e);
}
}
public function lpush(string $key, string $value): int
{
try {
return $this->redis->lpush($key, [$value]);
} catch (PRedisServerException $e) {
throw new ServerException('lpush command has failed', 0, $e);
}
}
public function brpop(array $keys, int $timeout): ?RedisResult
{
try {
if ($result = $this->redis->brpop($keys, $timeout)) {
return new RedisResult($result[0], $result[1]);
}
return null;
} catch (PRedisServerException $e) {
throw new ServerException('brpop command has failed', 0, $e);
}
}
public function rpop(string $key): ?RedisResult
{
try {
if ($message = $this->redis->rpop($key)) {
return new RedisResult($key, $message);
}
return null;
} catch (PRedisServerException $e) {
throw new ServerException('rpop command has failed', 0, $e);
}
}
public function connect(): void
{
if ($this->redis) {
return;
}
$this->redis = new Client($this->parameters, $this->options);
// No need to pass "auth" here because Predis already handles this internally
$this->redis->connect();
}
public function disconnect(): void
{
$this->redis->disconnect();
}
public function del(string $key): void
{
$this->redis->del([$key]);
}
}