|
@@ -19,6 +19,11 @@ use Predis\Profiles\ServerProfile;
|
|
|
use Predis\Pipeline\PipelineContext;
|
|
|
use Predis\Transaction\MultiExecContext;
|
|
|
|
|
|
+/**
|
|
|
+ * Main class that exposes the most high-level interface to interact with Redis.
|
|
|
+ *
|
|
|
+ * @author Daniele Alessandri <suppakilla@gmail.com>
|
|
|
+ */
|
|
|
class Client
|
|
|
{
|
|
|
const VERSION = '0.7.0-dev';
|
|
@@ -28,6 +33,12 @@ class Client
|
|
|
private $_connection;
|
|
|
private $_connectionFactory;
|
|
|
|
|
|
+ /**
|
|
|
+ * Initializes a new client with optional connection parameters and client options.
|
|
|
+ *
|
|
|
+ * @param mixed $parameters Connection parameters for one or multiple Redis servers.
|
|
|
+ * @param mixed $options Options that specify certain behaviours for the client.
|
|
|
+ */
|
|
|
public function __construct($parameters = null, $options = null)
|
|
|
{
|
|
|
$options = $this->filterOptions($options);
|
|
@@ -43,6 +54,14 @@ class Client
|
|
|
$this->_connection = $this->initializeConnection($parameters);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates an instance of Predis\Options\ClientOptions from various types of
|
|
|
+ * parameters (string, array, Predis\Profiles\ServerProfile) or returns the
|
|
|
+ * passed object if its an instance of Predis\Options\ClientOptions.
|
|
|
+ *
|
|
|
+ * @param mixed $options Client options.
|
|
|
+ * @return ClientOptions
|
|
|
+ */
|
|
|
private function filterOptions($options)
|
|
|
{
|
|
|
if ($options === null) {
|
|
@@ -64,6 +83,14 @@ class Client
|
|
|
throw new \InvalidArgumentException("Invalid type for client options");
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Initialize one or multiple connection (cluster) objects from various types of
|
|
|
+ * parameters (string, array) or returns the passed object if it implements the
|
|
|
+ * Predis\Network\IConnection interface.
|
|
|
+ *
|
|
|
+ * @param mixed $parameters Connection parameters or object.
|
|
|
+ * @return IConnection
|
|
|
+ */
|
|
|
private function initializeConnection($parameters)
|
|
|
{
|
|
|
if ($parameters === null) {
|
|
@@ -89,6 +116,12 @@ class Client
|
|
|
return $this->createConnection($parameters);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Create a new connection to a single Redis server using the provided parameters.
|
|
|
+ *
|
|
|
+ * @param mixed $parameters Connection parameters.
|
|
|
+ * @return IConnectionSingle
|
|
|
+ */
|
|
|
protected function createConnection($parameters)
|
|
|
{
|
|
|
$connection = $this->_connectionFactory->create($parameters);
|
|
@@ -107,21 +140,43 @@ class Client
|
|
|
return $connection;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns the server profile used by the client.
|
|
|
+ *
|
|
|
+ * @return IServerProfile
|
|
|
+ */
|
|
|
public function getProfile()
|
|
|
{
|
|
|
return $this->_profile;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns the client options specified upon client initialization.
|
|
|
+ *
|
|
|
+ * @return ClientOptions
|
|
|
+ */
|
|
|
public function getOptions()
|
|
|
{
|
|
|
return $this->_options;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns the connection factory object used by the client.
|
|
|
+ *
|
|
|
+ * @return IConnectionFactory
|
|
|
+ */
|
|
|
public function getConnectionFactory()
|
|
|
{
|
|
|
return $this->_connectionFactory;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns a new client instance for the specified connection when the client
|
|
|
+ * is connected to a cluster. The new client will use the same options of the
|
|
|
+ * the original instance.
|
|
|
+ *
|
|
|
+ * @return Client
|
|
|
+ */
|
|
|
public function getClientFor($connectionAlias)
|
|
|
{
|
|
|
if (($connection = $this->getConnection($connectionAlias)) === null) {
|
|
@@ -131,26 +186,48 @@ class Client
|
|
|
return new Client($connection, $this->_options);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Opens the connection to Redis.
|
|
|
+ */
|
|
|
public function connect()
|
|
|
{
|
|
|
$this->_connection->connect();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Disconnects from Redis.
|
|
|
+ */
|
|
|
public function disconnect()
|
|
|
{
|
|
|
$this->_connection->disconnect();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Disconnects from Redis. This method is an alias of disconnect().
|
|
|
+ */
|
|
|
public function quit()
|
|
|
{
|
|
|
$this->disconnect();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Checks if the underlying connection is connected to Redis.
|
|
|
+ *
|
|
|
+ * @return Boolean True means that the connection is open.
|
|
|
+ * False means that the connection is closed.
|
|
|
+ */
|
|
|
public function isConnected()
|
|
|
{
|
|
|
return $this->_connection->isConnected();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns the underlying connection instance or, when connected to a cluster,
|
|
|
+ * one of the connection instances identified by its alias.
|
|
|
+ *
|
|
|
+ * @param string $id The alias of a connection when connected to a cluster.
|
|
|
+ * @return IConnection
|
|
|
+ */
|
|
|
public function getConnection($id = null)
|
|
|
{
|
|
|
if (isset($id)) {
|
|
@@ -166,22 +243,48 @@ class Client
|
|
|
return $this->_connection;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Dinamically invokes a Redis command with the specified arguments.
|
|
|
+ *
|
|
|
+ * @param string $method The name of a Redis command.
|
|
|
+ * @param array $arguments The arguments for the command.
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
public function __call($method, $arguments)
|
|
|
{
|
|
|
$command = $this->_profile->createCommand($method, $arguments);
|
|
|
return $this->_connection->executeCommand($command);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates a new instance of the specified Redis command.
|
|
|
+ *
|
|
|
+ * @param string $method The name of a Redis command.
|
|
|
+ * @param array $arguments The arguments for the command.
|
|
|
+ * @return ICommand
|
|
|
+ */
|
|
|
public function createCommand($method, $arguments = array())
|
|
|
{
|
|
|
return $this->_profile->createCommand($method, $arguments);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Executes the specified Redis command.
|
|
|
+ *
|
|
|
+ * @param ICommand $command A Redis command.
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
public function executeCommand(ICommand $command)
|
|
|
{
|
|
|
return $this->_connection->executeCommand($command);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 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)) {
|
|
@@ -197,6 +300,15 @@ class Client
|
|
|
return array($this->_connection->executeCommand($command));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Call the specified initializer method on $this with 0, 1 or 2 arguments.
|
|
|
+ *
|
|
|
+ * TODO: Invert $argv and $initializer.
|
|
|
+ *
|
|
|
+ * @param array $argv Arguments for the initializer.
|
|
|
+ * @param string $initializer The initializer method.
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
private function sharedInitializer($argv, $initializer)
|
|
|
{
|
|
|
switch (count($argv)) {
|
|
@@ -216,53 +328,107 @@ class Client
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates a new pipeline context and returns it, or returns the results of
|
|
|
+ * a pipeline executed inside the optionally provided callable object.
|
|
|
+ *
|
|
|
+ * @param mixed $arg,... Options for the context, a callable object, or both.
|
|
|
+ * @return PipelineContext|array
|
|
|
+ */
|
|
|
public function pipeline(/* arguments */)
|
|
|
{
|
|
|
return $this->sharedInitializer(func_get_args(), 'initPipeline');
|
|
|
}
|
|
|
|
|
|
- protected function initPipeline(Array $options = null, $pipelineBlock = null)
|
|
|
+ /**
|
|
|
+ * Pipeline context initializer.
|
|
|
+ *
|
|
|
+ * @param array $options Options for the context.
|
|
|
+ * @param mixed $callable Optional callable object used to execute the context.
|
|
|
+ * @return PipelineContext|array
|
|
|
+ */
|
|
|
+ protected function initPipeline(Array $options = null, $callable = null)
|
|
|
{
|
|
|
$pipeline = new PipelineContext($this, $options);
|
|
|
- return $this->pipelineExecute($pipeline, $pipelineBlock);
|
|
|
+ return $this->pipelineExecute($pipeline, $callable);
|
|
|
}
|
|
|
|
|
|
- private function pipelineExecute(PipelineContext $pipeline, $block)
|
|
|
+ /**
|
|
|
+ * Executes a pipeline context when a callable object is passed.
|
|
|
+ *
|
|
|
+ * @param array $options Options of the context initialization.
|
|
|
+ * @param mixed $callable Optional callable object used to execute the context.
|
|
|
+ * @return PipelineContext|array
|
|
|
+ */
|
|
|
+ private function pipelineExecute(PipelineContext $pipeline, $callable)
|
|
|
{
|
|
|
- return $block !== null ? $pipeline->execute($block) : $pipeline;
|
|
|
+ return isset($callable) ? $pipeline->execute($callable) : $pipeline;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates a new transaction context and returns it, or returns the results of
|
|
|
+ * a transaction executed inside the optionally provided callable object.
|
|
|
+ *
|
|
|
+ * @param mixed $arg,... Options for the context, a callable object, or both.
|
|
|
+ * @return MultiExecContext|array
|
|
|
+ */
|
|
|
public function multiExec(/* arguments */)
|
|
|
{
|
|
|
return $this->sharedInitializer(func_get_args(), 'initMultiExec');
|
|
|
}
|
|
|
|
|
|
- protected function initMultiExec(Array $options = null, $block = null)
|
|
|
+ /**
|
|
|
+ * Transaction context initializer.
|
|
|
+ *
|
|
|
+ * @param array $options Options for the context.
|
|
|
+ * @param mixed $callable Optional callable object used to execute the context.
|
|
|
+ * @return MultiExecContext|array
|
|
|
+ */
|
|
|
+ protected function initMultiExec(Array $options = null, $callable = null)
|
|
|
{
|
|
|
$transaction = new MultiExecContext($this, $options ?: array());
|
|
|
- return isset($block) ? $transaction->execute($block) : $transaction;
|
|
|
+ return isset($callable) ? $transaction->execute($callable) : $transaction;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates a new Publish / Subscribe context and returns it, or executes it
|
|
|
+ * inside the optionally provided callable object.
|
|
|
+ *
|
|
|
+ * @param mixed $arg,... Options for the context, a callable object, or both.
|
|
|
+ * @return MultiExecContext|array
|
|
|
+ */
|
|
|
public function pubSub(/* arguments */)
|
|
|
{
|
|
|
return $this->sharedInitializer(func_get_args(), 'initPubSub');
|
|
|
}
|
|
|
|
|
|
- protected function initPubSub(Array $options = null, $block = null)
|
|
|
+ /**
|
|
|
+ * Publish / Subscribe context initializer.
|
|
|
+ *
|
|
|
+ * @param array $options Options for the context.
|
|
|
+ * @param mixed $callable Optional callable object used to execute the context.
|
|
|
+ * @return PubSubContext
|
|
|
+ */
|
|
|
+ protected function initPubSub(Array $options = null, $callable = null)
|
|
|
{
|
|
|
$pubsub = new PubSubContext($this, $options);
|
|
|
|
|
|
- if (!isset($block)) {
|
|
|
+ if (!isset($callable)) {
|
|
|
return $pubsub;
|
|
|
}
|
|
|
|
|
|
foreach ($pubsub as $message) {
|
|
|
- if ($block($pubsub, $message) === false) {
|
|
|
+ if ($callable($pubsub, $message) === false) {
|
|
|
$pubsub->closeContext();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns a new monitor context.
|
|
|
+ *
|
|
|
+ * @return MonitorContext
|
|
|
+ */
|
|
|
public function monitor()
|
|
|
{
|
|
|
return new MonitorContext($this);
|