連接預熱
FastDb::getInstance()->preConnect();
方法用于預熱連接池。
為了避免連接空檔期突如其來的高并發,我們可以對數據庫連接預熱,也就是 Worker
進程啟動的時候,提前準備好數據庫連接。
對連接進行預熱使用示例如下所示:
<?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 () {
// 連接預熱
FastDb::getInstance()->preConnect();
});
}
}