CacheSSDB.php
7.28 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/**
* Created by PhpStorm.
* User: shin
* Date: 15/10/19
* Time: 下午3:12
*/
namespace Hood\Cache;
use Hood\Core\Root;
use Hood\Cache\Ssdb;
use Hood\Debug\DebugException;
use Hood\Cache\Ssdb\SSDBException;
class CacheSSDB extends Root implements CacheInterface
{
private $instances = array();
private $section = 'ssdb';
private $node = 'servers';
private $childNodes = 'hosts';
private $port = 8888;
private $timeout = 5000;//毫秒
private $persistentID = 'hood.ssdb';
public function __construct($prefix = '', $persistentID = 'hood.ssdb')
{
parent::__construct();
$this->persistentID = $persistentID;
}
/**
* 设置子节点
* @param $childNode
* @return $this
*/
public function setChildNodes($childNode)
{
$this->childNodes = $childNode;
return $this;
}
/**
* 设置配置的块
* @param $section
* @return $this
*/
public function setSection($section)
{
$this->section = $section;
return $this;
}
/**
* 设置配置的块节点
* @param $node
* @return $this
*/
public function setNode($node = null)
{
if ($node != null) $this->node = $node;
return $this;
}
/**
* 设置共享连接ID
* @param $persistentID
* @return $this
*/
public function setPersistentID($persistentID)
{
$this->persistentID = $persistentID;
return $this;
}
/**
* 指定连接id删除
* @param $persistentID
* @return $this
*/
public function delInstances($persistentID)
{
if (isset($this->instances[$persistentID])) {
unset($this->instances[$persistentID]);
}
return $this;
}
public function init()
{
if (isset($this->instances[$this->persistentID])) {
return $this->instances[$this->persistentID];
} else {
$server = $this->getServerHost('cache');
$_serverHosts = $server->getServerConfig($this->section, $this->node);
if (empty($_serverHosts)) {
throw new DebugException('ssdb node :' . $this->node . ' is null');
}
if (!isset($_serverHosts[$this->childNodes]) || empty($_serverHosts[$this->childNodes])) {
throw new DebugException('ssdb Host:' . $this->childNodes . ' is Null.');
}
$allServerAndPort = $this->_makeHosts($_serverHosts[$this->childNodes]);
$serverAndPort = $allServerAndPort[rand(0, count($allServerAndPort) - 1)];
$host = $serverAndPort[0];
if (isset($serverAndPort[1]) && !empty($serverAndPort[1])) {
$this->port = $serverAndPort[1];
}
try {
$ssdb = new Ssdb\SimpleSSDB($host, $this->port, $this->timeout);
} catch (SSDBException $e) {
throw new DebugException('ssdb connetc error:' . $host . ':' . $this->port . ' ' . $e->getMessage());
}
$this->instances[$this->persistentID] = $ssdb;
return $ssdb;
}
}
/**
* 组织host
* @param array $hosts
* @return array
*/
private function _makeHosts($hosts)
{
$servers = explode(',', $hosts);
$_server = array();
foreach ($servers as $val) {
$_server[] = explode(':', $val);
}
return $_server;
}
/**
* @param $key
* @return mixed
*/
public function get($key)
{
return $this->init()->get($key);
}
/**
*
* @param $key
* @param $value
* @param $second
* @return bool
* @throws DebugException
*/
public function add($key, $value, $second)
{
$result = $this->init()->setnx($key, $value);
if ($result == 1) {
$this->expire($key, $second);
return true;
} else {
return false;
}
}
/**
* 设置过期时间,秒
* @param $key
* @param $ttl
* @throws DebugException
*/
public function expire($key, $ttl)
{
return $this->init()->expire($key, $ttl);
}
/**
* @param $key
* @param $value
* @param $minutes
* @return mixed
*/
public function set($key, $value, $second)
{
return $this->init()->setx($key, $value, $second);
}
/**
* @param $key
* @param int $value
* @return mixed
*/
public function increment($key, $value = 1)
{
}
/**
* @param $key
* @param int $value
* @return mixed
*/
public function decrement($key, $value = 1)
{
}
/**
* @param $key
* @return mixed
*/
public function delete($key)
{
return $this->init()->del($key);
}
/**
* @param $tagName
* @return $this
*/
public function tag($tagName)
{
}
/**
* zset类型添加
* @param $zname
* @param $zkey
* @param $score
* @return mixed
* @throws DebugException
*/
public function zset($zname, $zkey, $score)
{
return $this->init()->zset($zname, $zkey, $score);
}
/**
* 将zset里的东西列表
* @param $name_start
* @param $name_end
* @param $limit
* @throws DebugException
*/
public function zlist($name_start, $name_end, $limit)
{
$this->init()->zlist($name_start, $name_end, $limit);
}
/**
* 返回 zset 中的元素个数.
* @param $zname
* @return mixed
* @throws DebugException
*/
public function zsize($zname)
{
return $this->init()->zsize($zname);
}
/**
* 删除权重处于区间 [start,end] 的元素.包含边界
* @param $zname
* @param $score_start
* @param $score_end
*/
public function zremrangebyscore($zname, $score_start, $score_end)
{
return $this->init()->zremrangebyscore($zname, $score_start, $score_end);
}
/**
* 从 zset 首部删除并返回 `limit` 个元素.
* @param $zname
* @param $limit
* @return mixed
* @throws DebugException
*/
public function zpop_front($zname, $limit)
{
return $this->init()->zpop_front($zname, $limit);
}
/**
* 从 zset 尾部删除并返回 `limit` 个元素.
* @param $zname
* @param $limit
* @return mixed
* @throws DebugException
*/
public function zpop_back($zname, $limit)
{
return $this->init()->zpop_back($zname, $limit);
}
/**
* 根据下标索引区间 [offset, offset + limit) 获取 key-score 对, 下标从 0 开始
* @param $zname
* @param $offset
* @param $limist
* @return mixed
* @throws DebugException
*/
public function zrange($zname, $offset, $limist)
{
return $this->init()->zrange($zname, $offset, $limist);
}
/**
* 根据下标索引区间 [offset, offset + limit) 获取 key-score 对, 下标从 0 开始 zrrange 是反向顺序获取.
* @param $zname
* @param $offset
* @param $limist
* @return mixed
* @throws DebugException
*/
public function zrrange($zname, $offset, $limist)
{
return $this->init()->zrrange($zname, $offset, $limist);
}
}