/** * 获取一个redis连接 * * @param boolean 是否强制重置连接 * @throws Exception */ public function getConnection($force = false) { if (false === !!$force && !is_null($this->_redisObj)) { return $this->_redisObj; } if (!is_null($this->_redisObj)) { unset($this->_redisObj); $this->_redisObj = null; } $servers = AppConf::$arrRedis['server'][Env::getIDC()]; $port = AppConf::$arrRedis['port']; shuffle($servers); foreach ($servers as $server) { $this->_redisObj = new Redis(); $conRet = $this->_redisObj->connect($server, $port, AppConf::$arrRedis['connect_timeout']); if (false === $conRet) { unset($this->_redisObj); $this->_redisObj = null; KC_LOG_WARNING('connect to redis failed [ server: %s, port: %s, timeout: %s ].', $server, $port, AppConf::$arrRedis['connect_timeout']); } else { KC_LOG_DEBUG('connect to redis succ [ server: %s, port: %s, timeout: %s ].', $server, $port, AppConf::$arrRedis['connect_timeout']); break; } } if (is_null($this->_redisObj)) { throw new Exception('redis connect to redis all failed failed.'); } return $this->_redisObj; }
public static function getRedis($readOnly = false) { $readOnly = false; if (isset(self::$redisObjs[$readOnly ? 1 : 0]) && self::$redisObjs[$readOnly ? 1 : 0] instanceof Redis) { return self::$redisObjs[$readOnly ? 1 : 0]; } if ($readOnly) { $option = EnvConf::$redisOptions[array_rand(EnvConf::$redisOptions)]; $option['host'] = $option['host'][Env::getIDC()]; } else { $option = EnvConf::$redisOptions['master']; $option['host'] = $option['host'][Env::getIDC()]; } $redis = new Redis(); $redis->connect($option['host'][array_rand($option['host'])], $option['port'], $option['timeout']); self::$redisObjs[$readOnly ? 1 : 0] = $redis; return $redis; }
/** * * 连接到数据库 * @param string $identifier 连接到哪个数据库,分表依据 * @param string $tag 直接指明连接到哪个数据库 */ public function fetchMysqlHandler($identifier, $tag = NULL) { if ($this->_isConnected) { $this->_mysqli->close(); $this->_isConnected = false; $this->_mysqli = mysqli_init(); } if (!is_null($tag)) { if (!isset(AppConf::$dbConf['server'][Env::getIDC()][intval($tag)])) { KC_LOG_WARNING(__FUNCTION__ . " failed, invalid tag received [ tag: {$tag} ]"); return false; } $arrMysqlServer = AppConf::$dbConf['server'][Env::getIDC()][intval($tag)]; } else { $tag = $this->getDbNo($identifier); $arrMysqlServer = AppConf::$dbConf['server'][Env::getIDC()][intval($tag)]; } $totalNum = count($arrMysqlServer); $index = mt_rand(0, $totalNum - 1); for ($i = 0; $i < $totalNum; $i++) { $mysqlServer = $arrMysqlServer[$index]; if (!isset($mysqlServer['host']) || !isset($mysqlServer['username']) || !isset($mysqlServer['password']) || !isset($mysqlServer['database']) || !isset($mysqlServer['port'])) { KC_LOG_WARNING(__FUNCTION__ . " failed, config must have host/username/password/database/port fields [mysqlServer: " . json_encode($mysqlServer) . "]"); return false; } if (false === $this->_mysqli->real_connect($mysqlServer['host'], $mysqlServer['username'], $mysqlServer['password'], $mysqlServer['database'], $mysqlServer['port'], NULL, 0)) { $index = ++$index % $totalNum; continue; } KC_LOG_DEBUG("fetch mysql conntion host [" . $mysqlServer['host'] . "] port [" . $mysqlServer['port'] . "]"); $this->_mysqli->set_charset(AppConf::$dbConf['charset']); $this->_isConnected = true; break; } if (false === $this->_isConnected) { KC_LOG_WARNING("fetch mysql conntion [identifier: {$identifier}, tag: {$tag}] failed."); return false; } return true; }