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,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\Option;
/**
* Implements a client option.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
abstract class AbstractOption implements OptionInterface
{
/**
* {@inheritdoc}
*/
public function filter(ClientOptionsInterface $options, $value)
{
return $value;
}
/**
* {@inheritdoc}
*/
public function getDefault(ClientOptionsInterface $options)
{
return null;
}
/**
* {@inheritdoc}
*/
public function __invoke(ClientOptionsInterface $options, $value)
{
if (isset($value)) {
return $this->filter($options, $value);
}
return $this->getDefault($options);
}
}

View file

@ -0,0 +1,96 @@
<?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\Option;
use Predis\Connection\ClusterConnectionInterface;
use Predis\Connection\PredisCluster;
use Predis\Connection\RedisCluster;
/**
* Option class that returns a connection cluster to be used by a client.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class ClientCluster extends AbstractOption
{
/**
* Checks if the specified value is a valid instance of ClusterConnectionInterface.
*
* @param ClusterConnectionInterface $cluster Instance of a connection cluster.
* @return ClusterConnectionInterface
*/
protected function checkInstance($cluster)
{
if (!$cluster instanceof ClusterConnectionInterface) {
throw new \InvalidArgumentException('Instance of Predis\Connection\ClusterConnectionInterface expected');
}
return $cluster;
}
/**
* {@inheritdoc}
*/
public function filter(ClientOptionsInterface $options, $value)
{
if (is_callable($value)) {
return $this->checkInstance(call_user_func($value, $options, $this));
}
$initializer = $this->getInitializer($options, $value);
return $this->checkInstance($initializer());
}
/**
* Returns an initializer for the specified FQN or type.
*
* @param string $fqnOrType Type of cluster or FQN of a class implementing ClusterConnectionInterface.
* @param ClientOptionsInterface $options Instance of the client options.
* @return \Closure
*/
protected function getInitializer(ClientOptionsInterface $options, $fqnOrType)
{
switch ($fqnOrType) {
case 'predis':
return function () {
return new PredisCluster();
};
case 'redis':
return function () use ($options) {
$connectionFactory = $options->connections;
$cluster = new RedisCluster($connectionFactory);
return $cluster;
};
default:
// TODO: we should not even allow non-string values here.
if (is_string($fqnOrType) && !class_exists($fqnOrType)) {
throw new \InvalidArgumentException("Class $fqnOrType does not exist");
}
return function () use ($fqnOrType) {
return new $fqnOrType();
};
}
}
/**
* {@inheritdoc}
*/
public function getDefault(ClientOptionsInterface $options)
{
return new PredisCluster();
}
}

View file

@ -0,0 +1,73 @@
<?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\Option;
use Predis\Connection\ConnectionFactory;
use Predis\Connection\ConnectionFactoryInterface;
/**
* Option class that returns a connection factory to be used by a client.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class ClientConnectionFactory extends AbstractOption
{
/**
* {@inheritdoc}
*/
public function filter(ClientOptionsInterface $options, $value)
{
if ($value instanceof ConnectionFactoryInterface) {
return $value;
}
if (is_array($value)) {
$factory = $this->getDefault($options);
foreach ($value as $scheme => $initializer) {
$factory->define($scheme, $initializer);
}
return $factory;
}
if (is_callable($value)) {
$factory = call_user_func($value, $options, $this);
if (!$factory instanceof ConnectionFactoryInterface) {
throw new \InvalidArgumentException('Instance of Predis\Connection\ConnectionFactoryInterface expected');
}
return $factory;
}
if (@class_exists($value)) {
$factory = new $value();
if (!$factory instanceof ConnectionFactoryInterface) {
throw new \InvalidArgumentException("Class $value must be an instance of Predis\Connection\ConnectionFactoryInterface");
}
return $factory;
}
throw new \InvalidArgumentException('Invalid value for the connections option');
}
/**
* {@inheritdoc}
*/
public function getDefault(ClientOptionsInterface $options)
{
return new ConnectionFactory($options->profile);
}
}

View file

@ -0,0 +1,36 @@
<?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\Option;
/**
* Option class used to specify if the client should throw server exceptions.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class ClientExceptions extends AbstractOption
{
/**
* {@inheritdoc}
*/
public function filter(ClientOptionsInterface $options, $value)
{
return (bool) $value;
}
/**
* {@inheritdoc}
*/
public function getDefault(ClientOptionsInterface $options)
{
return true;
}
}

View file

@ -0,0 +1,125 @@
<?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\Option;
/**
* Class that manages client options with filtering and conversion.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class ClientOptions implements ClientOptionsInterface
{
private $handlers;
private $defined;
private $options = array();
/**
* @param array $options Array of client options.
*/
public function __construct(Array $options = array())
{
$this->handlers = $this->initialize($options);
$this->defined = array_fill_keys(array_keys($options), true);
}
/**
* Ensures that the default options are initialized.
*
* @return array
*/
protected function getDefaultOptions()
{
return array(
'profile' => new ClientProfile(),
'connections' => new ClientConnectionFactory(),
'cluster' => new ClientCluster(),
'replication' => new ClientReplication(),
'prefix' => new ClientPrefix(),
'exceptions' => new ClientExceptions(),
);
}
/**
* Initializes client options handlers.
*
* @param array $options List of client options values.
* @return array
*/
protected function initialize(Array $options)
{
$handlers = $this->getDefaultOptions();
foreach ($options as $option => $value) {
if (isset($handlers[$option])) {
$handler = $handlers[$option];
$handlers[$option] = function ($options) use ($handler, $value) {
return $handler->filter($options, $value);
};
} else {
$this->options[$option] = $value;
}
}
return $handlers;
}
/**
* Checks if the specified option is set.
*
* @param string $option Name of the option.
* @return bool
*/
public function __isset($option)
{
return isset($this->defined[$option]);
}
/**
* Returns the value of the specified option.
*
* @param string $option Name of the option.
* @return mixed
*/
public function __get($option)
{
if (isset($this->options[$option])) {
return $this->options[$option];
}
if (isset($this->handlers[$option])) {
$handler = $this->handlers[$option];
$value = $handler instanceof OptionInterface ? $handler->getDefault($this) : $handler($this);
$this->options[$option] = $value;
return $value;
}
}
/**
* Returns the default value for the specified option.
*
* @param string|OptionInterface $option Name or instance of the option.
* @return mixed
*/
public function getDefault($option)
{
if ($option instanceof OptionInterface) {
return $option->getDefault($this);
}
$options = $this->getDefaultOptions();
if (isset($options[$option])) {
return $options[$option]->getDefault($this);
}
}
}

View file

@ -0,0 +1,21 @@
<?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\Option;
/**
* Marker interface defining a client options bag.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
interface ClientOptionsInterface
{
}

View 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\Option;
use Predis\Command\Processor\KeyPrefixProcessor;
/**
* Option class that handles the prefixing of keys in commands.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class ClientPrefix extends AbstractOption
{
/**
* {@inheritdoc}
*/
public function filter(ClientOptionsInterface $options, $value)
{
return new KeyPrefixProcessor($value);
}
}

View file

@ -0,0 +1,61 @@
<?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\Option;
use Predis\Profile\ServerProfile;
use Predis\Profile\ServerProfileInterface;
/**
* Option class that handles server profiles to be used by a client.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class ClientProfile extends AbstractOption
{
/**
* {@inheritdoc}
*/
public function filter(ClientOptionsInterface $options, $value)
{
if (is_string($value)) {
$value = ServerProfile::get($value);
if (isset($options->prefix)) {
$value->setProcessor($options->prefix);
}
}
if (is_callable($value)) {
$value = call_user_func($value, $options, $this);
}
if (!$value instanceof ServerProfileInterface) {
throw new \InvalidArgumentException('Invalid value for the profile option');
}
return $value;
}
/**
* {@inheritdoc}
*/
public function getDefault(ClientOptionsInterface $options)
{
$profile = ServerProfile::getDefault();
if (isset($options->prefix)) {
$profile->setProcessor($options->prefix);
}
return $profile;
}
}

View file

@ -0,0 +1,78 @@
<?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\Option;
use Predis\Connection\MasterSlaveReplication;
use Predis\Connection\ReplicationConnectionInterface;
/**
* Option class that returns a replication connection be used by a client.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class ClientReplication extends AbstractOption
{
/**
* Checks if the specified value is a valid instance of ReplicationConnectionInterface.
*
* @param ReplicationConnectionInterface $connection Instance of a replication connection.
* @return ReplicationConnectionInterface
*/
protected function checkInstance($connection)
{
if (!$connection instanceof ReplicationConnectionInterface) {
throw new \InvalidArgumentException('Instance of Predis\Connection\ReplicationConnectionInterface expected');
}
return $connection;
}
/**
* {@inheritdoc}
*/
public function filter(ClientOptionsInterface $options, $value)
{
if (is_callable($value)) {
$connection = call_user_func($value, $options, $this);
if (!$connection instanceof ReplicationConnectionInterface) {
throw new \InvalidArgumentException('Instance of Predis\Connection\ReplicationConnectionInterface expected');
}
return $connection;
}
if (is_string($value)) {
if (!class_exists($value)) {
throw new \InvalidArgumentException("Class $value does not exist");
}
if (!($connection = new $value()) instanceof ReplicationConnectionInterface) {
throw new \InvalidArgumentException('Instance of Predis\Connection\ReplicationConnectionInterface expected');
}
return $connection;
}
if ($value == true) {
return $this->getDefault($options);
}
}
/**
* {@inheritdoc}
*/
public function getDefault(ClientOptionsInterface $options)
{
return new MasterSlaveReplication();
}
}

View file

@ -0,0 +1,89 @@
<?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\Option;
/**
* Implements a generic class used to dynamically define a client option.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class CustomOption implements OptionInterface
{
private $filter;
private $default;
/**
* @param array $options List of options
*/
public function __construct(Array $options = array())
{
$this->filter = $this->ensureCallable($options, 'filter');
$this->default = $this->ensureCallable($options, 'default');
}
/**
* Checks if the specified value in the options array is a callable object.
*
* @param array $options Array of options
* @param string $key Target option.
*/
private function ensureCallable($options, $key)
{
if (!isset($options[$key])) {
return;
}
if (is_callable($callable = $options[$key])) {
return $callable;
}
throw new \InvalidArgumentException("The parameter $key must be callable");
}
/**
* {@inheritdoc}
*/
public function filter(ClientOptionsInterface $options, $value)
{
if (isset($value)) {
if ($this->filter === null) {
return $value;
}
return call_user_func($this->filter, $options, $value);
}
}
/**
* {@inheritdoc}
*/
public function getDefault(ClientOptionsInterface $options)
{
if (!isset($this->default)) {
return;
}
return call_user_func($this->default, $options);
}
/**
* {@inheritdoc}
*/
public function __invoke(ClientOptionsInterface $options, $value)
{
if (isset($value)) {
return $this->filter($options, $value);
}
return $this->getDefault($options);
}
}

View 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\Option;
/**
* Interface that defines a client option.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
interface OptionInterface
{
/**
* Filters (and optionally converts) the passed value.
*
* @param ClientOptionsInterface $options Options container.
* @param mixed $value Input value.
* @return mixed
*/
public function filter(ClientOptionsInterface $options, $value);
/**
* Returns a default value for the option.
*
* @param ClientOptionsInterface $options Options container.
* @return mixed
*/
public function getDefault(ClientOptionsInterface $options);
/**
* Filters a value and, if no value is specified, returns the default one
* defined by the option.
*
* @param ClientOptionsInterface $options Options container.
* @param mixed $value Input value.
* @return mixed
*/
public function __invoke(ClientOptionsInterface $options, $value);
}