ClientFactory.php in Redis 8
File
src/ClientFactory.php
View source
<?php
namespace Drupal\redis;
use Drupal\Core\Site\Settings;
class ClientFactory {
const REDIS_DEFAULT_HOST = "127.0.0.1";
const REDIS_DEFAULT_PORT = 6379;
const REDIS_DEFAULT_BASE = NULL;
const REDIS_DEFAULT_PASSWORD = NULL;
const REDIS_IMPL_CACHE = '\\Drupal\\redis\\Cache\\';
const REDIS_IMPL_LOCK = '\\Drupal\\redis\\Lock\\';
const REDIS_IMPL_FLOOD = '\\Drupal\\redis\\Flood\\';
const REDIS_IMPL_PERSISTENT_LOCK = '\\Drupal\\redis\\PersistentLock\\';
const REDIS_IMPL_CLIENT = '\\Drupal\\redis\\Client\\';
const REDIS_IMPL_QUEUE = '\\Drupal\\redis\\Queue\\';
const REDIS_IMPL_RELIABLE_QUEUE = '\\Drupal\\redis\\Queue\\Reliable';
protected static $_clientInterface;
protected static $_client;
public static function hasClient() {
return isset(self::$_client);
}
public static function setClient(ClientInterface $interface) {
if (isset(self::$_client)) {
throw new \Exception("Once Redis client is connected, you cannot change client proxy instance.");
}
self::$_clientInterface = $interface;
}
public static function getClientInterface() {
if (!isset(self::$_clientInterface)) {
$settings = Settings::get('redis.connection', []);
if (!empty($settings['interface'])) {
$className = self::getClass(self::REDIS_IMPL_CLIENT, $settings['interface']);
self::$_clientInterface = new $className();
}
elseif (class_exists('Predis\\Client')) {
$className = self::getClass(self::REDIS_IMPL_CLIENT, 'Predis');
self::$_clientInterface = new $className();
}
elseif (class_exists('Redis')) {
$className = self::getClass(self::REDIS_IMPL_CLIENT, 'PhpRedis');
self::$_clientInterface = new $className();
}
else {
if (!isset(self::$_clientInterface)) {
throw new \Exception("No client interface set.");
}
}
}
return self::$_clientInterface;
}
public static function getClientName() {
return self::getClientInterface()
->getName();
}
public static function getClient() {
if (!isset(self::$_client)) {
$settings = Settings::get('redis.connection', []);
$settings += [
'host' => self::REDIS_DEFAULT_HOST,
'port' => self::REDIS_DEFAULT_PORT,
'base' => self::REDIS_DEFAULT_BASE,
'password' => self::REDIS_DEFAULT_PASSWORD,
'persistent' => FALSE,
];
if (isset($settings['replication']) && $settings['replication'] === TRUE) {
foreach ($settings['replication.host'] as $key => $replicationHost) {
if (!isset($replicationHost['port'])) {
$settings['replication.host'][$key]['port'] = self::REDIS_DEFAULT_PORT;
}
}
self::$_client = self::getClientInterface()
->getClient($settings['host'], $settings['port'], $settings['base'], $settings['password'], $settings['replication.host'], $settings['persistent']);
}
else {
self::$_client = self::getClientInterface()
->getClient($settings['host'], $settings['port'], $settings['base'], $settings['password'], [], $settings['persistent']);
}
}
return self::$_client;
}
public static function getClass($system, $clientName = NULL) {
$className = $system . ($clientName ?: self::getClientName());
if (!class_exists($className)) {
throw new \Exception($className . " does not exists");
}
return $className;
}
public static function reset() {
self::$_clientInterface = null;
self::$_client = null;
}
}
Classes
Name |
Description |
ClientFactory |
Common code and client singleton, for all Redis clients. |