You are here

class ClientFactory in Redis 8

Common code and client singleton, for all Redis clients.

Hierarchy

Expanded class hierarchy of ClientFactory

16 files declare their use of ClientFactory
CacheBackendFactory.php in src/Cache/CacheBackendFactory.php
FloodFactory.php in src/Flood/FloodFactory.php
LockFactory.php in src/Lock/LockFactory.php
PhpRedis.php in src/Lock/PhpRedis.php
PhpRedis.php in src/Flood/PhpRedis.php

... See full list

1 string reference to 'ClientFactory'
redis.services.yml in ./redis.services.yml
redis.services.yml
1 service uses ClientFactory
redis.factory in ./redis.services.yml
Drupal\redis\ClientFactory

File

src/ClientFactory.php, line 9

Namespace

Drupal\redis
View source
class ClientFactory {

  /**
   * Redis default host.
   */
  const REDIS_DEFAULT_HOST = "127.0.0.1";

  /**
   * Redis default port.
   */
  const REDIS_DEFAULT_PORT = 6379;

  /**
   * Redis default database: will select none (Database 0).
   */
  const REDIS_DEFAULT_BASE = NULL;

  /**
   * Redis default password: will not authenticate.
   */
  const REDIS_DEFAULT_PASSWORD = NULL;

  /**
   * Cache implementation namespace.
   */
  const REDIS_IMPL_CACHE = '\\Drupal\\redis\\Cache\\';

  /**
   * Lock implementation namespace.
   */
  const REDIS_IMPL_LOCK = '\\Drupal\\redis\\Lock\\';

  /**
   * Lock implementation namespace.
   */
  const REDIS_IMPL_FLOOD = '\\Drupal\\redis\\Flood\\';

  /**
   * Persistent Lock implementation namespace.
   */
  const REDIS_IMPL_PERSISTENT_LOCK = '\\Drupal\\redis\\PersistentLock\\';

  /**
   * Client implementation namespace.
   */
  const REDIS_IMPL_CLIENT = '\\Drupal\\redis\\Client\\';

  /**
   * Queue implementation namespace.
   */
  const REDIS_IMPL_QUEUE = '\\Drupal\\redis\\Queue\\';

  /**
   * Reliable queue implementation namespace.
   */
  const REDIS_IMPL_RELIABLE_QUEUE = '\\Drupal\\redis\\Queue\\Reliable';

  /**
   * @var \Drupal\redis\ClientInterface
   */
  protected static $_clientInterface;

  /**
   * @var mixed
   */
  protected static $_client;
  public static function hasClient() {
    return isset(self::$_client);
  }

  /**
   * Set client proxy.
   */
  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;
  }

  /**
   * Lazy instantiates client proxy depending on the actual configuration.
   *
   * If you are using a lock or cache backend using one of the Redis client
   * implementations, this will be overridden at early bootstrap phase and
   * configuration will be ignored.
   *
   * @return ClientInterface
   */
  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')) {

        // Transparent and arbitrary preference for Predis library.
        $className = self::getClass(self::REDIS_IMPL_CLIENT, 'Predis');
        self::$_clientInterface = new $className();
      }
      elseif (class_exists('Redis')) {

        // Fallback on PhpRedis if available.
        $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;
  }

  /**
   * Get underlying library name.
   *
   * @return string
   */
  public static function getClientName() {
    return self::getClientInterface()
      ->getName();
  }

  /**
   * Get client singleton.
   */
  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 using replication, lets create the client appropriately.
      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;
  }

  /**
   * Get specific class implementing the current client usage for the specific
   * asked core subsystem.
   *
   * @param string $system
   *   One of the ClientFactory::IMPL_* constant.
   * @param string $clientName
   *   Client name, if fixed.
   *
   * @return string
   *   Class name, if found.
   *
   * @throws \Exception
   *   If not found.
   */
  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;
  }

  /**
   * For unit testing only reset internals.
   */
  public static function reset() {
    self::$_clientInterface = null;
    self::$_client = null;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClientFactory::$_client protected static property
ClientFactory::$_clientInterface protected static property
ClientFactory::getClass public static function Get specific class implementing the current client usage for the specific asked core subsystem.
ClientFactory::getClient public static function Get client singleton.
ClientFactory::getClientInterface public static function Lazy instantiates client proxy depending on the actual configuration.
ClientFactory::getClientName public static function Get underlying library name.
ClientFactory::hasClient public static function
ClientFactory::REDIS_DEFAULT_BASE constant Redis default database: will select none (Database 0).
ClientFactory::REDIS_DEFAULT_HOST constant Redis default host.
ClientFactory::REDIS_DEFAULT_PASSWORD constant Redis default password: will not authenticate.
ClientFactory::REDIS_DEFAULT_PORT constant Redis default port.
ClientFactory::REDIS_IMPL_CACHE constant Cache implementation namespace.
ClientFactory::REDIS_IMPL_CLIENT constant Client implementation namespace.
ClientFactory::REDIS_IMPL_FLOOD constant Lock implementation namespace.
ClientFactory::REDIS_IMPL_LOCK constant Lock implementation namespace.
ClientFactory::REDIS_IMPL_PERSISTENT_LOCK constant Persistent Lock implementation namespace.
ClientFactory::REDIS_IMPL_QUEUE constant Queue implementation namespace.
ClientFactory::REDIS_IMPL_RELIABLE_QUEUE constant Reliable queue implementation namespace.
ClientFactory::reset public static function For unit testing only reset internals.
ClientFactory::setClient public static function Set client proxy.