/** * Handles a multi-bulk reply returned by Redis. * * @param IConnectionComposable $connection Connection to Redis. * @param string $lengthString Number of items in the multi-bulk reply. * @return array */ public function handle(IConnectionComposable $connection, $lengthString) { $length = (int) $lengthString; if ($length != $lengthString) { Helpers::onCommunicationException(new ProtocolException($connection, "Cannot parse '{$length}' as data length")); } if ($length === -1) { return null; } $list = array(); if ($length > 0) { $handlersCache = array(); $reader = $connection->getProtocol()->getReader(); for ($i = 0; $i < $length; $i++) { $header = $connection->readLine(); $prefix = $header[0]; if (isset($handlersCache[$prefix])) { $handler = $handlersCache[$prefix]; } else { $handler = $reader->getHandler($prefix); $handlersCache[$prefix] = $handler; } $list[$i] = $handler->handle($connection, substr($header, 1)); } } return $list; }
/** * Handles a multi-bulk reply returned by Redis in a streamable fashion. * * @param ComposableConnectionInterface $connection Connection to Redis. * @param string $lengthString Number of items in the multi-bulk reply. * @return MultiBulkResponseSimple */ public function handle(ComposableConnectionInterface $connection, $lengthString) { $length = (int) $lengthString; if ("{$length}" != $lengthString) { Helpers::onCommunicationException(new ProtocolException($connection, "Cannot parse '{$lengthString}' as multi-bulk length")); } return new MultiBulkResponseSimple($connection, $length); }
/** * Handles a multi-bulk reply returned by Redis in a streamable fashion. * * @param IConnectionComposable $connection Connection to Redis. * @param string $lengthString Number of items in the multi-bulk reply. * @return MultiBulkResponseSimple */ public function handle(IConnectionComposable $connection, $lengthString) { $length = (int) $lengthString; if ($length != $lengthString) { Helpers::onCommunicationException(new ProtocolException($connection, "Cannot parse '{$length}' as data length")); } return new MultiBulkResponseSimple($connection, $length); }
/** * @group disconnected */ public function testExtractKeyTag() { $this->assertEquals('foo:bar', Helpers::extractKeyTag('foo:bar')); $this->assertEquals('foo:', Helpers::extractKeyTag('{foo:}bar')); $this->assertEquals('bar', Helpers::extractKeyTag('foo:{bar}')); $this->assertEquals('foo:bar', Helpers::extractKeyTag('{foo:bar}')); $this->assertEquals('', Helpers::extractKeyTag('foo{}:bar')); $this->assertEquals('', Helpers::extractKeyTag('')); $this->assertEquals('', Helpers::extractKeyTag('{}')); }
/** * @group disconnected */ public function testFilterVariadicValues() { $arguments = array('key', 'value1', 'value2', 'value3'); $this->assertSame($arguments, Helpers::filterVariadicValues($arguments)); $this->assertSame($arguments, Helpers::filterVariadicValues(array('key', array('value1', 'value2', 'value3')))); $arguments = array(array(), array()); $this->assertSame($arguments, Helpers::filterArrayArguments($arguments)); $arguments = array(new \stdClass()); $this->assertSame($arguments, Helpers::filterArrayArguments($arguments)); }
/** * Handles an integer reply returned by Redis. * * @param IConnectionComposable $connection Connection to Redis. * @param string $number String representation of an integer. * @return int */ public function handle(IConnectionComposable $connection, $number) { if (is_numeric($number)) { return (int) $number; } if ($number !== 'nil') { Helpers::onCommunicationException(new ProtocolException($connection, "Cannot parse '{$number}' as numeric response")); } return null; }
/** * Handles a bulk reply returned by Redis. * * @param ComposableConnectionInterface $connection Connection to Redis. * @param string $lengthString Bytes size of the bulk reply. * @return string */ public function handle(ComposableConnectionInterface $connection, $lengthString) { $length = (int) $lengthString; if ("{$length}" !== $lengthString) { Helpers::onCommunicationException(new ProtocolException($connection, "Cannot parse '{$lengthString}' as bulk length")); } if ($length >= 0) { return substr($connection->readBytes($length + 2), 0, -2); } if ($length == -1) { return null; } }
/** * Returns a pipeline executor depending on the kind of the underlying * connection and the passed options. * * @param Client Client instance used by the context. * @param array Options for the context initialization. * @return IPipelineExecutor */ protected function createExecutor(Client $client, array $options) { if (!$options) { return new StandardExecutor(); } if (isset($options['executor'])) { $executor = $options['executor']; if (!$executor instanceof IPipelineExecutor) { throw new \InvalidArgumentException('The executor option accepts only instances ' . 'of Predis\\Pipeline\\IPipelineExecutor'); } return $executor; } if (isset($options['safe']) && $options['safe'] == true) { $isCluster = Helpers::isCluster($client->getConnection()); return $isCluster ? new SafeClusterExecutor() : new SafeExecutor(); } return new StandardExecutor(); }
/** * Executes the specified Redis command on all the nodes of a cluster. * * @param ICommand $command A Redis command. * @return array */ public function executeCommandOnShards(ICommand $command) { if (Helpers::isCluster($this->connection)) { $replies = array(); foreach ($this->connection as $connection) { $replies[] = $connection->executeCommand($command); } return $replies; } return array($this->connection->executeCommand($command)); }
/** * {@inheritdoc} */ public function getHash(INodeKeyGenerator $distributor) { if (isset($this->_hash)) { return $this->_hash; } if ($this->canBeHashed()) { $key = Helpers::getKeyHashablePart($this->_arguments[0]); $this->_hash = $distributor->generateKey($key); return $this->_hash; } return null; }
/** * Helper method used to handle a protocol error generated while reading a * reply from a connection to Redis. * * @param ComposableConnectionInterface $connection Connection to Redis that generated the error. * @param string $message Error message. */ private function protocolError(ComposableConnectionInterface $connection, $message) { Helpers::onCommunicationException(new ProtocolException($connection, $message)); }
/** * {@inheritdoc} */ protected function writeCommand($method, $arguments, $callback = null) { $arguments = Helpers::filterArrayArguments($arguments ?: array()); $command = $this->client->createCommand($method, $arguments); $this->client->executeCommand($command, $callback); }
/** * Writes a Redis command on the underlying connection. * * @param string $method ID of the command. * @param array $arguments List of arguments. */ private function writeCommand($method, $arguments) { $arguments = Helpers::filterArrayArguments($arguments); $command = $this->client->createCommand($method, $arguments); $this->client->getConnection()->writeCommand($command); }
/** * Helper method that handles protocol errors encountered inside a transaction. * * @param string $message Error message. */ private function onProtocolError($message) { // Since a MULTI/EXEC block cannot be initialized when using aggregated // connections, we can safely assume that Predis\Client::getConnection() // will always return an instance of Predis\Connection\SingleConnectionInterface. Helpers::onCommunicationException(new ProtocolException($this->client->getConnection(), $message)); }
/** * {@inheritdoc} */ protected function filterArguments(array $arguments) { return Helpers::filterVariadicValues($arguments); }
/** * Retrieves a connection instance from the cluster using a key. * * @param string $key Key of a Redis value. * @return IConnectionSingle */ public function getConnectionByKey($key) { $hashablePart = Helpers::getKeyHashablePart($key); $keyHash = $this->_distributor->generateKey($hashablePart); return $this->_distributor->get($keyHash); }
/** * Helper method to handle protocol errors. * * @param string $message Error message. */ protected function onProtocolError($message) { Helpers::onCommunicationException(new ProtocolException($this, $message)); }
/** * {@inheritdoc} */ protected function filterArguments(array $arguments) { return Helpers::filterArrayArguments($arguments); }
/** * {@inheritdoc} */ public function read(ComposableConnectionInterface $connection) { $chunk = $connection->readLine(); $prefix = $chunk[0]; $payload = substr($chunk, 1); switch ($prefix) { case '+': // inline switch ($payload) { case 'OK': return true; case 'QUEUED': return new ResponseQueued(); default: return $payload; } case '$': // bulk $size = (int) $payload; if ($size === -1) { return null; } return substr($connection->readBytes($size + 2), 0, -2); case '*': // multi bulk $count = (int) $payload; if ($count === -1) { return null; } if ($this->mbiterable == true) { return new MultiBulkResponseSimple($connection, $count); } $multibulk = array(); for ($i = 0; $i < $count; $i++) { $multibulk[$i] = $this->read($connection); } return $multibulk; case ':': // integer return (int) $payload; case '-': // error return new ResponseError($payload); default: Helpers::onCommunicationException(new ProtocolException($connection, "Unknown prefix: '{$prefix}'")); } }
/** * Helper method that handles protocol errors encountered inside a transaction. * * @param string $message Error message. */ private function onProtocolError($message) { // Since a MULTI/EXEC block cannot be initialized over a clustered // connection, we can safely assume that Predis\Client::getConnection() // will always return an instance of Predis\Network\IConnectionSingle. Helpers::onCommunicationException(new ProtocolException($this->client->getConnection(), $message)); }
/** * Checks if the passed client instance satisfies the required conditions * needed to initialize a monitor context. * * @param Client Client instance used by the context. */ private function checkCapabilities(Client $client) { if (Helpers::isCluster($client->getConnection())) { throw new ClientException('Cannot initialize a monitor context over a cluster of connections'); } if ($client->getProfile()->supportsCommand('monitor') === false) { throw new ClientException('The current profile does not support the MONITOR command'); } }