webman怎么在onWorkerStart中实例化数据库?

webman怎么在onWorkerStart中实例化数据库?以保证每个进程只需要连接一次数据库即可,降低数据库连接数

794 1 1
1个回答

2548a

在这个目录 support\bootstrap

use Webman\Bootstrap;

class ThinkOrm implements Bootstrap
{

/**
 * @param $worker
 * @return mixed|void
 */
public static function start($worker){
    // 配置
    Db::setConfig(config('thinkorm'));
    // 维持mysql心跳
    if ($worker) {
        Timer::add(55, function () {
            $connections = config('thinkorm.connections', []);
            foreach ($connections as $key => $item) {
                if ($item['type'] == 'mysql') {
                    Db::connect($key)->query('select 1');
                }
            }
        });
    }
}

}

然后在 config/bootstrap 文件

return [
\support\bootstrap\ThinkOrm::class,
];

  • 暂无评论
年代过于久远,无法发表回答
🔝