-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.php
60 lines (53 loc) · 1.53 KB
/
client.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
<?php
/**
* Created by PhpStorm.
* User: peiman
* Date: 11/17/18
* Time: 10:10 PM
*/
class RedisClient
{
private $server;
private $socket;
public function __construct($server = 'localhost:6379')
{
$this->server = $server;
}
public function __call($method, array $args)
{
array_unshift($args, $method);
$cmd = '*' . count($args) . "\r\n";
foreach ($args as $item) {
$cmd .= '$' . strlen($item) . "\r\n" . $item . "\r\n";
}
fwrite($this->getSocket(), $cmd);
return $this->parseResponse();
}
private function getSocket()
{
return $this->socket
? $this->socket
: ($this->socket = stream_socket_client($this->server));
}
private function parseResponse()
{
$line = fgets($this->getSocket());
list($type, $result) = array($line[0], substr($line, 1, strlen($line) - 3));
if ($type == '-') { // error message
throw new Exception($result);
} elseif ($type == '$') { // bulk reply
if ($result == -1) {
$result = null;
} else {
$line = fread($this->getSocket(), $result + 2);
$result = substr($line, 0, strlen($line) - 2);
}
} elseif ($type == '*') { // multi-bulk reply
$count = ( int ) $result;
for ($i = 0, $result = array(); $i < $count; $i++) {
$result[] = $this->parseResponse();
}
}
return $result;
}
}