require_once __DIR__ . '/vendor/autoload.php';
use Clue\React\Redis\Factory;
use Clue\React\Redis\Client;
use Workerman\Worker;
$worker = new Worker('text://0.0.0.0:6161');
// 進(jìn)程啟動(dòng)時(shí)
$worker->onWorkerStart = function() {
global $factory;
$loop = Worker::getEventLoop();
$factory = new Factory($loop);
};
$worker->onMessage = function($connection, $data) {
global $factory;
$factory->createClient('localhost:6379')->then(function (Client $client) use ($connection) {
$client->set('greeting', 'Hello world');
$client->append('greeting', '!');
$client->get('greeting')->then(function ($greeting) use ($connection){
// Hello world!
echo $greeting . PHP_EOL;
$connection->send($greeting);
});
$client->incr('invocation')->then(function ($n) use ($connection){
echo 'This is invocation #' . $n . PHP_EOL;
$connection->send($n);
});
});
};
Worker::runAll();
有看到官方給出的異步redis例子中是在onMessage中$factory->createClient('localhost:6379')->then()
疑問:每次來一條消息都要去創(chuàng)建一個(gè)連接,異步查詢,并且用完就關(guān)閉該連接是否存在浪費(fèi)資源? 請(qǐng)問推薦的最佳實(shí)踐是怎樣的? 是否將這個(gè)連接進(jìn)行共用?