-
Notifications
You must be signed in to change notification settings - Fork 436
Expand file tree
/
Copy pathPRedis.php
More file actions
159 lines (131 loc) · 4.4 KB
/
PRedis.php
File metadata and controls
159 lines (131 loc) · 4.4 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?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');
}
if (array_key_exists('client', $config) && null !== $config['client']) {
if (!$config['client'] instanceof Client) {
throw new \InvalidArgumentException(sprintf('%s configuration property is expected to be an instance of %s class. %s was passed instead.', 'client', Client::class, gettype($config['client'])));
}
$this->redis = $config['client'];
$this->options = [];
$this->parameters = [];
return;
}
$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 static function createWithClient(Client $client): self
{
return new self(['client' => $client]);
}
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]);
}
}