PHP predis 连接到 Redis Sentinel

标签 php redis predis redis-sentinel

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

相关文章:

php - 如何指示 predis 在出现错误时继续

azure - StackExchange.Redis 和 StackExchange.Redis.StrongName 之间有什么区别?

redis - Predis - 从连接池中删除服务器

php - 如何在 Laravel 中使用 Redis 缓存?

php - 使用复选框和按钮从 MySQL 表中删除行

php - 手动提交 Symfony 表单后 CollectionType 字段显示为空

php - 在同一查询中访问不同服务器上的两个不同数据库

php - Android json php mysql 仅返回单行

python - python对象的自动redis查找

php - 从 Predis 订阅函数中触发 Laravel 事件