Credis_Client::connect PHP Method

connect() public method

public connect ( ) : Credis_Client
return Credis_Client
    public function connect()
    {
        if ($this->connected) {
            return $this;
        }
        if ($this->standalone) {
            $flags = STREAM_CLIENT_CONNECT;
            $remote_socket = $this->port === NULL ? 'unix://' . $this->host : 'tcp://' . $this->host . ':' . $this->port;
            if ($this->persistent && $this->port !== NULL) {
                // Persistent connections to UNIX sockets are not supported
                $remote_socket .= '/' . $this->persistent;
                $flags = $flags | STREAM_CLIENT_PERSISTENT;
            }
            $result = $this->redis = @stream_socket_client($remote_socket, $errno, $errstr, $this->timeout !== null ? $this->timeout : 2.5, $flags);
        } else {
            if (!$this->redis) {
                $this->redis = new Redis();
            }
            $result = $this->persistent ? $this->redis->pconnect($this->host, $this->port, $this->timeout, $this->persistent) : $this->redis->connect($this->host, $this->port, $this->timeout);
        }
        // Use recursion for connection retries
        if (!$result) {
            $this->connectFailures++;
            if ($this->connectFailures <= $this->maxConnectRetries) {
                return $this->connect();
            }
            $failures = $this->connectFailures;
            $this->connectFailures = 0;
            throw new CredisException("Connection to Redis {$this->host}:{$this->port} failed after {$failures} failures." . (isset($errno) && isset($errstr) ? "Last Error : ({$errno}) {$errstr}" : ""));
        }
        $this->connectFailures = 0;
        $this->connected = TRUE;
        // Set read timeout
        if ($this->readTimeout) {
            $this->setReadTimeout($this->readTimeout);
        }
        if ($this->authPassword) {
            $this->auth($this->authPassword);
        }
        if ($this->selectedDb !== 0) {
            $this->select($this->selectedDb);
        }
        return $this;
    }

Usage Example

 /**
  * Check DB connection
  *
  * @return bool
  */
 public function hasConnection()
 {
     if (!$this->_useRedis) {
         return parent::hasConnection();
     }
     try {
         $this->_redis->connect();
         if ($this->_logLevel >= 7) {
             Mage::log(sprintf("%s: Connected to Redis", $this->_getPid()), Zend_Log::DEBUG, self::LOG_FILE);
             // reset timer
             $this->_timeStart = microtime(true);
         }
         return TRUE;
     } catch (Exception $e) {
         Mage::logException($e);
         $this->_redis = NULL;
         if ($this->_logLevel >= 0) {
             Mage::log(sprintf("%s: Unable to connect to Redis; falling back to MySQL handler", $this->_getPid()), Zend_Log::EMERG, self::LOG_FILE);
         }
         // Fall-back to MySQL handler. If this fails, the file handler will be used.
         $this->_useRedis = FALSE;
         parent::__construct();
         return parent::hasConnection();
     }
 }
All Usage Examples Of Credis_Client::connect