/
redis.php
163 lines (145 loc) · 3.99 KB
/
redis.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
*
* Simple PHP class for Redis (stand alone version)
* Adapted from Redis.php by Ludovico Magnocavallo. (http://redis.io/topics/twitter-clone)
* @author Mark Headd
*
*/
class Redis {
// Private class members.
private $server;
private $port;
private $sock;
private $auth;
/**
*
* Class consrtuctor
* @param string $host
* @param int $port
*/
function __construct($host='localhost', $port=6379) {
$this->host = $host;
$this->port = $port;
}
/**
*
* Connect to a Redis server.
*/
public function connect() {
if ($this->sock)
return;
if ($sock = fsockopen($this->host, $this->port, $errno, $errstr)) {
$this->sock = $sock;
return;
}
$msg = "Cannot open socket to {$this->host}:{$this->port}";
if ($errno || $errmsg) {
$msg .= "," . ($errno ? " error $errno" : "") . ($errmsg ? " $errmsg" : "");
}
trigger_error("$msg.", E_USER_ERROR);
}
/**
*
* Set authentication credentials
* @param string $password
*/
public function auth($password) {
$this->auth = $password;
}
/**
*
* Disconnect from a Redis server.
*/
public function disconnect() {
if ($this->sock)
@fclose($this->sock);
$this->sock = null;
}
/**
*
* Publish a message on a channel.
* @param string $channel
* @param string $message
*/
public function publish($channel, $message) {
// Connect to the server.
$this->connect();
// Authenticate.
if($this->auth) {
$this->doAuth($this->auth);
}
// Publish message.
$this->write("PUBLISH $channel $message\r\n");
return $this->get_response();
}
/**
*
* Private methods.
*
*/
private function doAuth($auth) {
$this->write("AUTH $auth\r\n");
}
private function write($s) {
while ($s) {
$i = fwrite($this->sock, $s);
if ($i == 0) // || $i == strlen($s))
break;
$s = substr($s, $i);
}
}
private function read($len=1024) {
if ($s = fgets($this->sock))
return $s;
$this->disconnect();
trigger_error("Cannot read from socket.", E_USER_ERROR);
}
private function get_response() {
$data = trim($this->read());
$c = $data[0];
$data = substr($data, 1);
switch ($c) {
case '-':
trigger_error(substr($data, 0, 4) == 'ERR ' ? substr($data, 4) : $data, E_USER_ERROR);
break;
case '+':
return $data;
case '*':
$num = (int)$data;
if ((string)$num != $data)
trigger_error("Cannot convert multi-response header '$data' to integer", E_USER_ERROR);
$result = array();
for ($i=0; $i<$num; $i++)
$result[] =& $this-get_value();
return $result;
default:
return $this->get_value($c . $data);
}
}
private function get_value($data=null) {
if ($data === null)
$data =& trim($this->read());
if ($data == '$-1')
return null;
$c = $data[0];
$data = substr($data, 1);
$i = strpos($data, '.') !== false ? (int)$data : (float)$data;
if ((string)$i != $data)
trigger_error("Cannot convert data '$c$data' to integer", E_USER_ERROR);
if ($c == ':')
return $i;
if ($c != '$')
trigger_error("Unkown response prefix for '$c$data'", E_USER_ERROR);
$buffer = '';
while (true) {
$data =& $this->read();
$i -= strlen($data);
$buffer .= $data;
if ($i < 0)
break;
}
return substr($buffer, 0, -2);
}
}
?>