Repository navigation
Expand file tree
/
Copy pathServerRedis.php
More file actions
177 lines (149 loc) · 4.42 KB
/
Copy pathServerRedis.php
File metadata and controls
177 lines (149 loc) · 4.42 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
<?php
namespace MyQEE\Server;
use \Swoole\Redis\Server as RedisServer;
/**
* 服务器对象
*
* 主端口可同时支持 WebSocket, Http 协议, 并可以额外监听TCP新端口
*
* @package MyQEE\Server
*/
class ServerRedis extends Server
{
public function bind()
{
parent::bind();
# Redis
if ($this->serverType === 4)
{
foreach (get_class_methods($this) as $method)
{
if (substr($method, 0, 5) === 'bind_')
{
$act = substr($method, 5);
# 批量绑定
$this->server->setHandler($act, [$this, $method]);
}
}
}
}
public function bind_GET($fd, $data)
{
if (count($data) == 0)
{
return RedisServer::format(RedisServer::ERROR, "ERR wrong number of arguments for 'GET' command");
}
$rs = $this->worker->get($data[0]);
if (empty($rs))
{
return RedisServer::format(RedisServer::NIL);
}
elseif (is_int($rs))
{
return RedisServer::format(RedisServer::INT, $rs);
}
else
{
return RedisServer::format(RedisServer::STRING, $rs);
}
}
public function bind_SET($fd, $data)
{
if (count($data) < 2)
{
return RedisServer::format(RedisServer::ERROR, "ERR wrong number of arguments for 'SET' command");
}
$key = $data[0];
$value = $data[1];
$rs = $this->worker->set($key, $value);
if ($rs)
{
return RedisServer::format(RedisServer::STATUS, 'OK');
}
else
{
return RedisServer::format(RedisServer::ERROR, "ERR set fail");
}
}
public function bind_sAdd($fd, $data)
{
if (count($data) < 2)
{
return RedisServer::format(RedisServer::ERROR, "ERR wrong number of arguments for 'sAdd' command");
}
$key = array_shift($data);
$count = $this->worker->sAdd($key, $data);
if (false === $count)
{
return RedisServer::format(RedisServer::ERROR, "ERR get sMembers");
}
return RedisServer::format(RedisServer::INT, $count);
}
public function bind_sMembers($fd, $data)
{
if (count($data) < 1)
{
return RedisServer::format(RedisServer::ERROR, "ERR wrong number of arguments for 'sMembers' command");
}
$rs = $this->worker->sMembers($data[0]);
if (false === $rs)
{
return RedisServer::format(RedisServer::ERROR, " sMembers fail");
}
if (!$rs)
{
return RedisServer::format(RedisServer::NIL);
}
return RedisServer::format(RedisServer::SET, $rs);
}
public function bind_hSet($fd, $data)
{
if (count($data) < 3)
{
return RedisServer::format(RedisServer::ERROR, "ERR wrong number of arguments for 'hSet' command");
}
$rs = $this->worker->hSet($data[0], $data[1], $data[2]);
if (false === $rs)
{
return RedisServer::format(RedisServer::ERROR, "ERR hSet fail");
}
return RedisServer::format(RedisServer::INT, $rs);
}
public function bind_hGet($fd, $data)
{
if (count($data) < 2)
{
return RedisServer::format(RedisServer::ERROR, "ERR wrong number of arguments for 'hSet' command");
}
$rs = $this->worker->hGet($data[0], $data[1]);
if (false === $rs)
{
return RedisServer::format(RedisServer::ERROR, "ERR hSet fail");
}
if (empty($rs))
{
return RedisServer::format(RedisServer::NIL);
}
elseif (is_int($rs))
{
return RedisServer::format(RedisServer::INT, $rs);
}
else
{
return RedisServer::format(RedisServer::STRING, $rs);
}
}
public function bind_hGetAll($fd, $data)
{
if (count($data) < 1)
{
return RedisServer::format(RedisServer::ERROR, "ERR wrong number of arguments for 'hGetAll' command");
}
$rs = $this->worker->hGetAll($data[0]);
if (false === $rs)
{
return RedisServer::format(RedisServer::ERROR, "ERR hGetAll fail");
}
return RedisServer::format(RedisServer::MAP, $rs);
}
}