0

我已经使用记录的代码建立了到服务器的新 PHPpredis连接。我可以验证它是否连接正常,但我不知道如何简单地测试连接是否存在。redis

$options = array(
    'scheme' => 'tcp',
    'host' => '127.0.0.1',
    'port' => $port,
    'instance' => 'myinstance',
    'timeout' => '0.100' // possibly add this to connections?
);

$sentinels = [
    "tcp://{$options['host']}:{$options['port']}?timeout={$options['timeout']}",
];

$connection = new Predis\Client($sentinels, [
    'replication' => 'sentinel',
    'service' => $options['instance'],
]);

if ($connection->isConnected() === true) {
// undocumented-- I don't think this works?
    echo "connected";
} else {
    echo "failed"; // this is what gets echoed
}

有没有一种方法可以测试连接是否良好,但实际上没有读/写它?isConnected()似乎不起作用。

4

1 回答 1

0

正如@drot 所建议的:

$options = array(
    'scheme' => 'tcp',
    'host' => '127.0.0.1',
    'port' => $port,
    'instance' => 'myinstance',
    'timeout' => '0.100' // possibly add this to connections?
);

$sentinels = [
   "tcp://{$options['host']}:{$options['port']}?timeout={$options['timeout']}",
];

$connection = new Predis\Client($sentinels, [
    'replication' => 'sentinel',
    'service' => $options['instance'],
]);

// do this: 
$connection->connect();

if ($connection->isConnected() === true) {
    // undocumented-- I don't think this works?
    echo "connected";
} else {
    echo "failed"; // this is what gets echoed
}
于 2019-02-03T00:06:51.640 回答