Showing
1 changed file
with
345 additions
and
0 deletions
Hood/Cache/CacheSSDB.php
0 → 100644
1 | +<?php | ||
2 | +/** | ||
3 | + * Created by PhpStorm. | ||
4 | + * User: shin | ||
5 | + * Date: 15/10/19 | ||
6 | + * Time: 下午3:12 | ||
7 | + */ | ||
8 | +namespace Hood\Cache; | ||
9 | + | ||
10 | +use Hood\Core\Root; | ||
11 | +use Hood\Cache\Ssdb; | ||
12 | +use Hood\Debug\DebugException; | ||
13 | +use Hood\Cache\Ssdb\SSDBException; | ||
14 | + | ||
15 | +class CacheSSDB extends Root implements CacheInterface | ||
16 | +{ | ||
17 | + | ||
18 | + private $instances = array(); | ||
19 | + | ||
20 | + private $section = 'ssdb'; | ||
21 | + | ||
22 | + private $node = 'servers'; | ||
23 | + | ||
24 | + private $childNodes = 'hosts'; | ||
25 | + | ||
26 | + private $port = 8888; | ||
27 | + | ||
28 | + private $timeout = 5000;//毫秒 | ||
29 | + | ||
30 | + private $persistentID = 'hood.ssdb'; | ||
31 | + | ||
32 | + public function __construct($prefix = '', $persistentID = 'hood.ssdb') | ||
33 | + { | ||
34 | + parent::__construct(); | ||
35 | + $this->persistentID = $persistentID; | ||
36 | + } | ||
37 | + | ||
38 | + | ||
39 | + /** | ||
40 | + * 设置子节点 | ||
41 | + * @param $childNode | ||
42 | + * @return $this | ||
43 | + */ | ||
44 | + public function setChildNodes($childNode) | ||
45 | + { | ||
46 | + $this->childNodes = $childNode; | ||
47 | + return $this; | ||
48 | + } | ||
49 | + | ||
50 | + | ||
51 | + /** | ||
52 | + * 设置配置的块 | ||
53 | + * @param $section | ||
54 | + * @return $this | ||
55 | + */ | ||
56 | + public function setSection($section) | ||
57 | + { | ||
58 | + $this->section = $section; | ||
59 | + return $this; | ||
60 | + } | ||
61 | + | ||
62 | + /** | ||
63 | + * 设置配置的块节点 | ||
64 | + * @param $node | ||
65 | + * @return $this | ||
66 | + */ | ||
67 | + public function setNode($node = null) | ||
68 | + { | ||
69 | + if ($node != null) $this->node = $node; | ||
70 | + return $this; | ||
71 | + } | ||
72 | + | ||
73 | + | ||
74 | + /** | ||
75 | + * 设置共享连接ID | ||
76 | + * @param $persistentID | ||
77 | + * @return $this | ||
78 | + */ | ||
79 | + public function setPersistentID($persistentID) | ||
80 | + { | ||
81 | + $this->persistentID = $persistentID; | ||
82 | + return $this; | ||
83 | + } | ||
84 | + | ||
85 | + | ||
86 | + /** | ||
87 | + * 指定连接id删除 | ||
88 | + * @param $persistentID | ||
89 | + * @return $this | ||
90 | + */ | ||
91 | + public function delInstances($persistentID) | ||
92 | + { | ||
93 | + if (isset($this->instances[$persistentID])) { | ||
94 | + unset($this->instances[$persistentID]); | ||
95 | + } | ||
96 | + return $this; | ||
97 | + } | ||
98 | + | ||
99 | + | ||
100 | + public function init() | ||
101 | + { | ||
102 | + if (isset($this->instances[$this->persistentID])) { | ||
103 | + return $this->instances[$this->persistentID]; | ||
104 | + } else { | ||
105 | + $server = $this->getServerHost('cache'); | ||
106 | + $_serverHosts = $server->getServerConfig($this->section, $this->node); | ||
107 | + if (empty($_serverHosts)) { | ||
108 | + throw new DebugException('ssdb node :' . $this->node . ' is null'); | ||
109 | + } | ||
110 | + if (!isset($_serverHosts[$this->childNodes]) || empty($_serverHosts[$this->childNodes])) { | ||
111 | + throw new DebugException('ssdb Host:' . $this->childNodes . ' is Null.'); | ||
112 | + } | ||
113 | + | ||
114 | + $allServerAndPort = $this->_makeHosts($_serverHosts[$this->childNodes]); | ||
115 | + $serverAndPort = $allServerAndPort[rand(0, count($allServerAndPort) - 1)]; | ||
116 | + $host = $serverAndPort[0]; | ||
117 | + if (isset($serverAndPort[1]) && !empty($serverAndPort[1])) { | ||
118 | + $this->port = $serverAndPort[1]; | ||
119 | + } | ||
120 | + try { | ||
121 | + $ssdb = new Ssdb\SimpleSSDB($host, $this->port, $this->timeout); | ||
122 | + } catch (SSDBException $e) { | ||
123 | + throw new DebugException('ssdb connetc error:' . $host . ':' . $this->port . ' ' . $e->getMessage()); | ||
124 | + } | ||
125 | + $this->instances[$this->persistentID] = $ssdb; | ||
126 | + return $ssdb; | ||
127 | + } | ||
128 | + } | ||
129 | + | ||
130 | + /** | ||
131 | + * 组织host | ||
132 | + * @param array $hosts | ||
133 | + * @return array | ||
134 | + */ | ||
135 | + private function _makeHosts($hosts) | ||
136 | + { | ||
137 | + $servers = explode(',', $hosts); | ||
138 | + $_server = array(); | ||
139 | + foreach ($servers as $val) { | ||
140 | + $_server[] = explode(':', $val); | ||
141 | + } | ||
142 | + return $_server; | ||
143 | + } | ||
144 | + | ||
145 | + | ||
146 | + /** | ||
147 | + * @param $key | ||
148 | + * @return mixed | ||
149 | + */ | ||
150 | + public function get($key) | ||
151 | + { | ||
152 | + return $this->init()->get($key); | ||
153 | + } | ||
154 | + | ||
155 | + | ||
156 | + /** | ||
157 | + * | ||
158 | + * @param $key | ||
159 | + * @param $value | ||
160 | + * @param $second | ||
161 | + * @return bool | ||
162 | + * @throws DebugException | ||
163 | + */ | ||
164 | + public function add($key, $value, $second) | ||
165 | + { | ||
166 | + $result = $this->init()->setnx($key, $value); | ||
167 | + if ($result == 1) { | ||
168 | + $this->expire($key, $second); | ||
169 | + return true; | ||
170 | + } else { | ||
171 | + return false; | ||
172 | + } | ||
173 | + } | ||
174 | + | ||
175 | + /** | ||
176 | + * 设置过期时间,秒 | ||
177 | + * @param $key | ||
178 | + * @param $ttl | ||
179 | + * @throws DebugException | ||
180 | + */ | ||
181 | + public function expire($key, $ttl) | ||
182 | + { | ||
183 | + return $this->init()->expire($key, $ttl); | ||
184 | + } | ||
185 | + | ||
186 | + /** | ||
187 | + * @param $key | ||
188 | + * @param $value | ||
189 | + * @param $minutes | ||
190 | + * @return mixed | ||
191 | + */ | ||
192 | + public function set($key, $value, $second) | ||
193 | + { | ||
194 | + return $this->init()->setx($key, $value, $second); | ||
195 | + } | ||
196 | + | ||
197 | + /** | ||
198 | + * @param $key | ||
199 | + * @param int $value | ||
200 | + * @return mixed | ||
201 | + */ | ||
202 | + public function increment($key, $value = 1) | ||
203 | + { | ||
204 | + | ||
205 | + } | ||
206 | + | ||
207 | + /** | ||
208 | + * @param $key | ||
209 | + * @param int $value | ||
210 | + * @return mixed | ||
211 | + */ | ||
212 | + public function decrement($key, $value = 1) | ||
213 | + { | ||
214 | + | ||
215 | + } | ||
216 | + | ||
217 | + /** | ||
218 | + * @param $key | ||
219 | + * @return mixed | ||
220 | + */ | ||
221 | + public function delete($key) | ||
222 | + { | ||
223 | + return $this->init()->del($key); | ||
224 | + } | ||
225 | + | ||
226 | + /** | ||
227 | + * @param $tagName | ||
228 | + * @return $this | ||
229 | + */ | ||
230 | + public function tag($tagName) | ||
231 | + { | ||
232 | + | ||
233 | + } | ||
234 | + | ||
235 | + | ||
236 | + /** | ||
237 | + * zset类型添加 | ||
238 | + * @param $zname | ||
239 | + * @param $zkey | ||
240 | + * @param $score | ||
241 | + * @return mixed | ||
242 | + * @throws DebugException | ||
243 | + */ | ||
244 | + public function zset($zname, $zkey, $score) | ||
245 | + { | ||
246 | + return $this->init()->zset($zname, $zkey, $score); | ||
247 | + } | ||
248 | + | ||
249 | + | ||
250 | + /** | ||
251 | + * 将zset里的东西列表 | ||
252 | + * @param $name_start | ||
253 | + * @param $name_end | ||
254 | + * @param $limit | ||
255 | + * @throws DebugException | ||
256 | + */ | ||
257 | + public function zlist($name_start, $name_end, $limit) | ||
258 | + { | ||
259 | + $this->init()->zlist($name_start, $name_end, $limit); | ||
260 | + } | ||
261 | + | ||
262 | + | ||
263 | + /** | ||
264 | + * 返回 zset 中的元素个数. | ||
265 | + * @param $zname | ||
266 | + * @return mixed | ||
267 | + * @throws DebugException | ||
268 | + */ | ||
269 | + public function zsize($zname) | ||
270 | + { | ||
271 | + return $this->init()->zsize($zname); | ||
272 | + } | ||
273 | + | ||
274 | + | ||
275 | + /** | ||
276 | + * 删除权重处于区间 [start,end] 的元素.包含边界 | ||
277 | + * @param $zname | ||
278 | + * @param $score_start | ||
279 | + * @param $score_end | ||
280 | + */ | ||
281 | + public function zremrangebyscore($zname, $score_start, $score_end) | ||
282 | + { | ||
283 | + return $this->init()->zremrangebyscore($zname, $score_start, $score_end); | ||
284 | + } | ||
285 | + | ||
286 | + | ||
287 | + /** | ||
288 | + * 从 zset 首部删除并返回 `limit` 个元素. | ||
289 | + * @param $zname | ||
290 | + * @param $limit | ||
291 | + * @return mixed | ||
292 | + * @throws DebugException | ||
293 | + */ | ||
294 | + public function zpop_front($zname, $limit) | ||
295 | + { | ||
296 | + return $this->init()->zpop_front($zname, $limit); | ||
297 | + } | ||
298 | + | ||
299 | + | ||
300 | + /** | ||
301 | + * 从 zset 尾部删除并返回 `limit` 个元素. | ||
302 | + * @param $zname | ||
303 | + * @param $limit | ||
304 | + * @return mixed | ||
305 | + * @throws DebugException | ||
306 | + */ | ||
307 | + public function zpop_back($zname, $limit) | ||
308 | + { | ||
309 | + return $this->init()->zpop_back($zname, $limit); | ||
310 | + } | ||
311 | + | ||
312 | + | ||
313 | + /** | ||
314 | + * 根据下标索引区间 [offset, offset + limit) 获取 key-score 对, 下标从 0 开始 | ||
315 | + * @param $zname | ||
316 | + * @param $offset | ||
317 | + * @param $limist | ||
318 | + * @return mixed | ||
319 | + * @throws DebugException | ||
320 | + */ | ||
321 | + public function zrange($zname, $offset, $limist) | ||
322 | + { | ||
323 | + return $this->init()->zrange($zname, $offset, $limist); | ||
324 | + } | ||
325 | + | ||
326 | + | ||
327 | + /** | ||
328 | + * 根据下标索引区间 [offset, offset + limit) 获取 key-score 对, 下标从 0 开始 zrrange 是反向顺序获取. | ||
329 | + * @param $zname | ||
330 | + * @param $offset | ||
331 | + * @param $limist | ||
332 | + * @return mixed | ||
333 | + * @throws DebugException | ||
334 | + */ | ||
335 | + public function zrrange($zname, $offset, $limist) | ||
336 | + { | ||
337 | + return $this->init()->zrrange($zname, $offset, $limist); | ||
338 | + } | ||
339 | +} | ||
340 | + | ||
341 | + | ||
342 | + | ||
343 | + | ||
344 | + | ||
345 | + |
-
Please register or login to post a comment