PhpRedis.php in Redis 8
File
src/Client/PhpRedis.php
View source
<?php
namespace Drupal\redis\Client;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Site\Settings;
use Drupal\redis\ClientInterface;
class PhpRedis implements ClientInterface {
public function getClient($host = NULL, $port = NULL, $base = NULL, $password = NULL, $replicationHosts = [], $persistent = FALSE) {
$client = new \Redis();
if (is_array($host)) {
$ip_host = $this
->askForMaster($client, $host, $password);
if (is_array($ip_host)) {
list($host, $port) = $ip_host;
}
}
if ($persistent) {
$client
->pconnect($host, $port);
}
else {
$client
->connect($host, $port);
}
if (isset($password)) {
$client
->auth($password);
}
if (isset($base)) {
$client
->select($base);
}
$client
->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_NONE);
return $client;
}
public function getName() {
return 'PhpRedis';
}
protected function askForMaster(\Redis $client, array $sentinels = [], $password = NULL) {
$ip_port = NULL;
$settings = Settings::get('redis.connection', []);
$settings += [
'instance' => NULL,
];
if ($settings['instance']) {
foreach ($sentinels as $sentinel) {
list($host, $port) = explode(':', $sentinel);
set_error_handler(function () {
return TRUE;
});
$success = $client
->connect($host, $port, 0.5);
restore_error_handler();
if (!$success) {
continue;
}
if (isset($password)) {
$client
->auth($password);
}
if ($client
->isConnected()) {
$ip_port = $client
->rawcommand('SENTINEL', 'get-master-addr-by-name', $settings['instance']);
if ($ip_port) {
break;
}
}
$client
->close();
}
}
return $ip_port;
}
}
Classes
Name |
Description |
PhpRedis |
PhpRedis client specific implementation. |