Example #1
0
 /**
  * Create command
  *
  * @param string  $key Key name
  * @param boolean $responseIterator[optional]  If true - command return iterator which read from socket buffer.
  *                                             Important: new connection will be created 
  * @return Rediska_Connection_Exec
  */
 public function create($key, $responseIterator = false)
 {
     $connection = $this->_rediska->getConnectionByKeyName($key);
     $command = array('SMEMBERS', $this->_rediska->getOption('namespace') . $key);
     $exec = new Rediska_Connection_Exec($connection, $command);
     if ($responseIterator) {
         $exec->setResponseIterator(true);
         $exec->setResponseCallback(array($this->getRediska()->getSerializer(), 'unserialize'));
     }
     return $exec;
 }
Example #2
0
 /**
  * Create command
  *
  * @param string  $key                         Key name
  * @param integer $withScores[optional]        Return values with scores. For default is false.
  * @param integer $start[optional]             Start index. For default is begin of set.
  * @param integer $end[optional]               End index. For default is end of set.
  * @param boolean $revert[optional]            Revert elements (not used in sorting). For default is false
  * @param boolean $responseIterator[optional]  If true - command return iterator which read from socket buffer.
  *                                             Important: new connection will be created 
  * @return Rediska_Connection_Exec
  */
 public function create($key, $withScores = false, $start = 0, $end = -1, $revert = false, $responseIterator = false)
 {
     $connection = $this->_rediska->getConnectionByKeyName($key);
     $command = array($revert ? 'ZREVRANGE' : 'ZRANGE', $this->_rediska->getOption('namespace') . $key, $start, $end);
     if ($withScores) {
         $command[] = 'WITHSCORES';
     }
     $exec = new Rediska_Connection_Exec($connection, $command);
     if ($responseIterator) {
         if ($withScores) {
             $responseIterator = 'Rediska_Command_GetSortedSet_WithScoresIterator';
         }
         $exec->setResponseIterator($responseIterator);
         $exec->setResponseCallback(array($this, 'parseIteratorResponse'));
     }
     return $exec;
 }
Example #3
0
 /**
  * Connect to redis server
  * 
  * @throws Rediska_Connection_Exception
  * @return boolean
  */
 public function connect()
 {
     if ($this->isConnected()) {
         return false;
     }
     $this->_socket = $this->_createSocketConnection();
     // Throw exception if can't connect
     if (!is_resource($this->_socket)) {
         $errorCode = socket_last_error();
         $errorMessage = socket_strerror($errorCode);
         $msg = "Can't connect to Redis server on {$this->getHost()}:{$this->getPort()}";
         if ($errorCode || $errorMessage) {
             $msg .= "," . ($errorCode ? " error {$errorCode}" : "") . ($errorMessage ? " {$errorMessage}" : "");
         }
         $this->_socket = null;
         throw new Rediska_Connection_Exception($msg);
     }
     // Set read timeout
     if ($this->_options['readTimeout'] != null) {
         $this->setReadTimeout($this->_options['readTimeout']);
     }
     // Set blocking mode
     //if (1 || $this->_options['blockingMode'] == false) {
     //    $this->setBlockingMode($this->_options['blockingMode']);
     //}
     // Send password
     if ($this->getPassword() != '') {
         $auth = new Rediska_Connection_Exec($this, "AUTH {$this->getPassword()}");
         try {
             $auth->execute();
         } catch (Rediska_Command_Exception $e) {
             throw new Rediska_Connection_Exception("Password error: {$e->getMessage()}");
         }
     }
     // Set db
     if ($this->_options['db'] !== self::DEFAULT_DB) {
         $selectDb = new Rediska_Connection_Exec($this, "SELECT {$this->_options['db']}");
         try {
             $selectDb->execute();
         } catch (Rediska_Command_Exception $e) {
             throw new Rediska_Connection_Exception("Select db error: {$e->getMessage()}");
         }
     }
     return true;
 }
 public function current()
 {
     if ($this->_count === null || $this->_count == 0) {
         throw new Rediska_Connection_Exception('call valid before');
     }
     $response = Rediska_Connection_Exec::readResponseFromConnection($this->_connection);
     if ($this->_callback !== null) {
         $response = call_user_func($this->_callback, $response);
     }
     return $response;
 }
Example #5
0
 public function add($aliasOrConnection)
 {
     if (!$aliasOrConnection instanceof Rediska_Connection) {
         $connection = $this->_monitor->getRediska()->getConnectionByAlias($aliasOrConnection);
     } else {
         $connection = $aliasOrConnection;
     }
     if (!array_key_exists($connection->getAlias(), $this->_connections)) {
         if (!array_key_exists($connection->getAlias(), self::$_allConnections)) {
             self::$_allConnections[$connection->getAlias()] = clone $connection;
             self::$_allConnections[$connection->getAlias()]->setBlockingMode(false);
             $connection = self::$_allConnections[$connection->getAlias()];
             $monitor = new Rediska_Connection_Exec($connection, 'MONITOR');
             $monitor->execute();
         } else {
             $connection = self::$_allConnections[$connection->getAlias()];
         }
         $this->_connections[$connection->getAlias()] = $connection;
         return true;
     }
     return false;
 }
 /**
  * Parse response
  * 
  * @param string|array $response
  * @return mixed
  */
 public function parseResponse($response)
 {
     if ($this->pushToKey !== null) {
         if (empty($response)) {
             return null;
         }
         if (!$this->isAtomic()) {
             $command = array('LPUSH', $this->_rediska->getOption('namespace') . $this->pushToKey, $response[1]);
             $exec = new Rediska_Connection_Exec($this->_storeConnection, $command);
             $exec->execute();
             $value = $response[1];
         } else {
             $value = $response;
         }
         return $this->_rediska->getSerializer()->unserialize($value);
     } else {
         if (!is_array($this->keyOrKeys) && !empty($response)) {
             $result = $this->_rediska->getSerializer()->unserialize($response[1]);
         } else {
             $result = Rediska_Command_Response_ListNameAndValue::factory($this->_rediska, $response);
         }
         return $result;
     }
 }
Example #7
0
    /**
     * Magic method for execute command in connection
     *
     * @return string
     */
    public function __invoke($command)
    {
        $exec = new Rediska_Connection_Exec($this, $command);

        return $exec->execute();
    }
Example #8
0
 /**
  * Get response from connection
  *
  * @param Rediska_Connection $connection
  * @return Rediska_PubSub_Response_Abstract
  */
 protected function _getResponseFromConnection(Rediska_Connection $connection)
 {
     $response = Rediska_Connection_Exec::readResponseFromConnection($connection);
     if ($response === null || $response === true) {
         return null;
     }
     list($type, $channel, $body) = $response;
     if ($this->getRediska()->getOption('namespace') !== '' && strpos($channel, $this->getRediska()->getOption('namespace')) === 0) {
         $channel = substr($channel, strlen($this->getRediska()->getOption('namespace')));
     }
     switch ($type) {
         case self::SUBSCRIBE:
             return new Rediska_PubSub_Response_Subscribe($connection, $channel);
         case self::UNSUBSCRIBE:
             return new Rediska_PubSub_Response_Unsubscribe($connection, $channel);
         case self::MESSAGE:
             $message = $this->getRediska()->getSerializer()->unserialize($body);
             return new Rediska_PubSub_Response_Message($connection, $channel, $message);
         default:
             throw new Rediska_PubSub_Response_Exception('Unknown reponse type: ' . $type);
     }
 }
Example #9
0
 public function count()
 {
     $exec = new Rediska_Connection_Exec($this->_connection, array('CONFIG', 'GET', '*'));
     $namesAndValues = $exec->execute();
     return count($namesAndValues) / 2;
 }
Example #10
0
 /**
  * Execute transaction
  * 
  * @return array
  */
 public function execute()
 {
     $results = array();
     $this->_rediska->getProfiler()->start($this);
     $multi = new Rediska_Connection_Exec($this->_connection, 'MULTI');
     $multi->execute();
     foreach ($this->_commands as $command) {
         $command->execute();
     }
     $exec = new Rediska_Connection_Exec($this->_connection, 'EXEC');
     $responses = $exec->execute();
     $this->_rediska->getProfiler()->stop();
     if (!$responses) {
         throw new Rediska_Transaction_AbortedException('Transaction has been aborted by server');
     }
     foreach ($this->_commands as $i => $command) {
         $results[] = $command->parseResponses(array($responses[$i]));
     }
     $this->_reset();
     return $results;
 }
Example #11
0
 /**
  * Get response from connection
  *
  * @param Rediska_Connection $connection
  * @return array|null
  */
 protected function _getResponseFromConnection(Rediska_Connection $connection)
 {
     $response = Rediska_Connection_Exec::readResponseFromConnection($connection);
     if ($response === null || $response === true) {
         return null;
     }
     $timestampAndCommand = explode(' ', $response, 2);
     $command = $timestampAndCommand[1];
     if ($command == '"MONITOR"') {
         return null;
     }
     return $timestampAndCommand;
 }
Example #12
0
 /**
  * Discard transaction
  * 
  * @return boolean
  */
 public function discard()
 {
     if (!$this->isStarted()) {
         return false;
     }
     $discard = new Rediska_Connection_Exec($this->_connection, 'DISCARD');
     $reply = $discard->execute();
     $this->_commands = array();
     $this->_isStarted = false;
     return $reply;
 }