Esempio n. 1
0
 public function connect()
 {
     if (!extension_loaded('redis')) {
         throw new MissingExtensionException("Please install and enable the redis extension. \nhttps://github.com/nicolasff/phpredis/");
     }
     if (!$this->driver) {
         $this->driver = new Driver\PhpRedisDriver();
     }
     if ($this->driver->isConnected()) {
         return;
     }
     $remaining = $this->connectionAttempts;
     $errors = array();
     do {
         try {
             if ($this->persistent) {
                 $isConnected = $this->driver->pconnect($this->host, $this->port, $this->timeout);
             } else {
                 $isConnected = $this->driver->connect($this->host, $this->port, $this->timeout);
             }
             if (!$isConnected || !$this->driver->isConnected()) {
                 $errorMessage = $this->driver->getLastError();
                 $this->driver->clearLastError();
                 throw new ConnectionException(sprintf('Connecting to %s failed: %s', $this->formatServerAddress(), $errorMessage));
             }
             if (isset($this->auth)) {
                 $this->driver->auth($this->auth);
             }
             $this->driver->select($this->database);
             $this->isConnected = $this->driver->isConnected();
             return;
         } catch (\Exception $e) {
             $errors[] = $e;
             if (!Debugger::$productionMode) {
                 break;
             }
             usleep(1000 * $this->connectionAttempts);
         } catch (\Throwable $e) {
             $errors[] = $e;
             break;
         }
     } while (--$remaining > 0);
     if ($e = reset($errors)) {
         $errorMessage = $this->driver->getLastError();
         $this->driver->clearLastError();
         throw new RedisClientException(sprintf('Client of %s; %s; %s', $this->formatServerAddress(), $e->getMessage(), $errorMessage), $e->getCode(), $e);
     }
 }
 public function connect()
 {
     if (!$this->driver) {
         $this->driver = new Driver\PhpRedisDriver();
     }
     if ($this->driver->isConnected()) {
         return;
     }
     try {
         if ($this->persistent) {
             $this->driver->pconnect($this->host, $this->port, $this->timeout);
         } else {
             $this->driver->connect($this->host, $this->port, $this->timeout);
         }
         if (isset($this->auth)) {
             $this->driver->auth($this->auth);
         }
         $this->driver->select($this->database);
         $this->isConnected = $this->driver->isConnected();
     } catch (\Exception $e) {
         throw new RedisClientException($e->getMessage(), $e->getCode(), $e);
     }
 }