/** * Gets a lazy loaded \Redis instance that is connected and optionally authenticated. * * @throws Exception * @return \Redis */ public function getRedis() { //lazy loaded connection try { if ($this->redis === null) { $this->redis = new \Redis(); if ($this->socket) { $connected = $this->redis->connect($this->socket); } else { /* default connection with different parameters */ if ($this->retry !== null) { $connected = $this->redis->connect($this->host, $this->port, $this->timeout, null, $this->retry); } else { if ($this->reserved !== null) { $connected = $this->redis->connect($this->host, $this->port, $this->timeout, $this->reserved); } else { $connected = $this->redis->connect($this->host, $this->port, $this->timeout); } } } if (!$connected) { $this->redis = null; throw new Exception('connection not made', Exception::PERSISTENCE_FAILED_TO_CONNECT); } if ($this->password) { $authenticated = $this->redis->auth($this->password); if (!$authenticated) { throw new Exception('authentication failed', Exception::PERSISTENCE_FAILED_TO_CONNECT); } } //set the database $this->redis->select($this->database); $this->redis->setOption(\Redis::OPT_PREFIX, $this->getPrefix()); //hook for subclass $this->onConnect(); } return $this->redis; } catch (\Exception $e) { throw new Exception(sprintf("error creating Redis connection: [%s]", $e->getMessage()), Exception::PERSISTENCE_FAILED_TO_CONNECT); } }