forked from ziogas/PHP-Redis-implementation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocket.php
More file actions
executable file
·296 lines (257 loc) · 7.64 KB
/
Socket.php
File metadata and controls
executable file
·296 lines (257 loc) · 7.64 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php declare(strict_types=1);
/**
* Issues commands to Redis server over TCP/UNIX socket and parses responses
*
* @package ziogas\Redis
*
* Based on https://github.com/ziogas/PHP-Redis-implementation which was
* released under MIT License by Arminas Žukauskas - arminas@ini.lt
*/
namespace ziogas\Redis;
use \ziogas\Redis\Exception as RedisException;
class Socket
{
const INT = ':';
const STRING = '+';
const BULK = '$';
const MULTIBULK = '*';
const ERROR = '-';
const NL = "\r\n";
/** @var resource|null */
private mixed $socket = null;
private string $socket_address = 'tcp://127.0.0.1:6379';
private int $timeout = 30;
private int $connect_timeout = 3;
/** @var array<string> */
private array $queue = [];
public function setOptions(
?string $socket_address = null,
?int $timeout = null,
?int $connect_timeout = null
): bool {
if (!empty($socket_address)) {
$this->socket_address = $socket_address;
}
if (!empty($timeout)) {
$this->timeout = $timeout;
}
if (!empty($connect_timeout)) {
$this->connect_timeout = $connect_timeout;
}
return true;
}
public function connect(): bool
{
if ($this->isConnected()) {
return true;
}
$socket = @stream_socket_client(
$this->socket_address,
$errno,
$errstr,
$this->connect_timeout
);
if (is_resource($socket)) {
$this->socket = $socket;
stream_set_timeout($this->socket, $this->timeout);
} else {
$err = 'Could not connect to Redis server (' . $this->socket_address . ')';
if ($errstr) {
$err .= ': ' . $errstr;
}
throw new RedisException($err, $errno);
}
return true;
}
public function isConnected(): bool
{
return isset($this->socket) && is_resource($this->socket);
}
public function reconnect(): bool
{
$this->disconnect();
return $this->connect();
}
public function disconnect(): void
{
if (isset($this->socket) && is_resource($this->socket)) {
fclose($this->socket);
}
}
public function __destruct()
{
$this->disconnect();
}
/**
* @return array<string>
*/
public function getQueue(): array
{
return $this->queue;
}
/**
* @param string|int ...$args
*/
private function buildCommand(...$args): string
{
$rlen = count($args);
$output = '*' . $rlen . self::NL;
foreach ($args as $arg) {
$arg = strval($arg);
$output .= '$' . strlen($arg) . self::NL . $arg . self::NL;
}
return $output;
}
/**
* @param string|int ...$args
*/
public function mutli(...$args): bool
{
if (empty($args)) {
throw new RedisException('Too few arguments passed');
}
$command = $this->buildCommand(...$args);
$this->queue[] = $command;
return true;
}
/**
* @param array<string> $commands
*/
private function execute(array $commands = []): ?bool
{
if (count($commands) < 1) {
return null;
}
if (!$this->isConnected()) {
$this->connect();
}
$command = implode(self::NL, $commands) . self::NL;
if (isset($this->socket) && is_resource($this->socket)) {
fwrite($this->socket, $command);
}
return true;
}
/**
* @param string|int ...$args
* @return string|int|array<mixed>|null|bool
*/
public function run(...$args): string|int|array|null|bool
{
if (!$this->queue) {
if (empty($args)) {
throw new RedisException('Too few arguments passed');
}
// run single command
$command = $this->buildCommand(...$args);
$this->execute([$command]);
$response = $this->getResponse();
} else {
if (!empty($args)) {
$err = 'Cannot run a single command during transaction';
throw new \RuntimeException($err);
}
// run multiple commands in transaction
$multi = $this->buildCommand('MULTI');
array_unshift($this->queue, $multi);
$exec = $this->buildCommand('EXEC');
array_unshift($this->queue, $exec);
$num_commands = $this->execute($this->queue);
$this->queue = [];
$response = [];
for ($i = 0; $i < $num_commands; $i++) {
$response[] = $this->getResponse();
}
}
return $response;
}
/**
* @return string|int|array<mixed>|null|bool
*/
private function getResponse(): string|int|array|null|bool
{
if (!isset($this->socket) || !is_resource($this->socket)) {
return false;
}
$response = false;
$char = fgetc($this->socket);
if ($char === self::STRING) {
$response = $this->getResponseString();
} else if ($char === self::INT) {
$response = $this->getResponseInt();
} else if ($char === self::ERROR) {
$this->throwResponseError();
} else if ($char === self::BULK) {
$response = $this->getResponseBulk();
} else if ($char === self::MULTIBULK) {
$response = $this->getResponseMultibulk();
}
return $response;
}
private function getResponseString(): string
{
if (!isset($this->socket) || !is_resource($this->socket)) {
return '';
}
return trim(fgets($this->socket) ?: '');
}
private function getResponseInt(): int
{
return (int) $this->getResponseString();
}
private function throwResponseError(): void
{
$error = $this->getResponseString();
throw new RedisException($error);
}
private function getResponseBulk(): ?string
{
if (!isset($this->socket) || !is_resource($this->socket)) {
return null;
}
$response = $this->getResponseString();
if ($response === '-1') {
$response = null;
} else {
$bulk = null;
$read = 0;
if (strlen($response) > 1 && substr($response, 0, 1) === self::BULK) {
$size = intval(substr($response, 1));
} else {
$size = intval($response);
}
while ($read < $size) {
$diff = $size - $read;
/** @var int<0, max> */
$block_size = min($diff, 8192);
$chunk = fread($this->socket, $block_size);
if ($chunk !== false) {
$chunk_length = strlen($chunk);
$read += $chunk_length;
$bulk .= $chunk;
} else {
fseek($this->socket, $read);
}
}
fgets($this->socket);
$response = $bulk;
}
return $response;
}
/**
* @return array<string|int|array<mixed>|null|bool>|null
*/
private function getResponseMultibulk(): ?array
{
$num_responses = $this->getResponseString();
$response = false;
if ($num_responses === '-1') {
$response = null;
} else {
$response = [];
for ($i = 0; $i < $num_responses; $i++) {
$response[] = $this->getResponse();
}
}
return $response;
}
}