Commit f99797ca authored by wanjilong's avatar wanjilong

add: 优化配置获取处理

parent 6cdf0c87
......@@ -158,10 +158,7 @@ abstract class MysqlClusterBase
}
// 读取配置文件内容
self::$iniConf = config('mysql', static::CONFIG_INDEX);
if (!self::$iniConf) {
throw new \Exception('mysql config not exist');
}
self::loadCfg();
// 获取库分片索引
$dbShardingIndex = self::getDatabaseShardingIndex();
......@@ -256,17 +253,11 @@ abstract class MysqlClusterBase
*
* @param string $type 读或写类型
* @return void
* @throws \Exception
*/
private static function getDbServerPort($type, $dbShardingIndex)
{
if(empty(self::$iniConf)) {
//重新加载 ini
self::$iniConf = config('mysql', static::CONFIG_INDEX);
if (!self::$iniConf) {
throw new \Exception('mysql yaconf ini config not exist');
}
}
self::loadCfg();
$server = '';
$port = 0;
$host = self::$iniConf[$type]['host'];
......@@ -315,8 +306,12 @@ abstract class MysqlClusterBase
* 获取数据库分片数量
*
* @return integer
* @throws \Exception
*/
private static function getDbShardingCount() {
self::loadCfg();
$host = self::$iniConf[static::WRITE]['host'];
$hostList = explode(',', $host);
self::$dbShardingCount = count($hostList);
......@@ -401,9 +396,12 @@ abstract class MysqlClusterBase
* 获取ini中的option配置,必须用\PDO::ATTR_STRINGIFY_FETCHES对应的int数值
*
* @return array
* @throws \Exception
*/
public static function getDbOptions()
{
self::loadCfg();
$options = [];
if (isset(self::$iniConf['options'])) {
foreach (self::$iniConf['options'] as $item) {
......@@ -579,6 +577,21 @@ abstract class MysqlClusterBase
return false;
}
/**
* @param bool $flush
* @throws \Exception
* 加载配置文件,支持强更新
*/
private static function loadCfg($flush = false) {
if($flush || empty(self::$iniConf)) {
//重新加载 ini
self::$iniConf = config('mysql', static::CONFIG_INDEX);
if (!self::$iniConf) {
throw new \Exception('mysql yaconf ini config not exist');
}
}
}
/**
* 格式化返回结果
*
......@@ -635,6 +648,7 @@ abstract class MysqlClusterBase
*
* @param int $dbShardingColumnValue
* @return bool
* @throws \Exception
*/
public static function beginTransaction($dbShardingColumnValue = 0, $dbShardingCompatColumnValue = 0)
{
......@@ -642,10 +656,7 @@ abstract class MysqlClusterBase
self::$dbShardingCompatColumnValue = $dbShardingCompatColumnValue;
// 读取配置文件内容
self::$iniConf = config('mysql', static::CONFIG_INDEX);
if (!self::$iniConf) {
throw new \Exception('mysql config not exist');
}
self::loadCfg();
// 获取库分片索引
self::$transationIndex = self::getDatabaseShardingIndex();
......@@ -734,15 +745,10 @@ abstract class MysqlClusterBase
* 获取所有分片连接
*
* @return array
* @throws \Exception
*/
public static function getWriteConnectionList()
{
// 读取配置文件内容
self::$iniConf = config('mysql', static::CONFIG_INDEX);
if (!self::$iniConf) {
throw new \Exception('mysql config not exist');
}
// 获取库分片数量
self::getDbShardingCount();
......@@ -757,15 +763,10 @@ abstract class MysqlClusterBase
* 获取所有分片连接
*
* @return array
* @throws \Exception
*/
public static function getReadConnectionList()
{
// 读取配置文件内容
self::$iniConf = config('mysql', static::CONFIG_INDEX);
if (!self::$iniConf) {
throw new \Exception('mysql config not exist');
}
// 获取库分片数量
self::getDbShardingCount();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment