| Total Complexity | 166 |
| Total Lines | 1365 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like queue often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use queue, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class queue |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * redis链接标识符号 |
||
| 25 | */ |
||
| 26 | protected static $redis = NULL; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * redis配置数组 |
||
| 30 | */ |
||
| 31 | protected static $configs = array(); |
||
| 32 | private static $links = array(); |
||
| 33 | private static $link_name = 'default'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * 默认redis前缀 |
||
| 37 | */ |
||
| 38 | public static $prefix = 'phpspider'; |
||
| 39 | |||
| 40 | public static $error = ''; |
||
| 41 | |||
| 42 | public static function init() |
||
| 43 | { |
||
| 44 | if ( ! extension_loaded('redis')) |
||
| 45 | { |
||
| 46 | self::$error = 'The redis extension was not found'; |
||
| 47 | return false; |
||
| 48 | } |
||
| 49 | |||
| 50 | // 获取配置 |
||
| 51 | $config = self::$link_name == 'default' ? self::_get_default_config() : self::$configs[self::$link_name]; |
||
| 52 | |||
| 53 | // 如果当前链接标识符为空,或者ping不同,就close之后重新打开 |
||
| 54 | //if ( empty(self::$links[self::$link_name]) || !self::ping() ) |
||
| 55 | if (empty(self::$links[self::$link_name])) |
||
| 56 | { |
||
| 57 | self::$links[self::$link_name] = new Redis(); |
||
| 58 | if (strstr($config['host'], '.sock')) |
||
| 59 | { |
||
| 60 | if ( ! self::$links[self::$link_name]->connect($config['host'])) |
||
| 61 | { |
||
| 62 | self::$error = 'Unable to connect to redis server'; |
||
| 63 | unset(self::$links[self::$link_name]); |
||
| 64 | return false; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | else |
||
| 68 | { |
||
| 69 | if ( ! self::$links[self::$link_name]->connect($config['host'], $config['port'], $config['timeout'])) |
||
| 70 | { |
||
| 71 | self::$error = 'Unable to connect to redis server'; |
||
| 72 | unset(self::$links[self::$link_name]); |
||
| 73 | return false; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | // 验证 |
||
| 78 | if ($config['pass']) |
||
| 79 | { |
||
| 80 | if ( ! self::$links[self::$link_name]->auth($config['pass'])) |
||
| 81 | { |
||
| 82 | self::$error = 'Redis Server authentication failed'; |
||
| 83 | unset(self::$links[self::$link_name]); |
||
| 84 | return false; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | $prefix = empty($config['prefix']) ? self::$prefix : $config['prefix']; |
||
| 89 | self::$links[self::$link_name]->setOption(Redis::OPT_PREFIX, $prefix.':'); |
||
| 90 | // 永不超时 |
||
| 91 | // ini_set('default_socket_timeout', -1); 无效,要用下面的做法 |
||
| 92 | self::$links[self::$link_name]->setOption(Redis::OPT_READ_TIMEOUT, -1); |
||
| 93 | self::$links[self::$link_name]->select($config['db']); |
||
| 94 | } |
||
| 95 | |||
| 96 | return self::$links[self::$link_name]; |
||
| 97 | } |
||
| 98 | |||
| 99 | public static function clear_link() |
||
| 100 | { |
||
| 101 | if(self::$links) |
||
|
|
|||
| 102 | { |
||
| 103 | foreach(self::$links as $k=>$v) |
||
| 104 | { |
||
| 105 | $v->close(); |
||
| 106 | unset(self::$links[$k]); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | public static function set_connect($link_name, $config = array()) |
||
| 112 | { |
||
| 113 | self::$link_name = $link_name; |
||
| 114 | if (!empty($config)) |
||
| 115 | { |
||
| 116 | self::$configs[self::$link_name] = $config; |
||
| 117 | } |
||
| 118 | else |
||
| 119 | { |
||
| 120 | if (empty(self::$configs[self::$link_name])) |
||
| 121 | { |
||
| 122 | throw new Exception('You not set a config array for connect!'); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | //print_r(self::$configs); |
||
| 126 | |||
| 127 | //// 先断开原来的连接 |
||
| 128 | //if ( !empty(self::$links[self::$link_name]) ) |
||
| 129 | //{ |
||
| 130 | //self::$links[self::$link_name]->close(); |
||
| 131 | //self::$links[self::$link_name] = null; |
||
| 132 | //} |
||
| 133 | } |
||
| 134 | |||
| 135 | public static function set_connect_default() |
||
| 136 | { |
||
| 137 | $config = self::_get_default_config(); |
||
| 138 | self::set_connect('default', $config); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * 获取默认配置 |
||
| 143 | */ |
||
| 144 | protected static function _get_default_config() |
||
| 145 | { |
||
| 146 | if (empty(self::$configs['default'])) |
||
| 147 | { |
||
| 148 | if (!is_array($GLOBALS['config']['redis'])) |
||
| 149 | { |
||
| 150 | exit('cls_redis.php _get_default_config()' . '没有redis配置'); |
||
| 151 | // You not set a config array for connect\nPlease check the configuration file config/inc_config.php |
||
| 152 | } |
||
| 153 | self::$configs['default'] = $GLOBALS['config']['redis']; |
||
| 154 | } |
||
| 155 | return self::$configs['default']; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * set |
||
| 160 | * |
||
| 161 | * @param mixed $key 键 |
||
| 162 | * @param mixed $value 值 |
||
| 163 | * @param int $expire 过期时间,单位:秒 |
||
| 164 | * @return void |
||
| 165 | * @author seatle <[email protected]> |
||
| 166 | * @created time :2015-12-13 01:05 |
||
| 167 | */ |
||
| 168 | public static function set($key, $value, $expire = 0) |
||
| 169 | { |
||
| 170 | self::init(); |
||
| 171 | try |
||
| 172 | { |
||
| 173 | if ( self::$links[self::$link_name] ) |
||
| 174 | { |
||
| 175 | if ($expire > 0) |
||
| 176 | { |
||
| 177 | return self::$links[self::$link_name]->setex($key, $expire, $value); |
||
| 178 | } |
||
| 179 | else |
||
| 180 | { |
||
| 181 | return self::$links[self::$link_name]->set($key, $value); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | catch (Exception $e) |
||
| 186 | { |
||
| 187 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 188 | log::warn($msg); |
||
| 189 | if ($e->getCode() == 0) |
||
| 190 | { |
||
| 191 | self::$links[self::$link_name]->close(); |
||
| 192 | self::$links[self::$link_name] = null; |
||
| 193 | usleep(100000); |
||
| 194 | return self::set($key, $value, $expire); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | return NULL; |
||
| 198 | } |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * set |
||
| 203 | * |
||
| 204 | * @param mixed $key 键 |
||
| 205 | * @param mixed $value 值 |
||
| 206 | * @param int $expire 过期时间,单位:秒 |
||
| 207 | * @return void |
||
| 208 | * @author seatle <[email protected]> |
||
| 209 | * @created time :2015-12-13 01:05 |
||
| 210 | */ |
||
| 211 | public static function setnx($key, $value, $expire = 0) |
||
| 212 | { |
||
| 213 | self::init(); |
||
| 214 | try |
||
| 215 | { |
||
| 216 | if ( self::$links[self::$link_name] ) |
||
| 217 | { |
||
| 218 | if ($expire > 0) |
||
| 219 | { |
||
| 220 | return self::$links[self::$link_name]->set($key, $value, array('nx', 'ex' => $expire)); |
||
| 221 | //self::$links[self::$link_name]->multi(); |
||
| 222 | //self::$links[self::$link_name]->setNX($key, $value); |
||
| 223 | //self::$links[self::$link_name]->expire($key, $expire); |
||
| 224 | //self::$links[self::$link_name]->exec(); |
||
| 225 | //return true; |
||
| 226 | } |
||
| 227 | else |
||
| 228 | { |
||
| 229 | return self::$links[self::$link_name]->setnx($key, $value); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | } |
||
| 233 | catch (Exception $e) |
||
| 234 | { |
||
| 235 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 236 | log::warn($msg); |
||
| 237 | if ($e->getCode() == 0) |
||
| 238 | { |
||
| 239 | self::$links[self::$link_name]->close(); |
||
| 240 | self::$links[self::$link_name] = null; |
||
| 241 | usleep(100000); |
||
| 242 | return self::setnx($key, $value, $expire); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | return NULL; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * 锁 |
||
| 250 | * 默认锁1秒 |
||
| 251 | * |
||
| 252 | * @param mixed $name 锁的标识名 |
||
| 253 | * @param mixed $value 锁的值,貌似没啥意义 |
||
| 254 | * @param int $expire 当前锁的最大生存时间(秒),必须大于0,超过生存时间系统会自动强制释放锁 |
||
| 255 | * @param int $interval 获取锁失败后挂起再试的时间间隔(微秒) |
||
| 256 | * @return void |
||
| 257 | * @author seatle <[email protected]> |
||
| 258 | * @created time :2016-10-30 23:56 |
||
| 259 | */ |
||
| 260 | public static function lock($name, $value = 1, $expire = 5, $interval = 100000) |
||
| 261 | { |
||
| 262 | if ($name == null) |
||
| 263 | { |
||
| 264 | return false; |
||
| 265 | } |
||
| 266 | |||
| 267 | self::init(); |
||
| 268 | try |
||
| 269 | { |
||
| 270 | if ( self::$links[self::$link_name] ) |
||
| 271 | { |
||
| 272 | $key = "Lock:{$name}"; |
||
| 273 | while (true) |
||
| 274 | { |
||
| 275 | // 因为 setnx 没有 expire 设置,所以还是用set |
||
| 276 | //$result = self::$links[self::$link_name]->setnx($key, $value); |
||
| 277 | $result = self::$links[self::$link_name]->set($key, $value, array('nx', 'ex' => $expire)); |
||
| 278 | if ($result != false) |
||
| 279 | { |
||
| 280 | return true; |
||
| 281 | } |
||
| 282 | |||
| 283 | usleep($interval); |
||
| 284 | } |
||
| 285 | return false; |
||
| 286 | } |
||
| 287 | } |
||
| 288 | catch (Exception $e) |
||
| 289 | { |
||
| 290 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 291 | log::warn($msg); |
||
| 292 | if ($e->getCode() == 0) |
||
| 293 | { |
||
| 294 | self::$links[self::$link_name]->close(); |
||
| 295 | self::$links[self::$link_name] = null; |
||
| 296 | // 睡眠100毫秒 |
||
| 297 | usleep(100000); |
||
| 298 | return self::lock($name, $value, $expire, $interval); |
||
| 299 | } |
||
| 300 | } |
||
| 301 | return false; |
||
| 302 | } |
||
| 303 | |||
| 304 | public static function unlock($name) |
||
| 305 | { |
||
| 306 | $key = "Lock:{$name}"; |
||
| 307 | return self::del($key); |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * get |
||
| 312 | * |
||
| 313 | * @param mixed $key |
||
| 314 | * @return void |
||
| 315 | * @author seatle <[email protected]> |
||
| 316 | * @created time :2015-12-13 01:05 |
||
| 317 | */ |
||
| 318 | public static function get($key) |
||
| 319 | { |
||
| 320 | self::init(); |
||
| 321 | try |
||
| 322 | { |
||
| 323 | if ( self::$links[self::$link_name] ) |
||
| 324 | { |
||
| 325 | return self::$links[self::$link_name]->get($key); |
||
| 326 | } |
||
| 327 | } |
||
| 328 | catch (Exception $e) |
||
| 329 | { |
||
| 330 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 331 | log::warn($msg); |
||
| 332 | if ($e->getCode() == 0) |
||
| 333 | { |
||
| 334 | self::$links[self::$link_name]->close(); |
||
| 335 | self::$links[self::$link_name] = null; |
||
| 336 | usleep(100000); |
||
| 337 | return self::get($key); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | return NULL; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * del 删除数据 |
||
| 345 | * |
||
| 346 | * @param mixed $key |
||
| 347 | * @return void |
||
| 348 | * @author seatle <[email protected]> |
||
| 349 | * @created time :2015-12-13 01:05 |
||
| 350 | */ |
||
| 351 | public static function del($key) |
||
| 352 | { |
||
| 353 | self::init(); |
||
| 354 | try |
||
| 355 | { |
||
| 356 | if ( self::$links[self::$link_name] ) |
||
| 357 | { |
||
| 358 | return self::$links[self::$link_name]->del($key); |
||
| 359 | } |
||
| 360 | } |
||
| 361 | catch (Exception $e) |
||
| 362 | { |
||
| 363 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 364 | log::warn($msg); |
||
| 365 | if ($e->getCode() == 0) |
||
| 366 | { |
||
| 367 | self::$links[self::$link_name]->close(); |
||
| 368 | self::$links[self::$link_name] = null; |
||
| 369 | usleep(100000); |
||
| 370 | return self::del($key); |
||
| 371 | } |
||
| 372 | } |
||
| 373 | return NULL; |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * type 返回值的类型 |
||
| 378 | * |
||
| 379 | * @param mixed $key |
||
| 380 | * @return void |
||
| 381 | * @author seatle <[email protected]> |
||
| 382 | * @created time :2015-12-13 01:05 |
||
| 383 | */ |
||
| 384 | public static function type($key) |
||
| 385 | { |
||
| 386 | self::init(); |
||
| 387 | |||
| 388 | $types = array( |
||
| 389 | '0' => 'set', |
||
| 390 | '1' => 'string', |
||
| 391 | '3' => 'list', |
||
| 392 | ); |
||
| 393 | |||
| 394 | try |
||
| 395 | { |
||
| 396 | if ( self::$links[self::$link_name] ) |
||
| 397 | { |
||
| 398 | $type = self::$links[self::$link_name]->type($key); |
||
| 399 | if (isset($types[$type])) |
||
| 400 | { |
||
| 401 | return $types[$type]; |
||
| 402 | } |
||
| 403 | } |
||
| 404 | } |
||
| 405 | catch (Exception $e) |
||
| 406 | { |
||
| 407 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 408 | log::warn($msg); |
||
| 409 | if ($e->getCode() == 0) |
||
| 410 | { |
||
| 411 | self::$links[self::$link_name]->close(); |
||
| 412 | self::$links[self::$link_name] = null; |
||
| 413 | usleep(100000); |
||
| 414 | return self::type($key); |
||
| 415 | } |
||
| 416 | } |
||
| 417 | return NULL; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * incr 名称为key的string增加integer, integer为0则增1 |
||
| 422 | * |
||
| 423 | * @param mixed $key |
||
| 424 | * @param int $integer |
||
| 425 | * @return void |
||
| 426 | * @author seatle <[email protected]> |
||
| 427 | * @created time :2015-12-18 11:28 |
||
| 428 | */ |
||
| 429 | public static function incr($key, $integer = 0) |
||
| 430 | { |
||
| 431 | self::init(); |
||
| 432 | try |
||
| 433 | { |
||
| 434 | if ( self::$links[self::$link_name] ) |
||
| 435 | { |
||
| 436 | if (empty($integer)) |
||
| 437 | { |
||
| 438 | return self::$links[self::$link_name]->incr($key); |
||
| 439 | } |
||
| 440 | else |
||
| 441 | { |
||
| 442 | return self::$links[self::$link_name]->incrby($key, $integer); |
||
| 443 | } |
||
| 444 | } |
||
| 445 | } |
||
| 446 | catch (Exception $e) |
||
| 447 | { |
||
| 448 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 449 | log::warn($msg); |
||
| 450 | if ($e->getCode() == 0) |
||
| 451 | { |
||
| 452 | self::$links[self::$link_name]->close(); |
||
| 453 | self::$links[self::$link_name] = null; |
||
| 454 | usleep(100000); |
||
| 455 | return self::incr($key, $integer); |
||
| 456 | } |
||
| 457 | } |
||
| 458 | return NULL; |
||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * decr 名称为key的string减少integer, integer为0则减1 |
||
| 463 | * |
||
| 464 | * @param mixed $key |
||
| 465 | * @param int $integer |
||
| 466 | * @return void |
||
| 467 | * @author seatle <[email protected]> |
||
| 468 | * @created time :2015-12-18 11:28 |
||
| 469 | */ |
||
| 470 | public static function decr($key, $integer = 0) |
||
| 471 | { |
||
| 472 | self::init(); |
||
| 473 | try |
||
| 474 | { |
||
| 475 | if ( self::$links[self::$link_name] ) |
||
| 476 | { |
||
| 477 | if (empty($integer)) |
||
| 478 | { |
||
| 479 | return self::$links[self::$link_name]->decr($key); |
||
| 480 | } |
||
| 481 | else |
||
| 482 | { |
||
| 483 | return self::$links[self::$link_name]->decrby($key, $integer); |
||
| 484 | } |
||
| 485 | } |
||
| 486 | } |
||
| 487 | catch (Exception $e) |
||
| 488 | { |
||
| 489 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 490 | log::warn($msg); |
||
| 491 | if ($e->getCode() == 0) |
||
| 492 | { |
||
| 493 | self::$links[self::$link_name]->close(); |
||
| 494 | self::$links[self::$link_name] = null; |
||
| 495 | usleep(100000); |
||
| 496 | return self::decr($key, $integer); |
||
| 497 | } |
||
| 498 | } |
||
| 499 | return NULL; |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * append 名称为key的string的值附加value |
||
| 504 | * |
||
| 505 | * @param mixed $key |
||
| 506 | * @param mixed $value |
||
| 507 | * @return void |
||
| 508 | * @author seatle <[email protected]> |
||
| 509 | * @created time :2015-12-18 11:28 |
||
| 510 | */ |
||
| 511 | public static function append($key, $value) |
||
| 512 | { |
||
| 513 | self::init(); |
||
| 514 | try |
||
| 515 | { |
||
| 516 | if ( self::$links[self::$link_name] ) |
||
| 517 | { |
||
| 518 | return self::$links[self::$link_name]->append($key, $value); |
||
| 519 | } |
||
| 520 | } |
||
| 521 | catch (Exception $e) |
||
| 522 | { |
||
| 523 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 524 | log::warn($msg); |
||
| 525 | if ($e->getCode() == 0) |
||
| 526 | { |
||
| 527 | self::$links[self::$link_name]->close(); |
||
| 528 | self::$links[self::$link_name] = null; |
||
| 529 | usleep(100000); |
||
| 530 | return self::append($key, $value); |
||
| 531 | } |
||
| 532 | } |
||
| 533 | return NULL; |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * substr 返回名称为key的string的value的子串 |
||
| 538 | * |
||
| 539 | * @param mixed $key |
||
| 540 | * @param mixed $start |
||
| 541 | * @param mixed $end |
||
| 542 | * @return void |
||
| 543 | * @author seatle <[email protected]> |
||
| 544 | * @created time :2015-12-18 11:28 |
||
| 545 | */ |
||
| 546 | public static function substr($key, $start, $end) |
||
| 547 | { |
||
| 548 | self::init(); |
||
| 549 | try |
||
| 550 | { |
||
| 551 | if ( self::$links[self::$link_name] ) |
||
| 552 | { |
||
| 553 | return self::$links[self::$link_name]->substr($key, $start, $end); |
||
| 554 | } |
||
| 555 | } |
||
| 556 | catch (Exception $e) |
||
| 557 | { |
||
| 558 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 559 | log::warn($msg); |
||
| 560 | if ($e->getCode() == 0) |
||
| 561 | { |
||
| 562 | self::$links[self::$link_name]->close(); |
||
| 563 | self::$links[self::$link_name] = null; |
||
| 564 | usleep(100000); |
||
| 565 | return self::substr($key, $start, $end); |
||
| 566 | } |
||
| 567 | } |
||
| 568 | return NULL; |
||
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * select 按索引查询 |
||
| 573 | * |
||
| 574 | * @param mixed $index |
||
| 575 | * @return void |
||
| 576 | * @author seatle <[email protected]> |
||
| 577 | * @created time :2015-12-18 11:28 |
||
| 578 | */ |
||
| 579 | public static function select($index) |
||
| 580 | { |
||
| 581 | self::init(); |
||
| 582 | try |
||
| 583 | { |
||
| 584 | if ( self::$links[self::$link_name] ) |
||
| 585 | { |
||
| 586 | return self::$links[self::$link_name]->select($index); |
||
| 587 | } |
||
| 588 | } |
||
| 589 | catch (Exception $e) |
||
| 590 | { |
||
| 591 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 592 | log::warn($msg); |
||
| 593 | if ($e->getCode() == 0) |
||
| 594 | { |
||
| 595 | self::$links[self::$link_name]->close(); |
||
| 596 | self::$links[self::$link_name] = null; |
||
| 597 | usleep(100000); |
||
| 598 | return self::select($index); |
||
| 599 | } |
||
| 600 | } |
||
| 601 | return NULL; |
||
| 602 | } |
||
| 603 | |||
| 604 | /** |
||
| 605 | * dbsize 返回当前数据库中key的数目 |
||
| 606 | * |
||
| 607 | * @param mixed $key |
||
| 608 | * @return void |
||
| 609 | * @author seatle <[email protected]> |
||
| 610 | * @created time :2015-12-18 11:28 |
||
| 611 | */ |
||
| 612 | public static function dbsize() |
||
| 613 | { |
||
| 614 | self::init(); |
||
| 615 | try |
||
| 616 | { |
||
| 617 | if ( self::$links[self::$link_name] ) |
||
| 618 | { |
||
| 619 | return self::$links[self::$link_name]->dbsize(); |
||
| 620 | } |
||
| 621 | } |
||
| 622 | catch (Exception $e) |
||
| 623 | { |
||
| 624 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 625 | log::warn($msg); |
||
| 626 | if ($e->getCode() == 0) |
||
| 627 | { |
||
| 628 | self::$links[self::$link_name]->close(); |
||
| 629 | self::$links[self::$link_name] = null; |
||
| 630 | usleep(100000); |
||
| 631 | return self::dbsize(); |
||
| 632 | } |
||
| 633 | } |
||
| 634 | return 0; |
||
| 635 | } |
||
| 636 | |||
| 637 | /** |
||
| 638 | * flushdb 删除当前选择数据库中的所有key |
||
| 639 | * |
||
| 640 | * @return void |
||
| 641 | * @author seatle <[email protected]> |
||
| 642 | * @created time :2015-12-18 11:28 |
||
| 643 | */ |
||
| 644 | public static function flushdb() |
||
| 645 | { |
||
| 646 | self::init(); |
||
| 647 | try |
||
| 648 | { |
||
| 649 | if ( self::$links[self::$link_name] ) |
||
| 650 | { |
||
| 651 | return self::$links[self::$link_name]->flushdb(); |
||
| 652 | } |
||
| 653 | } |
||
| 654 | catch (Exception $e) |
||
| 655 | { |
||
| 656 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 657 | log::warn($msg); |
||
| 658 | if ($e->getCode() == 0) |
||
| 659 | { |
||
| 660 | self::$links[self::$link_name]->close(); |
||
| 661 | self::$links[self::$link_name] = null; |
||
| 662 | usleep(100000); |
||
| 663 | return self::flushdb(); |
||
| 664 | } |
||
| 665 | } |
||
| 666 | return NULL; |
||
| 667 | } |
||
| 668 | |||
| 669 | /** |
||
| 670 | * flushall 删除所有数据库中的所有key |
||
| 671 | * |
||
| 672 | * @return void |
||
| 673 | * @author seatle <[email protected]> |
||
| 674 | * @created time :2015-12-18 11:28 |
||
| 675 | */ |
||
| 676 | public static function flushall() |
||
| 677 | { |
||
| 678 | self::init(); |
||
| 679 | try |
||
| 680 | { |
||
| 681 | if ( self::$links[self::$link_name] ) |
||
| 682 | { |
||
| 683 | return self::$links[self::$link_name]->flushall(); |
||
| 684 | } |
||
| 685 | } |
||
| 686 | catch (Exception $e) |
||
| 687 | { |
||
| 688 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 689 | log::warn($msg); |
||
| 690 | if ($e->getCode() == 0) |
||
| 691 | { |
||
| 692 | self::$links[self::$link_name]->close(); |
||
| 693 | self::$links[self::$link_name] = null; |
||
| 694 | usleep(100000); |
||
| 695 | return self::flushall(); |
||
| 696 | } |
||
| 697 | } |
||
| 698 | return NULL; |
||
| 699 | } |
||
| 700 | |||
| 701 | /** |
||
| 702 | * save 将数据保存到磁盘 |
||
| 703 | * |
||
| 704 | * @param mixed $is_bgsave 将数据异步保存到磁盘 |
||
| 705 | * @return void |
||
| 706 | * @author seatle <[email protected]> |
||
| 707 | * @created time :2015-12-18 11:28 |
||
| 708 | */ |
||
| 709 | public static function save($is_bgsave = false) |
||
| 710 | { |
||
| 711 | self::init(); |
||
| 712 | try |
||
| 713 | { |
||
| 714 | if ( self::$links[self::$link_name] ) |
||
| 715 | { |
||
| 716 | if (!$is_bgsave) |
||
| 717 | { |
||
| 718 | return self::$links[self::$link_name]->save(); |
||
| 719 | } |
||
| 720 | else |
||
| 721 | { |
||
| 722 | return self::$links[self::$link_name]->bgsave(); |
||
| 723 | } |
||
| 724 | } |
||
| 725 | } |
||
| 726 | catch (Exception $e) |
||
| 727 | { |
||
| 728 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 729 | log::warn($msg); |
||
| 730 | if ($e->getCode() == 0) |
||
| 731 | { |
||
| 732 | self::$links[self::$link_name]->close(); |
||
| 733 | self::$links[self::$link_name] = null; |
||
| 734 | usleep(100000); |
||
| 735 | return self::save($is_bgsave); |
||
| 736 | } |
||
| 737 | } |
||
| 738 | return NULL; |
||
| 739 | } |
||
| 740 | |||
| 741 | /** |
||
| 742 | * info 提供服务器的信息和统计 |
||
| 743 | * |
||
| 744 | * @return void |
||
| 745 | * @author seatle <[email protected]> |
||
| 746 | * @created time :2015-12-18 11:28 |
||
| 747 | */ |
||
| 748 | public static function info() |
||
| 749 | { |
||
| 750 | self::init(); |
||
| 751 | try |
||
| 752 | { |
||
| 753 | if ( self::$links[self::$link_name] ) |
||
| 754 | { |
||
| 755 | return self::$links[self::$link_name]->info(); |
||
| 756 | } |
||
| 757 | } |
||
| 758 | catch (Exception $e) |
||
| 759 | { |
||
| 760 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 761 | log::warn($msg); |
||
| 762 | if ($e->getCode() == 0) |
||
| 763 | { |
||
| 764 | self::$links[self::$link_name]->close(); |
||
| 765 | self::$links[self::$link_name] = null; |
||
| 766 | usleep(100000); |
||
| 767 | return self::info(); |
||
| 768 | } |
||
| 769 | } |
||
| 770 | return NULL; |
||
| 771 | } |
||
| 772 | |||
| 773 | /** |
||
| 774 | * slowlog 慢查询日志 |
||
| 775 | * |
||
| 776 | * @return void |
||
| 777 | * @author seatle <[email protected]> |
||
| 778 | * @created time :2015-12-18 11:28 |
||
| 779 | */ |
||
| 780 | public static function slowlog($command = 'get', $len = 0) |
||
| 781 | { |
||
| 782 | self::init(); |
||
| 783 | try |
||
| 784 | { |
||
| 785 | if ( self::$links[self::$link_name] ) |
||
| 786 | { |
||
| 787 | if (!empty($len)) |
||
| 788 | { |
||
| 789 | return $redis->slowlog($command, $len); |
||
| 790 | } |
||
| 791 | else |
||
| 792 | { |
||
| 793 | return $redis->slowlog($command); |
||
| 794 | } |
||
| 795 | } |
||
| 796 | } |
||
| 797 | catch (Exception $e) |
||
| 798 | { |
||
| 799 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 800 | log::warn($msg); |
||
| 801 | if ($e->getCode() == 0) |
||
| 802 | { |
||
| 803 | self::$links[self::$link_name]->close(); |
||
| 804 | self::$links[self::$link_name] = null; |
||
| 805 | usleep(100000); |
||
| 806 | return self::slowlog($command, $len); |
||
| 807 | } |
||
| 808 | } |
||
| 809 | return NULL; |
||
| 810 | } |
||
| 811 | |||
| 812 | /** |
||
| 813 | * lastsave 返回上次成功将数据保存到磁盘的Unix时戳 |
||
| 814 | * |
||
| 815 | * @return void |
||
| 816 | * @author seatle <[email protected]> |
||
| 817 | * @created time :2015-12-18 11:28 |
||
| 818 | */ |
||
| 819 | public static function lastsave() |
||
| 820 | { |
||
| 821 | self::init(); |
||
| 822 | try |
||
| 823 | { |
||
| 824 | if ( self::$links[self::$link_name] ) |
||
| 825 | { |
||
| 826 | return self::$links[self::$link_name]->lastsave(); |
||
| 827 | } |
||
| 828 | } |
||
| 829 | catch (Exception $e) |
||
| 830 | { |
||
| 831 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 832 | log::warn($msg); |
||
| 833 | if ($e->getCode() == 0) |
||
| 834 | { |
||
| 835 | self::$links[self::$link_name]->close(); |
||
| 836 | self::$links[self::$link_name] = null; |
||
| 837 | usleep(100000); |
||
| 838 | return self::lastsave(); |
||
| 839 | } |
||
| 840 | } |
||
| 841 | return NULL; |
||
| 842 | } |
||
| 843 | |||
| 844 | /** |
||
| 845 | * lpush 将数据从左边压入 |
||
| 846 | * |
||
| 847 | * @param mixed $key |
||
| 848 | * @param mixed $value |
||
| 849 | * @return void |
||
| 850 | * @author seatle <[email protected]> |
||
| 851 | * @created time :2015-12-13 01:05 |
||
| 852 | */ |
||
| 853 | public static function lpush($key, $value) |
||
| 854 | { |
||
| 855 | self::init(); |
||
| 856 | try |
||
| 857 | { |
||
| 858 | if ( self::$links[self::$link_name] ) |
||
| 859 | { |
||
| 860 | return self::$links[self::$link_name]->lpush($key, $value); |
||
| 861 | } |
||
| 862 | } |
||
| 863 | catch (Exception $e) |
||
| 864 | { |
||
| 865 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 866 | log::warn($msg); |
||
| 867 | if ($e->getCode() == 0) |
||
| 868 | { |
||
| 869 | self::$links[self::$link_name]->close(); |
||
| 870 | self::$links[self::$link_name] = null; |
||
| 871 | usleep(100000); |
||
| 872 | return self::lpush($key, $value); |
||
| 873 | } |
||
| 874 | } |
||
| 875 | return NULL; |
||
| 876 | } |
||
| 877 | |||
| 878 | /** |
||
| 879 | * rpush 将数据从右边压入 |
||
| 880 | * |
||
| 881 | * @param mixed $key |
||
| 882 | * @param mixed $value |
||
| 883 | * @return void |
||
| 884 | * @author seatle <[email protected]> |
||
| 885 | * @created time :2015-12-13 01:05 |
||
| 886 | */ |
||
| 887 | public static function rpush($key, $value) |
||
| 888 | { |
||
| 889 | self::init(); |
||
| 890 | try |
||
| 891 | { |
||
| 892 | if ( self::$links[self::$link_name] ) |
||
| 893 | { |
||
| 894 | return self::$links[self::$link_name]->rpush($key, $value); |
||
| 895 | } |
||
| 896 | } |
||
| 897 | catch (Exception $e) |
||
| 898 | { |
||
| 899 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 900 | log::warn($msg); |
||
| 901 | if ($e->getCode() == 0) |
||
| 902 | { |
||
| 903 | self::$links[self::$link_name]->close(); |
||
| 904 | self::$links[self::$link_name] = null; |
||
| 905 | usleep(100000); |
||
| 906 | return self::rpush($key, $value); |
||
| 907 | } |
||
| 908 | } |
||
| 909 | return NULL; |
||
| 910 | } |
||
| 911 | |||
| 912 | /** |
||
| 913 | * lpop 从左边弹出数据, 并删除数据 |
||
| 914 | * |
||
| 915 | * @param mixed $key |
||
| 916 | * @return void |
||
| 917 | * @author seatle <[email protected]> |
||
| 918 | * @created time :2015-12-13 01:05 |
||
| 919 | */ |
||
| 920 | public static function lpop($key) |
||
| 921 | { |
||
| 922 | self::init(); |
||
| 923 | try |
||
| 924 | { |
||
| 925 | if ( self::$links[self::$link_name] ) |
||
| 926 | { |
||
| 927 | return self::$links[self::$link_name]->lpop($key); |
||
| 928 | } |
||
| 929 | } |
||
| 930 | catch (Exception $e) |
||
| 931 | { |
||
| 932 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 933 | log::warn($msg); |
||
| 934 | if ($e->getCode() == 0) |
||
| 935 | { |
||
| 936 | self::$links[self::$link_name]->close(); |
||
| 937 | self::$links[self::$link_name] = null; |
||
| 938 | usleep(100000); |
||
| 939 | return self::lpop($key); |
||
| 940 | } |
||
| 941 | } |
||
| 942 | return NULL; |
||
| 943 | } |
||
| 944 | |||
| 945 | /** |
||
| 946 | * rpop 从右边弹出数据, 并删除数据 |
||
| 947 | * |
||
| 948 | * @param mixed $key |
||
| 949 | * @return void |
||
| 950 | * @author seatle <[email protected]> |
||
| 951 | * @created time :2015-12-13 01:05 |
||
| 952 | */ |
||
| 953 | public static function rpop($key) |
||
| 954 | { |
||
| 955 | self::init(); |
||
| 956 | try |
||
| 957 | { |
||
| 958 | if ( self::$links[self::$link_name] ) |
||
| 959 | { |
||
| 960 | return self::$links[self::$link_name]->rpop($key); |
||
| 961 | } |
||
| 962 | } |
||
| 963 | catch (Exception $e) |
||
| 964 | { |
||
| 965 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 966 | log::warn($msg); |
||
| 967 | if ($e->getCode() == 0) |
||
| 968 | { |
||
| 969 | self::$links[self::$link_name]->close(); |
||
| 970 | self::$links[self::$link_name] = null; |
||
| 971 | usleep(100000); |
||
| 972 | return self::rpop($key); |
||
| 973 | } |
||
| 974 | } |
||
| 975 | return NULL; |
||
| 976 | } |
||
| 977 | |||
| 978 | /** |
||
| 979 | * lsize 队列长度,同llen |
||
| 980 | * |
||
| 981 | * @param mixed $key |
||
| 982 | * @return void |
||
| 983 | * @author seatle <[email protected]> |
||
| 984 | * @created time :2015-12-13 01:05 |
||
| 985 | */ |
||
| 986 | public static function lsize($key) |
||
| 987 | { |
||
| 988 | self::init(); |
||
| 989 | try |
||
| 990 | { |
||
| 991 | if ( self::$links[self::$link_name] ) |
||
| 992 | { |
||
| 993 | return self::$links[self::$link_name]->lSize($key); |
||
| 994 | } |
||
| 995 | } |
||
| 996 | catch (Exception $e) |
||
| 997 | { |
||
| 998 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 999 | log::warn($msg); |
||
| 1000 | if ($e->getCode() == 0) |
||
| 1001 | { |
||
| 1002 | self::$links[self::$link_name]->close(); |
||
| 1003 | self::$links[self::$link_name] = null; |
||
| 1004 | usleep(100000); |
||
| 1005 | return self::lsize($key); |
||
| 1006 | } |
||
| 1007 | } |
||
| 1008 | return NULL; |
||
| 1009 | } |
||
| 1010 | |||
| 1011 | /** |
||
| 1012 | * lget 获取数据 |
||
| 1013 | * |
||
| 1014 | * @param mixed $key |
||
| 1015 | * @param int $index |
||
| 1016 | * @return void |
||
| 1017 | * @author seatle <[email protected]> |
||
| 1018 | * @created time :2015-12-13 01:05 |
||
| 1019 | */ |
||
| 1020 | public static function lget($key, $index = 0) |
||
| 1021 | { |
||
| 1022 | self::init(); |
||
| 1023 | try |
||
| 1024 | { |
||
| 1025 | if ( self::$links[self::$link_name] ) |
||
| 1026 | { |
||
| 1027 | return self::$links[self::$link_name]->lget($key, $index); |
||
| 1028 | } |
||
| 1029 | } |
||
| 1030 | catch (Exception $e) |
||
| 1031 | { |
||
| 1032 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 1033 | log::warn($msg); |
||
| 1034 | if ($e->getCode() == 0) |
||
| 1035 | { |
||
| 1036 | self::$links[self::$link_name]->close(); |
||
| 1037 | self::$links[self::$link_name] = null; |
||
| 1038 | usleep(100000); |
||
| 1039 | return self::lget($key, $index); |
||
| 1040 | } |
||
| 1041 | } |
||
| 1042 | return NULL; |
||
| 1043 | } |
||
| 1044 | |||
| 1045 | /** |
||
| 1046 | * lRange 获取范围数据 |
||
| 1047 | * |
||
| 1048 | * @param mixed $key |
||
| 1049 | * @param mixed $start |
||
| 1050 | * @param mixed $end |
||
| 1051 | * @return void |
||
| 1052 | * @author seatle <[email protected]> |
||
| 1053 | * @created time :2015-12-13 01:05 |
||
| 1054 | */ |
||
| 1055 | public static function lrange($key, $start, $end) |
||
| 1056 | { |
||
| 1057 | self::init(); |
||
| 1058 | try |
||
| 1059 | { |
||
| 1060 | if ( self::$links[self::$link_name] ) |
||
| 1061 | { |
||
| 1062 | return self::$links[self::$link_name]->lRange($key, $start, $end); |
||
| 1063 | } |
||
| 1064 | } |
||
| 1065 | catch (Exception $e) |
||
| 1066 | { |
||
| 1067 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 1068 | log::warn($msg); |
||
| 1069 | if ($e->getCode() == 0) |
||
| 1070 | { |
||
| 1071 | self::$links[self::$link_name]->close(); |
||
| 1072 | self::$links[self::$link_name] = null; |
||
| 1073 | usleep(100000); |
||
| 1074 | return self::lrange($key, $start, $end); |
||
| 1075 | } |
||
| 1076 | } |
||
| 1077 | return NULL; |
||
| 1078 | } |
||
| 1079 | |||
| 1080 | /** |
||
| 1081 | * rlist 从右边弹出 $length 长度数据,并删除数据 |
||
| 1082 | * |
||
| 1083 | * @param mixed $key |
||
| 1084 | * @param mixed $length |
||
| 1085 | * @return void |
||
| 1086 | * @author seatle <[email protected]> |
||
| 1087 | * @created time :2015-12-13 01:05 |
||
| 1088 | */ |
||
| 1089 | public static function rlist($key, $length) |
||
| 1090 | { |
||
| 1091 | $queue_length = self::lsize($key); |
||
| 1092 | // 如果队列中有数据 |
||
| 1093 | if ($queue_length > 0) |
||
| 1094 | { |
||
| 1095 | $list = array(); |
||
| 1096 | $count = ($queue_length >= $length) ? $length : $queue_length; |
||
| 1097 | for ($i = 0; $i < $count; $i++) |
||
| 1098 | { |
||
| 1099 | $data = self::rpop($key); |
||
| 1100 | if ($data === false) |
||
| 1101 | { |
||
| 1102 | continue; |
||
| 1103 | } |
||
| 1104 | |||
| 1105 | $list[] = $data; |
||
| 1106 | } |
||
| 1107 | return $list; |
||
| 1108 | } |
||
| 1109 | else |
||
| 1110 | { |
||
| 1111 | // 没有数据返回NULL |
||
| 1112 | return NULL; |
||
| 1113 | } |
||
| 1114 | } |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * keys |
||
| 1118 | * |
||
| 1119 | * @param mixed $key |
||
| 1120 | * @return void |
||
| 1121 | * @author seatle <[email protected]> |
||
| 1122 | * @created time :2015-12-13 01:05 |
||
| 1123 | * 查找符合给定模式的key。 |
||
| 1124 | * KEYS *命中数据库中所有key。 |
||
| 1125 | * KEYS h?llo命中hello, hallo and hxllo等。 |
||
| 1126 | * KEYS h*llo命中hllo和heeeeello等。 |
||
| 1127 | * KEYS h[ae]llo命中hello和hallo,但不命中hillo。 |
||
| 1128 | * 特殊符号用"\"隔开 |
||
| 1129 | * 因为这个类加了OPT_PREFIX前缀,所以并不能真的列出redis所有的key,需要的话,要把前缀去掉 |
||
| 1130 | */ |
||
| 1131 | public static function keys($key) |
||
| 1132 | { |
||
| 1133 | self::init(); |
||
| 1134 | try |
||
| 1135 | { |
||
| 1136 | if ( self::$links[self::$link_name] ) |
||
| 1137 | { |
||
| 1138 | return self::$links[self::$link_name]->keys($key); |
||
| 1139 | } |
||
| 1140 | } |
||
| 1141 | catch (Exception $e) |
||
| 1142 | { |
||
| 1143 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 1144 | log::warn($msg); |
||
| 1145 | if ($e->getCode() == 0) |
||
| 1146 | { |
||
| 1147 | self::$links[self::$link_name]->close(); |
||
| 1148 | self::$links[self::$link_name] = null; |
||
| 1149 | usleep(100000); |
||
| 1150 | return self::keys($key); |
||
| 1151 | } |
||
| 1152 | } |
||
| 1153 | return NULL; |
||
| 1154 | } |
||
| 1155 | |||
| 1156 | /** |
||
| 1157 | * ttl 返回某个KEY的过期时间 |
||
| 1158 | * 正数:剩余多少秒 |
||
| 1159 | * -1:永不超时 |
||
| 1160 | * -2:key不存在 |
||
| 1161 | * @param mixed $key |
||
| 1162 | * @return void |
||
| 1163 | * @author seatle <[email protected]> |
||
| 1164 | * @created time :2015-12-13 01:05 |
||
| 1165 | */ |
||
| 1166 | public static function ttl($key) |
||
| 1167 | { |
||
| 1168 | self::init(); |
||
| 1169 | try |
||
| 1170 | { |
||
| 1171 | if ( self::$links[self::$link_name] ) |
||
| 1172 | { |
||
| 1173 | return self::$links[self::$link_name]->ttl($key); |
||
| 1174 | } |
||
| 1175 | } |
||
| 1176 | catch (Exception $e) |
||
| 1177 | { |
||
| 1178 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 1179 | log::warn($msg); |
||
| 1180 | if ($e->getCode() == 0) |
||
| 1181 | { |
||
| 1182 | self::$links[self::$link_name]->close(); |
||
| 1183 | self::$links[self::$link_name] = null; |
||
| 1184 | usleep(100000); |
||
| 1185 | return self::ttl($key); |
||
| 1186 | } |
||
| 1187 | } |
||
| 1188 | return NULL; |
||
| 1189 | } |
||
| 1190 | |||
| 1191 | /** |
||
| 1192 | * expire 为某个key设置过期时间,同setTimeout |
||
| 1193 | * |
||
| 1194 | * @param mixed $key |
||
| 1195 | * @param mixed $expire |
||
| 1196 | * @return void |
||
| 1197 | * @author seatle <[email protected]> |
||
| 1198 | * @created time :2015-12-13 01:05 |
||
| 1199 | */ |
||
| 1200 | public static function expire($key, $expire) |
||
| 1223 | } |
||
| 1224 | |||
| 1225 | /** |
||
| 1226 | * exists key值是否存在 |
||
| 1227 | * |
||
| 1228 | * @param mixed $key |
||
| 1229 | * @return void |
||
| 1230 | * @author seatle <[email protected]> |
||
| 1231 | * @created time :2015-12-13 01:05 |
||
| 1232 | */ |
||
| 1233 | public static function exists($key) |
||
| 1234 | { |
||
| 1235 | self::init(); |
||
| 1236 | try |
||
| 1237 | { |
||
| 1238 | if ( self::$links[self::$link_name] ) |
||
| 1239 | { |
||
| 1240 | return self::$links[self::$link_name]->exists($key); |
||
| 1241 | } |
||
| 1242 | } |
||
| 1243 | catch (Exception $e) |
||
| 1244 | { |
||
| 1245 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 1246 | log::warn($msg); |
||
| 1247 | if ($e->getCode() == 0) |
||
| 1248 | { |
||
| 1249 | self::$links[self::$link_name]->close(); |
||
| 1250 | self::$links[self::$link_name] = null; |
||
| 1251 | usleep(100000); |
||
| 1252 | return self::exists($key); |
||
| 1253 | } |
||
| 1254 | } |
||
| 1255 | return false; |
||
| 1256 | } |
||
| 1257 | |||
| 1258 | /** |
||
| 1259 | * ping 检查当前redis是否存在且是否可以连接上 |
||
| 1260 | * |
||
| 1261 | * @return void |
||
| 1262 | * @author seatle <[email protected]> |
||
| 1263 | * @created time :2015-12-13 01:05 |
||
| 1264 | */ |
||
| 1265 | //protected static function ping() |
||
| 1266 | //{ |
||
| 1267 | //if ( empty (self::$links[self::$link_name]) ) |
||
| 1268 | //{ |
||
| 1269 | //return false; |
||
| 1270 | //} |
||
| 1271 | //return self::$links[self::$link_name]->ping() == '+PONG'; |
||
| 1272 | //} |
||
| 1273 | |||
| 1274 | public static function encode($value) |
||
| 1275 | { |
||
| 1276 | return json_encode($value, JSON_UNESCAPED_UNICODE); |
||
| 1277 | } |
||
| 1278 | |||
| 1279 | public static function decode($value) |
||
| 1280 | { |
||
| 1281 | return json_decode($value, true); |
||
| 1282 | } |
||
| 1283 | |||
| 1284 | /** |
||
| 1285 | * 集合操作 |
||
| 1286 | */ |
||
| 1287 | |||
| 1288 | /** |
||
| 1289 | * sadd 将数据压入集合 |
||
| 1290 | * |
||
| 1291 | * @param mixed $key |
||
| 1292 | * @param mixed $value |
||
| 1293 | * @return void |
||
| 1294 | * @author seatle <[email protected]> |
||
| 1295 | * @created time :2015-12-13 01:05 |
||
| 1296 | */ |
||
| 1297 | public static function sadd($key, $value) |
||
| 1298 | { |
||
| 1299 | self::init(); |
||
| 1300 | try |
||
| 1301 | { |
||
| 1302 | if (self::$links[self::$link_name]) |
||
| 1303 | { |
||
| 1304 | return self::$links[self::$link_name]->sadd($key, $value); |
||
| 1305 | } |
||
| 1306 | } |
||
| 1307 | catch (Exception $e) |
||
| 1308 | { |
||
| 1309 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 1310 | log::warn($msg); |
||
| 1311 | if ($e->getCode() == 0) |
||
| 1312 | { |
||
| 1313 | self::$links[self::$link_name]->close(); |
||
| 1314 | self::$links[self::$link_name] = null; |
||
| 1315 | usleep(100000); |
||
| 1316 | return self::sadd($key, $value); |
||
| 1317 | } |
||
| 1318 | } |
||
| 1319 | return null; |
||
| 1320 | } |
||
| 1321 | |||
| 1322 | /** |
||
| 1323 | * spop 从集合中随机取出数据并移除 |
||
| 1324 | * |
||
| 1325 | * @param mixed $key |
||
| 1326 | * @return void |
||
| 1327 | * @author seatle <[email protected]> |
||
| 1328 | * @created time :2015-12-13 01:05 |
||
| 1329 | */ |
||
| 1330 | public static function spop($key) |
||
| 1353 | } |
||
| 1354 | |||
| 1355 | /** |
||
| 1356 | * Redis Scard 命令返回集合中元素的数量。 |
||
| 1357 | * |
||
| 1358 | * @param mixed $key |
||
| 1359 | * @return void |
||
| 1360 | * @author seatle <[email protected]> |
||
| 1361 | * @created time :2015-12-13 01:05 |
||
| 1362 | */ |
||
| 1363 | public static function scard($key) |
||
| 1364 | { |
||
| 1365 | self::init(); |
||
| 1366 | try |
||
| 1367 | { |
||
| 1368 | if (self::$links[self::$link_name]) |
||
| 1369 | { |
||
| 1370 | return self::$links[self::$link_name]->scard($key); |
||
| 1371 | } |
||
| 1372 | } |
||
| 1373 | catch (Exception $e) |
||
| 1374 | { |
||
| 1375 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
||
| 1376 | log::warn($msg); |
||
| 1377 | if ($e->getCode() == 0) |
||
| 1378 | { |
||
| 1379 | self::$links[self::$link_name]->close(); |
||
| 1380 | self::$links[self::$link_name] = null; |
||
| 1381 | usleep(100000); |
||
| 1382 | return self::scard($key); |
||
| 1383 | } |
||
| 1384 | } |
||
| 1385 | return null; |
||
| 1386 | } |
||
| 1387 | |||
| 1388 | } |
||
| 1389 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.