国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

關于GatewayWorker中Db單例的疑問

wodetian55

根據(jù)GatewayWorker文檔中的描述:
DB屬于單例模式,在onXXX里初始化的數(shù)據(jù)庫單例只屬于當前子進程自己所有;
那么我是不是可以如下理解:

<?php
$worker = new BusinessWorker();
$worker->name = 'YourAppBusinessWorker';
$worker->count = 4;
$worker->registerAddress = '127.0.0.1:1238';
$worker->onWorkerStart = function()
{
    Db::instance('db1');
};
$worker->onMessage = function()
{
    $ret = Db->select('name,age')->from('users')->where('age>12')->query();
}
if(!defined('GLOBAL_START'))
{
    Worker::runAll();
}

那么這樣做的話,我就有一個問題了,如果這里我需要進行2個數(shù)據(jù)庫實例怎么辦?下面這么做我感覺是不正確的:

<?php
$worker = new BusinessWorker();
$worker->name = 'YourAppBusinessWorker';
$worker->count = 4;
$worker->registerAddress = '127.0.0.1:1238';
$db1 = null;
$db2 = null;
$worker->onWorkerStart = function()
{
    $db1 = Db::instance('db1');
    $db2 = Db::instance('db2');
};
$worker->onMessage = function()
{
    $ret1 = $db1->select('name,age')->from('users')->where('age>12')->query();
    $ret2 = $db2->select('name,age')->from('users')->where('age>12')->query();
}
if(!defined('GLOBAL_START'))
{
    Worker::runAll();
}
4228 2 0
2個回答

wodetian55

大概知道這么解決這個問題了,不知道這么做對不對,請老大審閱:

<?php
$worker = new BusinessWorker();
$worker->name = 'YourAppBusinessWorker';
$worker->count = 4;
$worker->registerAddress = '127.0.0.1:1238';
$worker->onWorkerStart = function()
{
    Db::instance('db1');
    Db::instance('db2');
};
$worker->onMessage = function()
{
    Db::instance('db1')->select('name,age')->from('users')->where('age>12')->query();
    Db::instance('db1')->select('name,age')->from('users')->where('age>12')->query();
}
  • 暫無評論
walkor 打賞

對,
$worker->onWorkerStart代碼不需要,可以刪除。
Db::instance('db1')本身是單例的用法。

  • 暫無評論
年代過于久遠,無法發(fā)表回答
??