You are here

class Redis_Client in Redis 7

Same name and namespace in other branches
  1. 7.3 lib/Redis/Client.php \Redis_Client
  2. 7.2 lib/Redis/Client.php \Redis_Client

Common code and client singleton, for all Redis clients.

Hierarchy

Expanded class hierarchy of Redis_Client

File

lib/Redis/Client.php, line 12

View source
class Redis_Client {

  /**
   * 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;

  /**
   * @var Redis_Client_Interface
   */
  protected static $_clientInterface;

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

  /**
   * Set client proxy.
   */
  public static function setClient(Redis_Client_Interface $interface) {
    if (isset(self::$_client)) {
      throw new Exception("Once Redis client is connected, you cannot change client proxy instance.");
    }
    self::$_clientInterface = $interface;
  }

  /**
   * Lazy instanciate client proxy depending on the actual configuration.
   *
   * If you are using a lock, session or cache backend using one of the Redis
   * client implementation, this will be overrided at early bootstrap phase
   * and configuration will be ignored.
   *
   * @return Redis_Client_Interface
   */
  public static function getClientInterface() {
    if (!isset(self::$_clientInterface)) {
      global $conf;
      if (isset($conf['redis_client_interface']) && class_exists($conf['redis_client_interface'])) {
        self::$_clientInterface = new $conf['redis_client_interface']();
      }
      else {
        if (!isset(self::$_clientInterface)) {
          throw new Exception("No client interface set.");
        }
      }
    }
    return self::$_clientInterface;
  }

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

  /**
   * Get client singleton.
   */
  public static function getClient() {
    if (!isset(self::$_client)) {
      global $conf;

      // Always prefer socket connection.
      self::$_client = self::getClientInterface()
        ->getClient(isset($conf['redis_cache_host']) ? $conf['redis_client_host'] : self::REDIS_DEFAULT_HOST, isset($conf['redis_cache_port']) ? $conf['redis_client_port'] : self::REDIS_DEFAULT_PORT, isset($conf['redis_cache_base']) ? $conf['redis_client_base'] : self::REDIS_DEFAULT_BASE);
    }
    return self::$_client;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Redis_Client::$_client protected static property
Redis_Client::$_clientInterface protected static property
Redis_Client::getClient public static function Get client singleton.
Redis_Client::getClientInterface public static function Lazy instanciate client proxy depending on the actual configuration.
Redis_Client::getClientName public static function Get underlaying library name.
Redis_Client::hasClient public static function
Redis_Client::REDIS_DEFAULT_BASE constant Redis default database: will select none (Database 0).
Redis_Client::REDIS_DEFAULT_HOST constant Redis default host.
Redis_Client::REDIS_DEFAULT_PORT constant Redis default port.
Redis_Client::setClient public static function Set client proxy.