gpt4 book ai didi

PHP predis 连接到 Redis Sentinel

转载 作者:可可西里 更新时间:2023-11-01 11:33:41 26 4
gpt4 key购买 nike

我已经使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com