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