You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pdd-order-api/app/libs/tool/class.RedisExt.php

87 lines
1.9 KiB
PHP

<?php
class RedisExt {
private $errorMode = 'bool';
private $lastException;
private $redis;
private $isConnected;
private $bizId;
protected function __construct($bizId, $errorMode) {
$this->redis = new Redis();
$this->errorMode = $errorMode;
$this->bizId = $bizId;
}
public function __call($method, $args) {
$this->lastException = null;
try {
if (!$this->isConnected) {
$this->tryConnect();
}
return call_user_func_array(array (
$this->redis,
$method
), $args);
} catch (Exception $ex) {
$this->lastException = $ex;
if ($this->errorMode == 'bool') {
return false;
} else {
throw $ex;
}
}
}
public function lastException() {
return $this->lastException;
}
public function setLastException($ex){
$this->lastException = $ex;
}
/**
*
* @return Redis
*/
public static function factory($bizCache, $errorMode = 'bool') {
$redisExt = new RedisExt($bizCache, $errorMode);
return $redisExt;
}
private function tryConnect() {
if ($this->isConnected) {
return true;
}
$redisConfig = Zc::C('redis');
$params = $redisConfig[$this->bizId];;
$r = $this->redis->connect($params['host'], $params['port'], $params['timeout'], NULL, 100);
if (!$r) {
$this->setLastException(new RuntimeException('connect fail, params: ' . ZcArrayHelper::sp($params)));
return false;
}
if (!empty($params['password'])) {
$r = $this->redis->auth($params['password']);
if (!$r) {
$this->setLastException(new RuntimeException('auth fail, params: ' . ZcArrayHelper::sp($params)));
return false;
}
}
if (!empty($params['dbIndex'])) {
$r = $this->redis->select($params['dbIndex']);
if (!$r) {
$this->setLastException(new RuntimeException('select dbIndex fail, params: ' . ZcArrayHelper::sp($params)));
return false;
}
}
$this->isConnected = true;
return true;
}
}