FastDb 類使用
調(diào)用方法說明
addDb
用于注冊連接池。
<?php
$config = new \EasySwoole\FastDb\Config([
'name' => 'default', // 設(shè)置 連接池名稱,默認為 default
'host' => '127.0.0.1', // 設(shè)置 數(shù)據(jù)庫 host
'user' => 'easyswoole', // 設(shè)置 數(shù)據(jù)庫 用戶名
'password' => 'easyswoole', // 設(shè)置 數(shù)據(jù)庫 用戶密碼
'database' => 'easyswoole', // 設(shè)置 數(shù)據(jù)庫庫名
'port' => 3306, // 設(shè)置 數(shù)據(jù)庫 端口
'timeout' => 5, // 設(shè)置 數(shù)據(jù)庫連接超時時間
'charset' => 'utf8', // 設(shè)置 數(shù)據(jù)庫字符編碼,默認為 utf8
'autoPing' => 5, // 設(shè)置 自動 ping 客戶端鏈接的間隔
'useMysqli' => false, // 設(shè)置 不使用 php mysqli 擴展連接數(shù)據(jù)庫
// 配置 數(shù)據(jù)庫 連接池配置,配置詳細說明請看連接池組件 http://www.fe88.cn/Components/Pool/introduction.html
// 下面的參數(shù)可使用組件提供的默認值
'intervalCheckTime' => 15 * 1000, // 設(shè)置 連接池定時器執(zhí)行頻率
'maxIdleTime' => 10, // 設(shè)置 連接池對象最大閑置時間 (秒)
'maxObjectNum' => 20, // 設(shè)置 連接池最大數(shù)量
'minObjectNum' => 5, // 設(shè)置 連接池最小數(shù)量
'getObjectTimeout' => 3.0, // 設(shè)置 獲取連接池的超時時間
'loadAverageTime' => 0.001, // 設(shè)置 負載閾值
]);
// 或使用對象設(shè)置屬性方式進行配置
// $config->setName('default');
// $config->setHost('127.0.0.1');
FastDb::getInstance()->addDb($config);
testDb
用于測試連接池的數(shù)據(jù)庫配置是否可用。
FastDb::getInstance()->testDb();
FastDb::getInstance()->testDb('read');
FastDb::getInstance()->testDb('write');
setOnQuery
設(shè)置連接池連接執(zhí)行 SQL
查詢時的回調(diào),可用于監(jiān)聽 SQL
,可查看監(jiān)聽 SQL
章節(jié)。
<?php
FastDb::getInstance()->setOnQuery(function (\asySwoole\FastDb\Mysql\QueryResult $queryResult) {
// 打印 sql
if ($queryResult->getQueryBuilder()) {
echo $queryResult->getQueryBuilder()->getLastQuery() . "\n";
} else {
echo $queryResult->getRawSql() . "\n";
}
});
invoke
可用于執(zhí)行數(shù)據(jù)庫操作。
在高并發(fā)情況下,資源浪費的占用時間越短越好,可以提高程序的服務(wù)效率。
ORM
默認情況下都是使用 defer
方法獲取 pool
內(nèi)的連接資源,并在協(xié)程退出時自動歸還,在此情況下,在帶來便利的同時,會造成不必要資源的浪費。
我們可以使用 invoke
方式,讓 ORM
查詢結(jié)束后馬上歸還資源,可以提高資源的利用率。
<?php
$builder = new \EasySwoole\Mysqli\QueryBuilder();
$builder->raw('select * from user');
$result = FastDb::getInstance()->invoke(function (\EasySwoole\FastDb\Mysql\Connection $connection) use ($builder) {
$connection->query($builder);
return $connection->rawQuery("select * from user");
});
begin
啟動事務(wù)。
FastDb::getInstance()->begin();
commit
提交事務(wù)。
FastDb::getInstance()->commit();
rollback
回滾事務(wù)。
FastDb::getInstance()->rollback();
query
自定義 SQL
執(zhí)行。
$builder = new \EasySwoole\Mysqli\QueryBuilder();
$builder->raw("select * from user where id = ?", [1]);
FastDb::getInstance()->query($builder);
原生
SQL
表達式將會被當做字符串注入到查詢中,因此你應(yīng)該小心使用,避免創(chuàng)建SQL
注入的漏洞。
rawQuery
自定義 SQL
執(zhí)行。
FastDb::getInstance()->rawQuery('select * from user where id = 1');
原生
SQL
表達式將會被當做字符串注入到查詢中,因此你應(yīng)該小心使用,避免創(chuàng)建SQL
注入的漏洞。
currentConnection
獲取當前所用的連接。
FastDb::getInstance()->currentConnection();
reset
銷毀所有連接池。
FastDb::getInstance()->reset();
preConnect
用于預(yù)熱連接池。
為了避免連接空檔期突如其來的高并發(fā),我們可以對數(shù)據(jù)庫連接預(yù)熱,也就是 Worker
進程啟動的時候,提前準備好數(shù)據(jù)庫連接。
對連接進行預(yù)熱使用示例如下所示:
<?php
namespace EasySwoole\EasySwoole;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\FastDb\FastDb;
class EasySwooleEvent implements Event
{
public static function initialize()
{
date_default_timezone_set('Asia/Shanghai');
$mysqlArrayConfig = Config::getInstance()->getConf('MYSQL');
$config = new \EasySwoole\FastDb\Config($mysqlArrayConfig);
FastDb::getInstance()->addDb($config);
}
public static function mainServerCreate(EventRegister $register)
{
$register->add($register::onWorkerStart, function () {
// 連接預(yù)熱
FastDb::getInstance()->preConnect();
});
}
}
isInTransaction
當前連接是否處于事務(wù)中。
FastDb::getInstance()->isInTransaction();
getConfig
根據(jù)連接池名稱獲取當前連接池配置。
FastDb::getInstance()->getConfig();
FastDb::getInstance()->getConfig('read');