-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQredis.php
100 lines (91 loc) · 2.77 KB
/
Qredis.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
<?php
namespace v10086;
class Qredis {
static $socket = null;
//连接
public static function conn($host='192.168.139.128',$port=6379) {
!self::$socket && self::$socket = fsockopen ($host, $port,$errno, $errmsg );
if ($errno || $errmsg)
throw new \Exception($errno.'_'.$errmsg);
}
//执行命令并返回响应
public static function exec($command) {
self::reqWrite($command);
return self::repReadAndParse();
}
//请求信息解释
public static function reqParse($command){
if (is_array($command)){
$s = '*'.count($command)."\r\n";
foreach ($command as $m){
$s.='$'.strlen($m)."\r\n";
$s.=$m."\r\n";
}
}else{
$s = $command . "\r\n";
}
return $s;
}
//请求信息写入
public static function reqWrite($command){
$s = self::reqParse($command);
while ($s) {
$i = fwrite (self::$socket,$s);
if ($i == 0)
break;
$s = substr($s,$i);
}
}
//响应信息读取
public static function repRead(){
$s = fgets(self::$socket);
if(!$s){
self::$socket && @fclose (self::$socket) && self::$socket=null;
throw new \Exception ( "无法读取响应socket." );
}
return trim($s);
}
//响应信息读取并解析
public static function repReadAndParse(){
$s = self::repRead();
switch ($s [0]) {
case '-' :
throw new \Exception(substr($s,1));
case '+' :
return substr($s,1);
case ':' :
return substr($s,1)+0;
case '$' :
$i = (int)(substr ($s,1));
if ($i == - 1){
return null;
}
$buffer = '';
if ($i == 0){
$s = self::repRead();
}
while ( $i > 0 ) {
$s = self::repRead();
$l = strlen($s);
$i -= $l;
if ($i < 0){
$s = substr($s,0,$i);
}
$buffer .= $s;
}
return $buffer;
case '*' :
$i = (int)(substr($s,1));
if ($i == - 1){
return null;
}
$res = [];
for($c = 0; $c < $i; $c ++) {
$res[] = self::repReadAndParse();
}
return $res;
default :
throw new \Exception ('无法解析响应信息:'.$s );
}
}
}