我已经使用 documented 设置了一个新的 PHP predis
连接到 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()
似乎不起作用。
最佳答案
正如@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
}
关于PHP predis 连接到 Redis Sentinel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38128322/