連接預(yù)熱
FastDb::getInstance()->preConnect();
方法用于預(yù)熱連接池。
為了避免連接空檔期突如其來(lái)的高并發(fā),我們可以對(duì)數(shù)據(jù)庫(kù)連接預(yù)熱,也就是 Worker
進(jìn)程啟動(dòng)的時(shí)候,提前準(zhǔn)備好數(shù)據(jù)庫(kù)連接。
對(duì)連接進(jìn)行預(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();
});
}
}