CacheRedis.php
6.86 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
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 14/11/23
* Time: 上午1:39
*/
namespace Hood\Cache;
use \Redis as Redis;
use Hood\Core\Root;
use Hood\Debug\DebugException;
class CacheRedis extends Root implements CacheInterface
{
private $redisInstances = array();
private $section = 'redis';
private $node = 'servers';
private $childNodes = 'hosts';
private $persistentIDs = array();
private $tagName = '';
private $prefix = '';
private $persistentID = 'hood.cache';
private $port = 6379;
/**
*
* Enter description here ...
* @var Redis
*/
private $redis;
private $timeout = 2.5;
public function __construct($prefix = '', $persistentID = 'hood.cache')
{
parent::__construct();
$this->prefix = $prefix;
$this->persistentIDs[] = $this->persistentID = $persistentID;
}
/**
* 设置共享连接ID
* @param $persistentID
* @return $this
*/
public function setPersistentID($persistentID)
{
$this->persistentID = $persistentID;
return $this;
}
/**
* @return Redis
* @throws DebugException
*/
final private function init()
{
if (isset($this->redisInstances[$this->persistentID])) {
$redis = $this->redisInstances[$this->persistentID];
} else {
$redis = new \Redis();
$server = $this->getServerHost('cache');
$_serverHosts = $server->getServerConfig($this->section, $this->node);
if (empty($_serverHosts)) {
throw new DebugException('redis node :' . $this->node . ' is null');
}
if (!isset($_serverHosts[$this->childNodes]) || empty($_serverHosts[$this->childNodes])) {
throw new DebugException('Redis 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 {
$redis->connect($host, $this->port, $this->timeout);
} catch (\RedisException $e) {
throw new DebugException('redis connetc error:' . $host . ':' . $this->port . ' ' . $e->getMessage());
}
$this->redisInstances[$this->persistentID] = $redis;
}
return $redis;
}
/**
* 设置子节点
* @param $childNode
* @return $this
*/
public function setChildNodes($childNode)
{
$this->childNodes = $childNode;
return $this;
}
/**
* 设置redis配置的块
* @param $section
* @return $this
*/
public function setSection($section)
{
$this->section = $section;
return $this;
}
/**
* 设置redis配置的块节点
* @param $node
* @return $this
*/
public function setNode($node = null)
{
if ($node != null) $this->node = $node;
return $this;
}
/**
* 组织host
* @param array $hosts
* @return array
*/
private function _makeHosts($hosts)
{
$servers = explode(',', $hosts);
$_server = array();
foreach ($servers as $val) {
$_server[] = explode(':', $val);
}
return $_server;
}
/**
*
* 返回key所关联的字符串值,如果key不存在则返回特殊值nil。
* @param String $key
* @return Mixed or nil
*/
public function get($key)
{
return $this->init()->get((string)$key);
}
/**
* 设置key-value并设置过期时间
* @param string $key
* @param string $val
* @param int $timeout 过期时间,单位:秒
* @return bool
* @throws DebugException
*/
public function set($key, $val, $timeout = 0)
{
return $this->init()->set((string)$key, $val, $timeout);
}
/**
*
* @param $key
* @param $ttl
* @param $value
* @return bool
* @throws DebugException
* @example $redis->setex('key', 3600, 'value');
*/
public function setex($key, $ttl, $value)
{
return $this->init()->setex($key, $ttl, $value);
}
/**
*
* 选择数据库
* @param int $db
* @return bool
*/
public function select($db = 9)
{
return $this->init()->select((int)$db);
}
/**
* 增量
* @param string $key
* @param string $value
* @throws DebugException
*/
public function increment($key, $value = 1)
{
return $this->init()->incrBy((string)$key, (int)$value);
}
/**
* 减量
* @param string $key
* @param int $value
* @throws DebugException
*/
public function decrement($key, $value = 1)
{
return $this->init()->decrBy((string)$key, (int)$value);
}
/**
* 删除
* @param $key
* @return int
* @throws DebugException
*/
public function delete($key)
{
return $this->init()->del((string)$key);
}
public function add($key, $value, $minutes)
{
}
public function tag($tagName)
{
}
/**
* 向list左压入
* @param string $key
* @param string $value
* @throws DebugException
*/
public function lpush($key, $value)
{
return $this->init()->lPush((string)$key, $value);
}
/**
* 从list左弹出
* @param string $key
* @throws DebugException
*/
public function lpop($key)
{
return $this->init()->lPop((string)$key);
}
/**
* 向list右压入
* @param string $key
* @param string $value
* @throws DebugException
*/
public function rpush($key, $value)
{
return $this->init()->rPush((string)$key, $value);
}
/**
* 从list右弹出
* @param string $key
* @throws DebugException
*/
public function rpop($key)
{
return $this->init()->rPop((string)$key);
}
/**
* 检查一个key是否存在
* @param string $key
* @return int
*/
public function exists($key)
{
return $this->init()->exists((string)$key);
}
/**
* 设置过期时间
* @param string $key
* @param int $second
* @return int
*/
public function expire($key, $second)
{
return $this->init()->expire((string)$key, (int)$second);
}
/**
* 获取多个,返回数组按传入参数的顺序,不存在的,返回false
* @return array
*/
public function mget()
{
$keys = func_get_args();
if (empty($keys)) {
return array();
}
return $this->init()->mGet($keys);
}
}