-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRedisCluster.php
More file actions
193 lines (153 loc) · 4.86 KB
/
RedisCluster.php
File metadata and controls
193 lines (153 loc) · 4.86 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace Utopia\Queue\Connection;
use Utopia\Queue\Connection;
class RedisCluster implements Connection
{
protected array $seeds;
protected float $connectTimeout;
protected float $readTimeout;
protected ?\RedisCluster $redis = null;
public function __construct(array $seeds, float $connectTimeout = -1, float $readTimeout = -1)
{
$this->seeds = $seeds;
$this->connectTimeout = $connectTimeout;
$this->readTimeout = $readTimeout;
}
public function rightPopLeftPushArray(string $queue, string $destination, int $timeout): array|false
{
$response = $this->rightPopLeftPush($queue, $destination, $timeout);
if (!$response) {
return false;
}
return json_decode($response, true);
}
public function rightPopLeftPush(string $queue, string $destination, int $timeout): string|false
{
$response = $this->getRedis()->bRPopLPush($queue, $destination, $timeout);
if (!$response) {
return false;
}
return $response;
}
public function rightPushArray(string $queue, array $value): bool
{
return !!$this->getRedis()->rPush($queue, json_encode($value));
}
public function rightPush(string $queue, string $value): bool
{
return !!$this->getRedis()->rPush($queue, $value);
}
public function leftPushArray(string $queue, array $value): bool
{
return !!$this->getRedis()->lPush($queue, json_encode($value));
}
public function leftPush(string $queue, string $value): bool
{
return !!$this->getRedis()->lPush($queue, $value);
}
public function rightPopArray(string $queue, int $timeout): array|false
{
$response = $this->rightPop($queue, $timeout);
if ($response === false) {
return false;
}
return json_decode($response, true) ?? false;
}
public function rightPop(string $queue, int $timeout): string|false
{
$response = $this->getRedis()->brPop([$queue], $timeout);
if (empty($response)) {
return false;
}
return $response[1];
}
public function leftPopArray(string $queue, int $timeout): array|false
{
$response = $this->getRedis()->blPop($queue, $timeout);
if (empty($response)) {
return false;
}
return json_decode($response[1], true) ?? false;
}
public function leftPop(string $queue, int $timeout): string|false
{
$response = $this->getRedis()->blPop($queue, $timeout);
if (empty($response)) {
return false;
}
return $response[1];
}
public function listRemove(string $queue, string $key): bool
{
return !!$this->getRedis()->lRem($queue, $key, 1);
}
public function remove(string $key): bool
{
return !!$this->getRedis()->del($key);
}
public function move(string $queue, string $destination): bool
{
// Move is not supported for Redis Cluster
return false;
}
public function setArray(string $key, array $value, int $ttl = 0): bool
{
return $this->set($key, json_encode($value), $ttl);
}
public function set(string $key, string $value, int $ttl = 0): bool
{
if ($ttl > 0) {
return $this->getRedis()->setex($key, $ttl, $value);
}
return $this->getRedis()->set($key, $value);
}
public function get(string $key): array|string|null
{
return $this->getRedis()->get($key);
}
public function listSize(string $key): int
{
return $this->getRedis()->lLen($key);
}
public function increment(string $key): int
{
return $this->getRedis()->incr($key);
}
public function decrement(string $key): int
{
return $this->getRedis()->decr($key);
}
public function listRange(string $key, int $total, int $offset): array
{
$start = $offset;
$end = $start + $total - 1;
$results = $this->getRedis()->lRange($key, $start, $end);
return $results;
}
public function ping(): bool
{
try {
foreach ($this->getRedis()->_masters() as $master) {
$this->getRedis()->ping($master);
}
return true;
} catch (\Throwable) {
return false;
}
}
public function close(): void
{
$this->redis?->close();
$this->redis = null;
}
protected function getRedis(): \RedisCluster
{
if ($this->redis) {
return $this->redis;
}
$connectTimeout = $this->connectTimeout < 0 ? 0 : $this->connectTimeout;
$readTimeout = $this->readTimeout < 0 ? 0 : $this->readTimeout;
$this->redis = new \RedisCluster(null, $this->seeds, $connectTimeout, $readTimeout);
return $this->redis;
}
}