简体   繁体   中英

PHP predis connect to Redis Sentinel

I've set up a new PHP predis connection to a redis server using the documented code. I can verify it's connected ok, but I don't know how to simply test if the connection exists.

$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
}

Is there a method to test if the connection is good short of actually reading / writing to it? isConnected() doesn't appear to work.

As suggested by @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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM