yii\redis\Connection::open PHP Метод

open() публичный Метод

It does nothing if a DB connection has already been established.
public open ( )
    public function open()
    {
        if ($this->_socket !== false) {
            return;
        }
        $connection = ($this->unixSocket ?: $this->hostname . ':' . $this->port) . ', database=' . $this->database;
        \Yii::trace('Opening redis DB connection: ' . $connection, __METHOD__);
        $this->_socket = @stream_socket_client($this->unixSocket ? 'unix://' . $this->unixSocket : 'tcp://' . $this->hostname . ':' . $this->port, $errorNumber, $errorDescription, $this->connectionTimeout ? $this->connectionTimeout : ini_get('default_socket_timeout'), $this->socketClientFlags);
        if ($this->_socket) {
            if ($this->dataTimeout !== null) {
                stream_set_timeout($this->_socket, $timeout = (int) $this->dataTimeout, (int) (($this->dataTimeout - $timeout) * 1000000));
            }
            if ($this->password !== null) {
                $this->executeCommand('AUTH', [$this->password]);
            }
            if ($this->database !== null) {
                $this->executeCommand('SELECT', [$this->database]);
            }
            $this->initConnection();
        } else {
            \Yii::error("Failed to open redis DB connection ({$connection}): {$errorNumber} - {$errorDescription}", __CLASS__);
            $message = YII_DEBUG ? "Failed to open redis DB connection ({$connection}): {$errorNumber} - {$errorDescription}" : 'Failed to open DB connection.';
            throw new Exception($message, $errorDescription, $errorNumber);
        }
    }

Usage Example

Пример #1
0
 /**
  * @param  boolean    $reset whether to clean up the test database
  * @return Connection
  */
 public function getConnection($reset = true)
 {
     $databases = $this->getParam('databases');
     $params = isset($databases['redis']) ? $databases['redis'] : [];
     $db = new Connection($params);
     if ($reset) {
         $db->open();
         $db->flushdb();
     }
     return $db;
 }