Retwis ported to modern Redis data types.
The example (and related documentation at redis.io) was not updated for 5 years. Redis had no sorted sets and hashes when the original code was written (!). An update was really needed. We also use a modern PHP client now: Predis. A copy is shipped within this repository to make life easier to newcomers trying Redis for the first time via this example.
This commit is contained in:
commit
c58f935fdc
277 changed files with 18503 additions and 0 deletions
30
Predis/Protocol/CommandSerializerInterface.php
Normal file
30
Predis/Protocol/CommandSerializerInterface.php
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
<?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\Protocol;
|
||||
|
||||
use Predis\Command\CommandInterface;
|
||||
|
||||
/**
|
||||
* Interface that defines a custom serializer for Redis commands.
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
interface CommandSerializerInterface
|
||||
{
|
||||
/**
|
||||
* Serializes a Redis command.
|
||||
*
|
||||
* @param CommandInterface $command Redis command.
|
||||
* @return string
|
||||
*/
|
||||
public function serialize(CommandInterface $command);
|
||||
}
|
||||
50
Predis/Protocol/ComposableProtocolInterface.php
Normal file
50
Predis/Protocol/ComposableProtocolInterface.php
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
<?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\Protocol;
|
||||
|
||||
/**
|
||||
* Interface that defines a customizable protocol processor that serializes
|
||||
* Redis commands and parses replies returned by the server to PHP objects
|
||||
* using a pluggable set of classes defining the underlying wire protocol.
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
interface ComposableProtocolInterface extends ProtocolInterface
|
||||
{
|
||||
/**
|
||||
* Sets the command serializer to be used by the protocol processor.
|
||||
*
|
||||
* @param CommandSerializerInterface $serializer Command serializer.
|
||||
*/
|
||||
public function setSerializer(CommandSerializerInterface $serializer);
|
||||
|
||||
/**
|
||||
* Returns the command serializer used by the protocol processor.
|
||||
*
|
||||
* @return CommandSerializerInterface
|
||||
*/
|
||||
public function getSerializer();
|
||||
|
||||
/**
|
||||
* Sets the response reader to be used by the protocol processor.
|
||||
*
|
||||
* @param ResponseReaderInterface $reader Response reader.
|
||||
*/
|
||||
public function setReader(ResponseReaderInterface $reader);
|
||||
|
||||
/**
|
||||
* Returns the response reader used by the protocol processor.
|
||||
*
|
||||
* @return ResponseReaderInterface
|
||||
*/
|
||||
public function getReader();
|
||||
}
|
||||
24
Predis/Protocol/ProtocolException.php
Normal file
24
Predis/Protocol/ProtocolException.php
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<?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\Protocol;
|
||||
|
||||
use Predis\CommunicationException;
|
||||
|
||||
/**
|
||||
* Exception class that identifies errors encountered while
|
||||
* handling the Redis wire protocol.
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class ProtocolException extends CommunicationException
|
||||
{
|
||||
}
|
||||
40
Predis/Protocol/ProtocolInterface.php
Normal file
40
Predis/Protocol/ProtocolInterface.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?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\Protocol;
|
||||
|
||||
use Predis\Command\CommandInterface;
|
||||
use Predis\Connection\ComposableConnectionInterface;
|
||||
|
||||
/**
|
||||
* Interface that defines a protocol processor that serializes Redis commands
|
||||
* and parses replies returned by the server to PHP objects.
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
interface ProtocolInterface extends ResponseReaderInterface
|
||||
{
|
||||
/**
|
||||
* Writes a Redis command on the specified connection.
|
||||
*
|
||||
* @param ComposableConnectionInterface $connection Connection to Redis.
|
||||
* @param CommandInterface $command Redis command.
|
||||
*/
|
||||
public function write(ComposableConnectionInterface $connection, CommandInterface $command);
|
||||
|
||||
/**
|
||||
* Sets the options for the protocol processor.
|
||||
*
|
||||
* @param string $option Name of the option.
|
||||
* @param mixed $value Value of the option.
|
||||
*/
|
||||
public function setOption($option, $value);
|
||||
}
|
||||
32
Predis/Protocol/ResponseHandlerInterface.php
Normal file
32
Predis/Protocol/ResponseHandlerInterface.php
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
<?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\Protocol;
|
||||
|
||||
use Predis\Connection\ComposableConnectionInterface;
|
||||
|
||||
/**
|
||||
* Interface that defines an handler able to parse a reply.
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
interface ResponseHandlerInterface
|
||||
{
|
||||
/**
|
||||
* Parses a type of reply returned by Redis and reads more data from the
|
||||
* connection if needed.
|
||||
*
|
||||
* @param ComposableConnectionInterface $connection Connection to Redis.
|
||||
* @param string $payload Initial payload of the reply.
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle(ComposableConnectionInterface $connection, $payload);
|
||||
}
|
||||
31
Predis/Protocol/ResponseReaderInterface.php
Normal file
31
Predis/Protocol/ResponseReaderInterface.php
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
<?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\Protocol;
|
||||
|
||||
use Predis\Connection\ComposableConnectionInterface;
|
||||
|
||||
/**
|
||||
* Interface that defines a response reader able to parse replies returned by
|
||||
* Redis and deserialize them to PHP objects.
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
interface ResponseReaderInterface
|
||||
{
|
||||
/**
|
||||
* Reads replies from a connection to Redis and deserializes them.
|
||||
*
|
||||
* @param ComposableConnectionInterface $connection Connection to Redis.
|
||||
* @return mixed
|
||||
*/
|
||||
public function read(ComposableConnectionInterface $connection);
|
||||
}
|
||||
129
Predis/Protocol/Text/ComposableTextProtocol.php
Normal file
129
Predis/Protocol/Text/ComposableTextProtocol.php
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
<?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\Protocol\Text;
|
||||
|
||||
use Predis\Command\CommandInterface;
|
||||
use Predis\Connection\ComposableConnectionInterface;
|
||||
use Predis\Protocol\ResponseReaderInterface;
|
||||
use Predis\Protocol\CommandSerializerInterface;
|
||||
use Predis\Protocol\ComposableProtocolInterface;
|
||||
|
||||
/**
|
||||
* Implements a customizable protocol processor that uses the standard Redis
|
||||
* wire protocol to serialize Redis commands and parse replies returned by
|
||||
* the server using a pluggable set of classes.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class ComposableTextProtocol implements ComposableProtocolInterface
|
||||
{
|
||||
private $serializer;
|
||||
private $reader;
|
||||
|
||||
/**
|
||||
* @param array $options Set of options used to initialize the protocol processor.
|
||||
*/
|
||||
public function __construct(Array $options = array())
|
||||
{
|
||||
$this->setSerializer(new TextCommandSerializer());
|
||||
$this->setReader(new TextResponseReader());
|
||||
|
||||
if (count($options) > 0) {
|
||||
$this->initializeOptions($options);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the protocol processor using a set of options.
|
||||
*
|
||||
* @param array $options Set of options.
|
||||
*/
|
||||
private function initializeOptions(Array $options)
|
||||
{
|
||||
foreach ($options as $k => $v) {
|
||||
$this->setOption($k, $v);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setOption($option, $value)
|
||||
{
|
||||
switch ($option) {
|
||||
case 'iterable_multibulk':
|
||||
$handler = $value ? new ResponseMultiBulkStreamHandler() : new ResponseMultiBulkHandler();
|
||||
$this->reader->setHandler(TextProtocol::PREFIX_MULTI_BULK, $handler);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \InvalidArgumentException("The option $option is not supported by the current protocol");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function serialize(CommandInterface $command)
|
||||
{
|
||||
return $this->serializer->serialize($command);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function write(ComposableConnectionInterface $connection, CommandInterface $command)
|
||||
{
|
||||
$connection->writeBytes($this->serializer->serialize($command));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function read(ComposableConnectionInterface $connection)
|
||||
{
|
||||
return $this->reader->read($connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setSerializer(CommandSerializerInterface $serializer)
|
||||
{
|
||||
$this->serializer = $serializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSerializer()
|
||||
{
|
||||
return $this->serializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setReader(ResponseReaderInterface $reader)
|
||||
{
|
||||
$this->reader = $reader;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getReader()
|
||||
{
|
||||
return $this->reader;
|
||||
}
|
||||
}
|
||||
53
Predis/Protocol/Text/ResponseBulkHandler.php
Normal file
53
Predis/Protocol/Text/ResponseBulkHandler.php
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<?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\Protocol\Text;
|
||||
|
||||
use Predis\CommunicationException;
|
||||
use Predis\Connection\ComposableConnectionInterface;
|
||||
use Predis\Protocol\ProtocolException;
|
||||
use Predis\Protocol\ResponseHandlerInterface;
|
||||
|
||||
/**
|
||||
* Implements a response handler for bulk replies using the standard wire
|
||||
* protocol defined by Redis.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class ResponseBulkHandler implements ResponseHandlerInterface
|
||||
{
|
||||
/**
|
||||
* 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) {
|
||||
CommunicationException::handle(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
37
Predis/Protocol/Text/ResponseErrorHandler.php
Normal file
37
Predis/Protocol/Text/ResponseErrorHandler.php
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
<?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\Protocol\Text;
|
||||
|
||||
use Predis\ResponseError;
|
||||
use Predis\Connection\ComposableConnectionInterface;
|
||||
use Predis\Protocol\ResponseHandlerInterface;
|
||||
|
||||
/**
|
||||
* Implements a response handler for error replies using the standard wire
|
||||
* protocol defined by Redis.
|
||||
*
|
||||
* This handler returns a reply object to notify the user that an error has
|
||||
* occurred on the server.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class ResponseErrorHandler implements ResponseHandlerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(ComposableConnectionInterface $connection, $errorMessage)
|
||||
{
|
||||
return new ResponseError($errorMessage);
|
||||
}
|
||||
}
|
||||
49
Predis/Protocol/Text/ResponseIntegerHandler.php
Normal file
49
Predis/Protocol/Text/ResponseIntegerHandler.php
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
<?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\Protocol\Text;
|
||||
|
||||
use Predis\CommunicationException;
|
||||
use Predis\Connection\ComposableConnectionInterface;
|
||||
use Predis\Protocol\ProtocolException;
|
||||
use Predis\Protocol\ResponseHandlerInterface;
|
||||
|
||||
/**
|
||||
* Implements a response handler for integer replies using the standard wire
|
||||
* protocol defined by Redis.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class ResponseIntegerHandler implements ResponseHandlerInterface
|
||||
{
|
||||
/**
|
||||
* Handles an integer reply returned by Redis.
|
||||
*
|
||||
* @param ComposableConnectionInterface $connection Connection to Redis.
|
||||
* @param string $number String representation of an integer.
|
||||
* @return int
|
||||
*/
|
||||
public function handle(ComposableConnectionInterface $connection, $number)
|
||||
{
|
||||
if (is_numeric($number)) {
|
||||
return (int) $number;
|
||||
}
|
||||
|
||||
if ($number !== 'nil') {
|
||||
CommunicationException::handle(new ProtocolException(
|
||||
$connection, "Cannot parse '$number' as numeric response"
|
||||
));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
72
Predis/Protocol/Text/ResponseMultiBulkHandler.php
Normal file
72
Predis/Protocol/Text/ResponseMultiBulkHandler.php
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
<?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\Protocol\Text;
|
||||
|
||||
use Predis\CommunicationException;
|
||||
use Predis\Connection\ComposableConnectionInterface;
|
||||
use Predis\Protocol\ProtocolException;
|
||||
use Predis\Protocol\ResponseHandlerInterface;
|
||||
|
||||
/**
|
||||
* Implements a response handler for multi-bulk replies using the standard
|
||||
* wire protocol defined by Redis.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class ResponseMultiBulkHandler implements ResponseHandlerInterface
|
||||
{
|
||||
/**
|
||||
* Handles a multi-bulk reply returned by Redis.
|
||||
*
|
||||
* @param ComposableConnectionInterface $connection Connection to Redis.
|
||||
* @param string $lengthString Number of items in the multi-bulk reply.
|
||||
* @return array
|
||||
*/
|
||||
public function handle(ComposableConnectionInterface $connection, $lengthString)
|
||||
{
|
||||
$length = (int) $lengthString;
|
||||
|
||||
if ("$length" !== $lengthString) {
|
||||
CommunicationException::handle(new ProtocolException(
|
||||
$connection, "Cannot parse '$lengthString' as multi-bulk 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;
|
||||
}
|
||||
}
|
||||
48
Predis/Protocol/Text/ResponseMultiBulkStreamHandler.php
Normal file
48
Predis/Protocol/Text/ResponseMultiBulkStreamHandler.php
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
<?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\Protocol\Text;
|
||||
|
||||
use Predis\CommunicationException;
|
||||
use Predis\Connection\ComposableConnectionInterface;
|
||||
use Predis\Iterator\MultiBulkResponseSimple;
|
||||
use Predis\Protocol\ProtocolException;
|
||||
use Predis\Protocol\ResponseHandlerInterface;
|
||||
|
||||
/**
|
||||
* Implements a response handler for iterable multi-bulk replies using the
|
||||
* standard wire protocol defined by Redis.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class ResponseMultiBulkStreamHandler implements ResponseHandlerInterface
|
||||
{
|
||||
/**
|
||||
* 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) {
|
||||
CommunicationException::handle(new ProtocolException(
|
||||
$connection, "Cannot parse '$lengthString' as multi-bulk length"
|
||||
));
|
||||
}
|
||||
|
||||
return new MultiBulkResponseSimple($connection, $length);
|
||||
}
|
||||
}
|
||||
43
Predis/Protocol/Text/ResponseStatusHandler.php
Normal file
43
Predis/Protocol/Text/ResponseStatusHandler.php
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
<?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\Protocol\Text;
|
||||
|
||||
use Predis\ResponseQueued;
|
||||
use Predis\Connection\ComposableConnectionInterface;
|
||||
use Predis\Protocol\ResponseHandlerInterface;
|
||||
|
||||
/**
|
||||
* Implements a response handler for status replies using the standard wire
|
||||
* protocol defined by Redis.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class ResponseStatusHandler implements ResponseHandlerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(ComposableConnectionInterface $connection, $status)
|
||||
{
|
||||
switch ($status) {
|
||||
case 'OK':
|
||||
return true;
|
||||
|
||||
case 'QUEUED':
|
||||
return new ResponseQueued();
|
||||
|
||||
default:
|
||||
return $status;
|
||||
}
|
||||
}
|
||||
}
|
||||
47
Predis/Protocol/Text/TextCommandSerializer.php
Normal file
47
Predis/Protocol/Text/TextCommandSerializer.php
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<?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\Protocol\Text;
|
||||
|
||||
use Predis\Command\CommandInterface;
|
||||
use Predis\Protocol\CommandSerializerInterface;
|
||||
|
||||
/**
|
||||
* Implements a pluggable command serializer using the standard wire protocol
|
||||
* defined by Redis.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class TextCommandSerializer implements CommandSerializerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function serialize(CommandInterface $command)
|
||||
{
|
||||
$commandId = $command->getId();
|
||||
$arguments = $command->getArguments();
|
||||
|
||||
$cmdlen = strlen($commandId);
|
||||
$reqlen = count($arguments) + 1;
|
||||
|
||||
$buffer = "*{$reqlen}\r\n\${$cmdlen}\r\n{$commandId}\r\n";
|
||||
|
||||
for ($i = 0, $reqlen--; $i < $reqlen; $i++) {
|
||||
$argument = $arguments[$i];
|
||||
$arglen = strlen($argument);
|
||||
$buffer .= "\${$arglen}\r\n{$argument}\r\n";
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
}
|
||||
137
Predis/Protocol/Text/TextProtocol.php
Normal file
137
Predis/Protocol/Text/TextProtocol.php
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
<?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\Protocol\Text;
|
||||
|
||||
use Predis\CommunicationException;
|
||||
use Predis\ResponseError;
|
||||
use Predis\ResponseQueued;
|
||||
use Predis\Command\CommandInterface;
|
||||
use Predis\Connection\ComposableConnectionInterface;
|
||||
use Predis\Iterator\MultiBulkResponseSimple;
|
||||
use Predis\Protocol\ProtocolException;
|
||||
use Predis\Protocol\ProtocolInterface;
|
||||
|
||||
/**
|
||||
* Implements a protocol processor for the standard wire protocol defined by Redis.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class TextProtocol implements ProtocolInterface
|
||||
{
|
||||
const NEWLINE = "\r\n";
|
||||
const OK = 'OK';
|
||||
const ERROR = 'ERR';
|
||||
const QUEUED = 'QUEUED';
|
||||
const NULL = 'nil';
|
||||
|
||||
const PREFIX_STATUS = '+';
|
||||
const PREFIX_ERROR = '-';
|
||||
const PREFIX_INTEGER = ':';
|
||||
const PREFIX_BULK = '$';
|
||||
const PREFIX_MULTI_BULK = '*';
|
||||
|
||||
const BUFFER_SIZE = 4096;
|
||||
|
||||
private $mbiterable;
|
||||
private $serializer;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->mbiterable = false;
|
||||
$this->serializer = new TextCommandSerializer();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function write(ComposableConnectionInterface $connection, CommandInterface $command)
|
||||
{
|
||||
$connection->writeBytes($this->serializer->serialize($command));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function read(ComposableConnectionInterface $connection)
|
||||
{
|
||||
$chunk = $connection->readLine();
|
||||
$prefix = $chunk[0];
|
||||
$payload = substr($chunk, 1);
|
||||
|
||||
switch ($prefix) {
|
||||
case '+':
|
||||
switch ($payload) {
|
||||
case 'OK':
|
||||
return true;
|
||||
|
||||
case 'QUEUED':
|
||||
return new ResponseQueued();
|
||||
|
||||
default:
|
||||
return $payload;
|
||||
}
|
||||
|
||||
case '$':
|
||||
$size = (int) $payload;
|
||||
if ($size === -1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return substr($connection->readBytes($size + 2), 0, -2);
|
||||
|
||||
case '*':
|
||||
$count = (int) $payload;
|
||||
|
||||
if ($count === -1) {
|
||||
return null;
|
||||
}
|
||||
if ($this->mbiterable) {
|
||||
return new MultiBulkResponseSimple($connection, $count);
|
||||
}
|
||||
|
||||
$multibulk = array();
|
||||
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$multibulk[$i] = $this->read($connection);
|
||||
}
|
||||
|
||||
return $multibulk;
|
||||
|
||||
case ':':
|
||||
return (int) $payload;
|
||||
|
||||
case '-':
|
||||
return new ResponseError($payload);
|
||||
|
||||
default:
|
||||
CommunicationException::handle(new ProtocolException(
|
||||
$connection, "Unknown prefix: '$prefix'"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setOption($option, $value)
|
||||
{
|
||||
switch ($option) {
|
||||
case 'iterable_multibulk':
|
||||
$this->mbiterable = (bool) $value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
113
Predis/Protocol/Text/TextResponseReader.php
Normal file
113
Predis/Protocol/Text/TextResponseReader.php
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
<?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\Protocol\Text;
|
||||
|
||||
use Predis\CommunicationException;
|
||||
use Predis\Connection\ComposableConnectionInterface;
|
||||
use Predis\Protocol\ProtocolException;
|
||||
use Predis\Protocol\ResponseHandlerInterface;
|
||||
use Predis\Protocol\ResponseReaderInterface;
|
||||
|
||||
/**
|
||||
* Implements a pluggable response reader using the standard wire protocol
|
||||
* defined by Redis.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class TextResponseReader implements ResponseReaderInterface
|
||||
{
|
||||
private $handlers;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->handlers = $this->getDefaultHandlers();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default set of response handlers for all the type of replies
|
||||
* that can be returned by Redis.
|
||||
*/
|
||||
private function getDefaultHandlers()
|
||||
{
|
||||
return array(
|
||||
TextProtocol::PREFIX_STATUS => new ResponseStatusHandler(),
|
||||
TextProtocol::PREFIX_ERROR => new ResponseErrorHandler(),
|
||||
TextProtocol::PREFIX_INTEGER => new ResponseIntegerHandler(),
|
||||
TextProtocol::PREFIX_BULK => new ResponseBulkHandler(),
|
||||
TextProtocol::PREFIX_MULTI_BULK => new ResponseMultiBulkHandler(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a response handler for a certain prefix that identifies a type of
|
||||
* reply that can be returned by Redis.
|
||||
*
|
||||
* @param string $prefix Identifier for a type of reply.
|
||||
* @param ResponseHandlerInterface $handler Response handler for the reply.
|
||||
*/
|
||||
public function setHandler($prefix, ResponseHandlerInterface $handler)
|
||||
{
|
||||
$this->handlers[$prefix] = $handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the response handler associated to a certain type of reply that
|
||||
* can be returned by Redis.
|
||||
*
|
||||
* @param string $prefix Identifier for a type of reply.
|
||||
* @return ResponseHandlerInterface
|
||||
*/
|
||||
public function getHandler($prefix)
|
||||
{
|
||||
if (isset($this->handlers[$prefix])) {
|
||||
return $this->handlers[$prefix];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function read(ComposableConnectionInterface $connection)
|
||||
{
|
||||
$header = $connection->readLine();
|
||||
|
||||
if ($header === '') {
|
||||
$this->protocolError($connection, 'Unexpected empty header');
|
||||
}
|
||||
|
||||
$prefix = $header[0];
|
||||
|
||||
if (!isset($this->handlers[$prefix])) {
|
||||
$this->protocolError($connection, "Unknown prefix: '$prefix'");
|
||||
}
|
||||
|
||||
$handler = $this->handlers[$prefix];
|
||||
|
||||
return $handler->handle($connection, substr($header, 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
CommunicationException::handle(new ProtocolException($connection, $message));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue