Pipeline.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Pipeline;
  11. use Exception;
  12. use InvalidArgumentException;
  13. use SplQueue;
  14. use Predis\BasicClientInterface;
  15. use Predis\ClientException;
  16. use Predis\ClientInterface;
  17. use Predis\ExecutableContextInterface;
  18. use Predis\Command\CommandInterface;
  19. use Predis\Connection\ConnectionInterface;
  20. use Predis\Connection\ReplicationConnectionInterface;
  21. use Predis\Response;
  22. /**
  23. * Implementation of a command pipeline in which write and read operations of
  24. * Redis commands are pipelined to alleviate the effects of network round-trips.
  25. *
  26. * @author Daniele Alessandri <suppakilla@gmail.com>
  27. */
  28. class Pipeline implements BasicClientInterface, ExecutableContextInterface
  29. {
  30. private $client;
  31. private $pipeline;
  32. private $responses = array();
  33. private $running = false;
  34. /**
  35. * @param ClientInterface $client Client instance used by the context.
  36. */
  37. public function __construct(ClientInterface $client)
  38. {
  39. $this->client = $client;
  40. $this->pipeline = new SplQueue();
  41. }
  42. /**
  43. * Queues a command into the pipeline buffer.
  44. *
  45. * @param string $method Command ID.
  46. * @param array $arguments Arguments for the command.
  47. * @return Pipeline
  48. */
  49. public function __call($method, $arguments)
  50. {
  51. $command = $this->client->createCommand($method, $arguments);
  52. $this->recordCommand($command);
  53. return $this;
  54. }
  55. /**
  56. * Queues a command instance into the pipeline buffer.
  57. *
  58. * @param CommandInterface $command Command to queue in the buffer.
  59. */
  60. protected function recordCommand(CommandInterface $command)
  61. {
  62. $this->pipeline->enqueue($command);
  63. }
  64. /**
  65. * Queues a command instance into the pipeline buffer.
  66. *
  67. * @param CommandInterface $command Command to queue in the buffer.
  68. */
  69. public function executeCommand(CommandInterface $command)
  70. {
  71. $this->recordCommand($command);
  72. }
  73. /**
  74. * Throws and exception on -ERR responses returned by Redis.
  75. *
  76. * @param ConnectionInterface $connection The connection that returned the error.
  77. * @param Response\ErrorInterface $response The error response instance.
  78. */
  79. protected function exception(ConnectionInterface $connection, Response\ErrorInterface $response)
  80. {
  81. $connection->disconnect();
  82. $message = $response->getMessage();
  83. throw new Response\ServerException($message);
  84. }
  85. /**
  86. * Implements the logic to flush the queued commands and read the responses
  87. * from the current connection.
  88. *
  89. * @param ConnectionInterface $connection Current connection instance.
  90. * @param SplQueue $commands Queued commands.
  91. * @return array
  92. */
  93. protected function executePipeline(ConnectionInterface $connection, SplQueue $commands)
  94. {
  95. if ($connection instanceof ReplicationConnectionInterface) {
  96. $connection->switchTo('master');
  97. }
  98. foreach ($commands as $command) {
  99. $connection->writeCommand($command);
  100. }
  101. $responses = array();
  102. $exceptions = $this->throwServerExceptions();
  103. while (!$commands->isEmpty()) {
  104. $command = $commands->dequeue();
  105. $response = $connection->readResponse($command);
  106. if (!$response instanceof Response\ObjectInterface) {
  107. $responses[] = $command->parseResponse($response);
  108. } else if ($response instanceof Response\ErrorInterface && $exceptions) {
  109. $this->exception($connection, $response);
  110. } else {
  111. $responses[] = $response;
  112. }
  113. }
  114. return $responses;
  115. }
  116. /**
  117. * Flushes the buffer that holds the queued commands.
  118. *
  119. * @param bool $send Specifies if the commands in the buffer should be sent to Redis.
  120. * @return Pipeline
  121. */
  122. public function flushPipeline($send = true)
  123. {
  124. if ($send && !$this->pipeline->isEmpty()) {
  125. $connection = $this->client->getConnection();
  126. $responses = $this->executePipeline($connection, $this->pipeline);
  127. $this->responses = array_merge($this->responses, $responses);
  128. } else {
  129. $this->pipeline = new SplQueue();
  130. }
  131. return $this;
  132. }
  133. /**
  134. * Marks the running status of the pipeline.
  135. *
  136. * @param bool $bool Sets the running status of the pipeline.
  137. */
  138. private function setRunning($bool)
  139. {
  140. if ($bool && $this->running) {
  141. throw new ClientException('This pipeline is already opened');
  142. }
  143. $this->running = $bool;
  144. }
  145. /**
  146. * Handles the actual execution of the whole pipeline.
  147. *
  148. * @param mixed $callable Optional callback for execution.
  149. * @return array
  150. */
  151. public function execute($callable = null)
  152. {
  153. if ($callable && !is_callable($callable)) {
  154. throw new InvalidArgumentException('Argument passed must be a callable object');
  155. }
  156. $exception = null;
  157. $this->setRunning(true);
  158. try {
  159. if ($callable) {
  160. call_user_func($callable, $this);
  161. }
  162. $this->flushPipeline();
  163. } catch (Exception $exception) {
  164. // NOOP
  165. }
  166. $this->setRunning(false);
  167. if ($exception) {
  168. throw $exception;
  169. }
  170. return $this->responses;
  171. }
  172. /**
  173. * Returns if the pipeline should throw exceptions on server errors.
  174. *
  175. * @todo Awful naming...
  176. * @return bool
  177. */
  178. protected function throwServerExceptions()
  179. {
  180. return (bool) $this->client->getOptions()->exceptions;
  181. }
  182. /**
  183. * Returns the underlying client instance used by the pipeline object.
  184. *
  185. * @return ClientInterface
  186. */
  187. public function getClient()
  188. {
  189. return $this->client;
  190. }
  191. }