mirror of
https://github.com/predis/predis.git
synced 2026-08-25 08:19:35 +00:00
373d30b070
Status response objects are needed mostly to make it possible from the
client perspective to differentiate a status response with the payload
"OK" from a normale bulk reply containing "OK".
The biggest change is for commands returning +OK responses: these were
previously translated to TRUE (bool value), but they are now returned
as instances of Predis\Response\Status. Just to illustrate an example
of the possibilities with this change we will use SET since it is the
most widely used command returning +OK:
$response = $client->set('foo', 'bar');
echo $response; // 'OK'
$response == 'OK'; // TRUE
isset($response->ok); // TRUE
$response == true; // TRUE
$response === true; // FALSE
$response instanceof Predis\Response\ObjectInterface; // TRUE
$response instanceof Predis\Response\Status; // TRUE
For those checking responses returned by commands such as SET or PONG,
the breaking change basically lies in the usage of strict comparison:
doing $response === true will now evaluate to FALSE instead of TRUE.
By default Predis caches common status responses such as OK or QUEUED
to lower the memory usage when using pipelines or transactions.
182 lines
4.9 KiB
PHP
182 lines
4.9 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Predis package.
|
|
*
|
|
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Predis\Connection;
|
|
|
|
use Predis\NotSupportedException;
|
|
use Predis\Command\CommandInterface;
|
|
use Predis\Response;
|
|
|
|
/**
|
|
* This class provides the implementation of a Predis connection that uses PHP's
|
|
* streams for network communication and wraps the phpiredis C extension (PHP
|
|
* bindings for hiredis) to parse and serialize the Redis protocol. Everything
|
|
* is highly experimental (even the very same phpiredis since it is quite new),
|
|
* so use it at your own risk.
|
|
*
|
|
* This class is mainly intended to provide an optional low-overhead alternative
|
|
* for processing replies from Redis compared to the standard pure-PHP classes.
|
|
* Differences in speed when dealing with short inline replies are practically
|
|
* nonexistent, the actual speed boost is for long multibulk replies when this
|
|
* protocol processor can parse and return replies very fast.
|
|
*
|
|
* For instructions on how to build and install the phpiredis extension, please
|
|
* consult the repository of the project.
|
|
*
|
|
* The connection parameters supported by this class are:
|
|
*
|
|
* - scheme: it can be either 'tcp' or 'unix'.
|
|
* - host: hostname or IP address of the server.
|
|
* - port: TCP port of the server.
|
|
* - timeout: timeout to perform the connection.
|
|
* - read_write_timeout: timeout of read / write operations.
|
|
* - async_connect: performs the connection asynchronously.
|
|
* - tcp_nodelay: enables or disables Nagle's algorithm for coalescing.
|
|
* - persistent: the connection is left intact after a GC collection.
|
|
*
|
|
* @link https://github.com/nrk/phpiredis
|
|
* @author Daniele Alessandri <suppakilla@gmail.com>
|
|
*/
|
|
class PhpiredisStreamConnection extends StreamConnection
|
|
{
|
|
private $reader;
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function __construct(ConnectionParametersInterface $parameters)
|
|
{
|
|
$this->assertExtensions();
|
|
|
|
parent::__construct($parameters);
|
|
|
|
$this->reader = $this->createReader();
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function __destruct()
|
|
{
|
|
phpiredis_reader_destroy($this->reader);
|
|
|
|
parent::__destruct();
|
|
}
|
|
|
|
/**
|
|
* Checks if the phpiredis extension is loaded in PHP.
|
|
*/
|
|
private function assertExtensions()
|
|
{
|
|
if (!extension_loaded('phpiredis')) {
|
|
throw new NotSupportedException(
|
|
'The "phpiredis" extension is required by this connection backend'
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Creates a new instance of the protocol reader resource.
|
|
*
|
|
* @return resource
|
|
*/
|
|
private function createReader()
|
|
{
|
|
$reader = phpiredis_reader_create();
|
|
|
|
phpiredis_reader_set_status_handler($reader, $this->getStatusHandler());
|
|
phpiredis_reader_set_error_handler($reader, $this->getErrorHandler());
|
|
|
|
return $reader;
|
|
}
|
|
|
|
/**
|
|
* Returns the underlying protocol reader resource.
|
|
*
|
|
* @return resource
|
|
*/
|
|
protected function getReader()
|
|
{
|
|
return $this->reader;
|
|
}
|
|
|
|
/**
|
|
* Returns the handler used by the protocol reader to handle status replies.
|
|
*
|
|
* @return \Closure
|
|
*/
|
|
protected function getStatusHandler()
|
|
{
|
|
return function ($payload) {
|
|
return Response\Status::get($payload);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Returns the handler used by the protocol reader to handle Redis errors.
|
|
*
|
|
* @return \Closure
|
|
*/
|
|
protected function getErrorHandler()
|
|
{
|
|
return function ($errorMessage) {
|
|
return new Response\Error($errorMessage);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function read()
|
|
{
|
|
$socket = $this->getResource();
|
|
$reader = $this->reader;
|
|
|
|
while (PHPIREDIS_READER_STATE_INCOMPLETE === $state = phpiredis_reader_get_state($reader)) {
|
|
$buffer = fread($socket, 4096);
|
|
|
|
if ($buffer === false || $buffer === '') {
|
|
$this->onConnectionError('Error while reading bytes from the server');
|
|
|
|
return;
|
|
}
|
|
|
|
phpiredis_reader_feed($reader, $buffer);
|
|
}
|
|
|
|
if ($state === PHPIREDIS_READER_STATE_COMPLETE) {
|
|
return phpiredis_reader_get_reply($reader);
|
|
} else {
|
|
$this->onProtocolError(phpiredis_reader_get_error($reader));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function writeRequest(CommandInterface $command)
|
|
{
|
|
$arguments = $command->getArguments();
|
|
array_unshift($arguments, $command->getId());
|
|
|
|
$this->writeBytes(phpiredis_format_command($arguments));
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function __wakeup()
|
|
{
|
|
$this->assertExtensions();
|
|
$this->reader = $this->createReader();
|
|
}
|
|
}
|