本文整理汇总了PHP中Env::getIDC方法的典型用法代码示例。如果您正苦于以下问题:PHP Env::getIDC方法的具体用法?PHP Env::getIDC怎么用?PHP Env::getIDC使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Env
的用法示例。
在下文中一共展示了Env::getIDC方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getConnection
/**
* 获取一个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;
}
示例2: getRedis
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;
}
示例3: fetchMysqlHandler
/**
*
* 连接到数据库
* @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;
}