Exemple #1
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 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;
     }
 }
Exemple #4
0
    /**
     * Magic method for execute command in connection
     *
     * @return string
     */
    public function __invoke($command)
    {
        $exec = new Rediska_Connection_Exec($this, $command);

        return $exec->execute();
    }
Exemple #5
0
 public function count()
 {
     $exec = new Rediska_Connection_Exec($this->_connection, array('CONFIG', 'GET', '*'));
     $namesAndValues = $exec->execute();
     return count($namesAndValues) / 2;
 }
Exemple #6
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;
 }
Exemple #7
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;
 }