Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
php_utils
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bp
php_utils
Commits
f99797ca
Commit
f99797ca
authored
Jul 28, 2021
by
wanjilong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add: 优化配置获取处理
parent
6cdf0c87
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
28 deletions
+29
-28
MysqlClusterBase.php
src/Mysql/MysqlClusterBase.php
+29
-28
No files found.
src/Mysql/MysqlClusterBase.php
View file @
f99797ca
...
...
@@ -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
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment