You are here

public static function ClientFactory::getClient in Redis 8

Get client singleton.

File

src/ClientFactory.php, line 145

Class

ClientFactory
Common code and client singleton, for all Redis clients.

Namespace

Drupal\redis

Code

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