/** * Returns the wrapped Redis client instance. * * This method is called from AbstractRedisWrapper, who uses it to call the desired method directly * on the wrapped redis, rather than using call_user_func(). * The command and key are passed into the method to allow for some logic within the implementations * of this method. * * @param string $command the current command * @param string|null $key the current key if available, null otherwise. Commands that operate on * multiple keys will pass their keys as a comma separated string. * @throws Exception when an error happens during connecting * @return Redis */ protected function getWrappedRedis($command, $key = null) { // if we don't have a client yet, we never connected, so let's do it now if (!$this->client) { // gotta make sure the app actually tried to connect first if (!$this->connectWasCalled) { throw new Exception('call connect() or pconnect() first'); } if (!($this->persistent ? $this->conn->pconnect($this->host, $this->port, $this->timeout) : $this->conn->connect($this->host, $this->port, $this->timeout))) { throw new Exception('could not connect to ' . $this->conn->getUniqueId() . ' params: ' . (!empty($this->host) ? $this->host . ':' . $this->port : 'none')); } $this->client = $this->conn->getClient(); } return $this->client; }
/** * This method uses the stored connection parameters to connect an instance. * * @param Connection $conn * @return bool */ protected function connectInstance(Connection $conn) { // connect() and pconnect() can throw exceptions, which we just want to treat as connection failures // TODO: really? can they? try { $res = $this->origPersistent ? $conn->pconnect($this->origHost, $this->origPort, $this->origTimeout) : $conn->connect($this->origHost, $this->origPort, $this->origTimeout); } catch (ConnectionError $e) { return false; } if ($this->origDbindex) { $res = $res && $conn->select($this->origDbindex); } return $res; }
/** * Test connecting to invalid host persistently */ public function testPconnectInvalidHost() { $this->redis->disconnect(); $this->assertFalse($this->redis->pconnect('ThisHostCanNotPossiblyExist')); $this->assertFalse($this->redis->isconnected()); }