PhpiredisStreamConnection.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\Connection;
  11. use Predis\NotSupportedException;
  12. use Predis\ResponseError;
  13. use Predis\ResponseQueued;
  14. use Predis\Command\CommandInterface;
  15. /**
  16. * This class provides the implementation of a Predis connection that uses PHP's
  17. * streams for network communication and wraps the phpiredis C extension (PHP
  18. * bindings for hiredis) to parse and serialize the Redis protocol. Everything
  19. * is highly experimental (even the very same phpiredis since it is quite new),
  20. * so use it at your own risk.
  21. *
  22. * This class is mainly intended to provide an optional low-overhead alternative
  23. * for processing replies from Redis compared to the standard pure-PHP classes.
  24. * Differences in speed when dealing with short inline replies are practically
  25. * nonexistent, the actual speed boost is for long multibulk replies when this
  26. * protocol processor can parse and return replies very fast.
  27. *
  28. * For instructions on how to build and install the phpiredis extension, please
  29. * consult the repository of the project.
  30. *
  31. * The connection parameters supported by this class are:
  32. *
  33. * - scheme: it can be either 'tcp' or 'unix'.
  34. * - host: hostname or IP address of the server.
  35. * - port: TCP port of the server.
  36. * - timeout: timeout to perform the connection.
  37. * - read_write_timeout: timeout of read / write operations.
  38. * - async_connect: performs the connection asynchronously.
  39. * - persistent: the connection is left intact after a GC collection.
  40. *
  41. * @link https://github.com/nrk/phpiredis
  42. * @author Daniele Alessandri <suppakilla@gmail.com>
  43. */
  44. class PhpiredisStreamConnection extends StreamConnection
  45. {
  46. private $reader;
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function __construct(ConnectionParametersInterface $parameters)
  51. {
  52. $this->checkExtensions();
  53. $this->initializeReader();
  54. parent::__construct($parameters);
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function __destruct()
  60. {
  61. phpiredis_reader_destroy($this->reader);
  62. parent::__destruct();
  63. }
  64. /**
  65. * Checks if the phpiredis extension is loaded in PHP.
  66. */
  67. protected function checkExtensions()
  68. {
  69. if (!function_exists('phpiredis_reader_create')) {
  70. throw new NotSupportedException(
  71. 'The phpiredis extension must be loaded in order to be able to use this connection class'
  72. );
  73. }
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. protected function checkParameters(ConnectionParametersInterface $parameters)
  79. {
  80. if (isset($parameters->iterable_multibulk)) {
  81. $this->onInvalidOption('iterable_multibulk', $parameters);
  82. }
  83. return parent::checkParameters($parameters);
  84. }
  85. /**
  86. * Initializes the protocol reader resource.
  87. */
  88. protected function initializeReader()
  89. {
  90. $reader = phpiredis_reader_create();
  91. phpiredis_reader_set_status_handler($reader, $this->getStatusHandler());
  92. phpiredis_reader_set_error_handler($reader, $this->getErrorHandler());
  93. $this->reader = $reader;
  94. }
  95. /**
  96. * Gets the handler used by the protocol reader to handle status replies.
  97. *
  98. * @return \Closure
  99. */
  100. protected function getStatusHandler()
  101. {
  102. return function ($payload) {
  103. switch ($payload) {
  104. case 'OK':
  105. return true;
  106. case 'QUEUED':
  107. return new ResponseQueued();
  108. default:
  109. return $payload;
  110. }
  111. };
  112. }
  113. /**
  114. * Gets the handler used by the protocol reader to handle Redis errors.
  115. *
  116. * @param Boolean $throw_errors Specify if Redis errors throw exceptions.
  117. * @return \Closure
  118. */
  119. protected function getErrorHandler()
  120. {
  121. return function ($errorMessage) {
  122. return new ResponseError($errorMessage);
  123. };
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function read()
  129. {
  130. $socket = $this->getResource();
  131. $reader = $this->reader;
  132. while (PHPIREDIS_READER_STATE_INCOMPLETE === $state = phpiredis_reader_get_state($reader)) {
  133. $buffer = fread($socket, 4096);
  134. if ($buffer === false || $buffer === '') {
  135. $this->onConnectionError('Error while reading bytes from the server');
  136. return;
  137. }
  138. phpiredis_reader_feed($reader, $buffer);
  139. }
  140. if ($state === PHPIREDIS_READER_STATE_COMPLETE) {
  141. return phpiredis_reader_get_reply($reader);
  142. } else {
  143. $this->onProtocolError(phpiredis_reader_get_error($reader));
  144. }
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function writeCommand(CommandInterface $command)
  150. {
  151. $cmdargs = $command->getArguments();
  152. array_unshift($cmdargs, $command->getId());
  153. $this->writeBytes(phpiredis_format_command($cmdargs));
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public function __sleep()
  159. {
  160. return array_diff(parent::__sleep(), array('mbiterable'));
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function __wakeup()
  166. {
  167. $this->checkExtensions();
  168. $this->initializeReader();
  169. }
  170. }