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:
antirez 2014-05-28 11:06:06 +02:00
commit c58f935fdc
277 changed files with 18503 additions and 0 deletions

View file

@ -0,0 +1,34 @@
<?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\Command\Processor;
/**
* Defines an object that can process commands using command processors.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
interface CommandProcessingInterface
{
/**
* Associates a command processor.
*
* @param CommandProcessorInterface $processor The command processor.
*/
public function setProcessor(CommandProcessorInterface $processor);
/**
* Returns the associated command processor.
*
* @return CommandProcessorInterface
*/
public function getProcessor();
}

View file

@ -0,0 +1,42 @@
<?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\Command\Processor;
/**
* A command processor chain processes a command using multiple chained command
* processor before it is sent to Redis.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
interface CommandProcessorChainInterface extends CommandProcessorInterface, \IteratorAggregate, \Countable
{
/**
* Adds a command processor.
*
* @param CommandProcessorInterface $processor A command processor.
*/
public function add(CommandProcessorInterface $processor);
/**
* Removes a command processor from the chain.
*
* @param CommandProcessorInterface $processor A command processor.
*/
public function remove(CommandProcessorInterface $processor);
/**
* Returns an ordered list of the command processors in the chain.
*
* @return array
*/
public function getProcessors();
}

View file

@ -0,0 +1,29 @@
<?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\Command\Processor;
use Predis\Command\CommandInterface;
/**
* A command processor processes commands before they are sent to Redis.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
interface CommandProcessorInterface
{
/**
* Processes a Redis command.
*
* @param CommandInterface $command Redis command.
*/
public function process(CommandInterface $command);
}

View 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\Command\Processor;
use Predis\Command\CommandInterface;
use Predis\Command\PrefixableCommandInterface;
/**
* Command processor that is used to prefix the keys contained in the arguments
* of a Redis command.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class KeyPrefixProcessor implements CommandProcessorInterface
{
private $prefix;
/**
* @param string $prefix Prefix for the keys.
*/
public function __construct($prefix)
{
$this->setPrefix($prefix);
}
/**
* Sets a prefix that is applied to all the keys.
*
* @param string $prefix Prefix for the keys.
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;
}
/**
* Gets the current prefix.
*
* @return string
*/
public function getPrefix()
{
return $this->prefix;
}
/**
* {@inheritdoc}
*/
public function process(CommandInterface $command)
{
if ($command instanceof PrefixableCommandInterface && $command->getArguments()) {
$command->prefixKeys($this->prefix);
}
}
/**
* {@inheritdoc}
*/
public function __toString()
{
return $this->getPrefix();
}
}

View file

@ -0,0 +1,130 @@
<?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\Command\Processor;
use Predis\Command\CommandInterface;
/**
* Default implementation of a command processors chain.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class ProcessorChain implements CommandProcessorChainInterface, \ArrayAccess
{
private $processors = array();
/**
* @param array $processors List of instances of CommandProcessorInterface.
*/
public function __construct($processors = array())
{
foreach ($processors as $processor) {
$this->add($processor);
}
}
/**
* {@inheritdoc}
*/
public function add(CommandProcessorInterface $processor)
{
$this->processors[] = $processor;
}
/**
* {@inheritdoc}
*/
public function remove(CommandProcessorInterface $processor)
{
if (false !== $index = array_search($processor, $this->processors, true)) {
unset($this[$index]);
}
}
/**
* {@inheritdoc}
*/
public function process(CommandInterface $command)
{
for ($i = 0; $i < $count = count($this->processors); $i++) {
$this->processors[$i]->process($command);
}
}
/**
* {@inheritdoc}
*/
public function getProcessors()
{
return $this->processors;
}
/**
* Returns an iterator over the list of command processor in the chain.
*
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->processors);
}
/**
* Returns the number of command processors in the chain.
*
* @return int
*/
public function count()
{
return count($this->processors);
}
/**
* {@inheritdoc}
*/
public function offsetExists($index)
{
return isset($this->processors[$index]);
}
/**
* {@inheritdoc}
*/
public function offsetGet($index)
{
return $this->processors[$index];
}
/**
* {@inheritdoc}
*/
public function offsetSet($index, $processor)
{
if (!$processor instanceof CommandProcessorInterface) {
throw new \InvalidArgumentException(
'A processor chain can hold only instances of classes implementing '.
'the Predis\Command\Processor\CommandProcessorInterface interface'
);
}
$this->processors[$index] = $processor;
}
/**
* {@inheritdoc}
*/
public function offsetUnset($index)
{
unset($this->processors[$index]);
$this->processors = array_values($this->processors);
}
}