Esempio n. 1
0
 public function testConnectionErrorOnQuit()
 {
     $redis = $this->getRedis();
     $redis->connect('localhost');
     // interupt the connection
     phpiredis_command_bs($this->readAttribute($redis, 'connection'), array('QUIT'));
     try {
         $redis->get('something');
         $this->fail('Getting something from a disrupted connection should throw a ConnectionError');
     } catch (ConnectionError $e) {
         // everything is fine here
     }
 }
Esempio n. 2
0
 /**
  * @param $cmdArgs
  *
  * @return bool|mixed
  */
 public function query($cmdArgs)
 {
     // no connection || no commands?
     if ($this->_getRedisInstance() === FALSE || $cmdArgs === FALSE) {
         return FALSE;
     }
     // force strings only
     $cmdArgs = $this->_ensureStringCommands($cmdArgs);
     // query redis
     $response = phpiredis_command_bs($this->_getRedisInstance(), $cmdArgs);
     if (is_array($response) || substr($response, 0, 2) !== 'ERR') {
         return $response;
     }
     return FALSE;
 }
Esempio n. 3
0
 public function __call($method, $params)
 {
     if (is_null($params)) {
         return false;
     }
     array_unshift($params, $method);
     $data = phpiredis_command_bs($this->conn, $params);
     if ($data == "NULL") {
         return NULL;
     }
     if (strcasecmp($method, "ds_mget") == 0 || strcasecmp($method, "ds_hmget") == 0) {
         return $this->array_to_hash($data);
     }
     return $data;
 }
 public function testProperConnections()
 {
     $this->redis->del('testDeadConn');
     $this->assertEquals(1, $this->redis->incr('testDeadConn'));
     // now we kill it for good by setting an unreachable host
     phpiredis_command_bs($this->readAttribute($this->phpiredisA, 'connection'), array('QUIT'));
     $this->phpiredisA->prepareConnect('unreachableHost');
     // still everything should work at this place, because we have a backup connection
     $this->assertEquals(2, $this->redis->incr('testDeadConn'));
     $this->assertTrue($this->phpiredisB->isconnected());
     // restore original host
     $this->phpiredisA->prepareConnect('localhost', $this->getPort());
     // forward time
     $this->time->setNextCurrent($this->time->current() + 100);
     // we should automatically reconnect to the master host
     $this->assertEquals(3, $this->redis->incr('testDeadConn'));
     $this->assertTrue($this->phpiredisA->isconnected());
     $this->assertFalse($this->phpiredisB->isconnected());
 }
Esempio n. 5
0
 /**
  * @param array $cmd is passed by reference, the array must not be used or changed by the caller afterwards.
  * @param null $code
  * @param null $arg
  * @throws \Plista\Core\Redis\Exception
  * @return array|bool|int
  */
 protected function executeCommand(array &$cmd, $code = null, $arg = null)
 {
     if (!$this->connection) {
         // let's check whether we're connected first
         throw new ConnectionError($this->getConnectionDetails('Cannot execute commands on an unconnected instance'));
     }
     // remember post processing types in any batch mode
     if ($this->isInBatchMode()) {
         // we are storing the commands as references, to prevent the engine from doing a copy on them.
         // this gives much better performance at the cost of flexibility, as the arrays must remain
         // unchanged after this method returns (otherwise the piped commands are changed as well)
         if ($this->isInStreamMode()) {
             $this->pipeCommands[] =& $cmd;
         } else {
             $this->pipeCommands[] = array(&$cmd, $code, $arg);
         }
     }
     // in pipeline mode we exit here because we dont want to execute any commands yet
     if ($this->isInPipelineMode()) {
         return true;
     }
     // do the actual query
     $result = phpiredis_command_bs($this->connection, $cmd);
     return $this->processResult($result, $code, $arg);
 }
Esempio n. 6
0
 public function testMultiModeFailover()
 {
     $this->redis->del('testDeadConn');
     $this->assertEquals(1, $this->redis->incr('testDeadConn'));
     // lets start multi mode
     $this->assertTrue($this->redis->pipeline());
     // issue a single command
     $this->assertTrue($this->redis->incr('testDeadConn'));
     // then kill the connection
     phpiredis_command_bs($this->readAttribute($this->phpiredisA, 'connection'), array('QUIT'));
     // the next command should fail, but automatic reconnect should revive the connection
     $this->assertEquals(2, $this->redis->incr('testDeadConn'));
     $this->assertTrue($this->phpiredisA->isconnected());
     // make sure multi mode was disabled properly
     try {
         $this->redis->exec();
         $this->fail("exception not thrown");
     } catch (MultiModeException $e) {
         // all good
     }
 }