PhpiredisStreamConnection.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. * - path: path of a UNIX domain socket when scheme is 'unix'.
  37. * - timeout: timeout to perform the connection.
  38. * - read_write_timeout: timeout of read / write operations.
  39. * - async_connect: performs the connection asynchronously.
  40. * - tcp_nodelay: enables or disables Nagle's algorithm for coalescing.
  41. * - persistent: the connection is left intact after a GC collection.
  42. *
  43. * @link https://github.com/nrk/phpiredis
  44. * @author Daniele Alessandri <suppakilla@gmail.com>
  45. */
  46. class PhpiredisStreamConnection extends StreamConnection
  47. {
  48. private $reader;
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function __construct(ConnectionParametersInterface $parameters)
  53. {
  54. $this->checkExtensions();
  55. $this->initializeReader();
  56. parent::__construct($parameters);
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function __destruct()
  62. {
  63. phpiredis_reader_destroy($this->reader);
  64. parent::__destruct();
  65. }
  66. /**
  67. * Checks if the phpiredis extension is loaded in PHP.
  68. */
  69. protected function checkExtensions()
  70. {
  71. if (!function_exists('phpiredis_reader_create')) {
  72. throw new NotSupportedException(
  73. 'The phpiredis extension must be loaded in order to be able to use this connection class'
  74. );
  75. }
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. protected function checkParameters(ConnectionParametersInterface $parameters)
  81. {
  82. if (isset($parameters->iterable_multibulk)) {
  83. $this->onInvalidOption('iterable_multibulk', $parameters);
  84. }
  85. return parent::checkParameters($parameters);
  86. }
  87. /**
  88. * Initializes the protocol reader resource.
  89. */
  90. protected function initializeReader()
  91. {
  92. $reader = phpiredis_reader_create();
  93. phpiredis_reader_set_status_handler($reader, $this->getStatusHandler());
  94. phpiredis_reader_set_error_handler($reader, $this->getErrorHandler());
  95. $this->reader = $reader;
  96. }
  97. /**
  98. * Gets the handler used by the protocol reader to handle status replies.
  99. *
  100. * @return \Closure
  101. */
  102. protected function getStatusHandler()
  103. {
  104. return function ($payload) {
  105. switch ($payload) {
  106. case 'OK':
  107. return true;
  108. case 'QUEUED':
  109. return new ResponseQueued();
  110. default:
  111. return $payload;
  112. }
  113. };
  114. }
  115. /**
  116. * Gets the handler used by the protocol reader to handle Redis errors.
  117. *
  118. * @param Boolean $throw_errors Specify if Redis errors throw exceptions.
  119. * @return \Closure
  120. */
  121. protected function getErrorHandler()
  122. {
  123. return function ($errorMessage) {
  124. return new ResponseError($errorMessage);
  125. };
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. public function read()
  131. {
  132. $socket = $this->getResource();
  133. $reader = $this->reader;
  134. while (PHPIREDIS_READER_STATE_INCOMPLETE === $state = phpiredis_reader_get_state($reader)) {
  135. $buffer = fread($socket, 4096);
  136. if ($buffer === false || $buffer === '') {
  137. $this->onConnectionError('Error while reading bytes from the server');
  138. }
  139. phpiredis_reader_feed($reader, $buffer);
  140. }
  141. if ($state === PHPIREDIS_READER_STATE_COMPLETE) {
  142. return phpiredis_reader_get_reply($reader);
  143. } else {
  144. $this->onProtocolError(phpiredis_reader_get_error($reader));
  145. }
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function writeCommand(CommandInterface $command)
  151. {
  152. $cmdargs = $command->getArguments();
  153. array_unshift($cmdargs, $command->getId());
  154. $this->writeBytes(phpiredis_format_command($cmdargs));
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function __sleep()
  160. {
  161. return array_diff(parent::__sleep(), array('mbiterable'));
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function __wakeup()
  167. {
  168. $this->checkExtensions();
  169. $this->initializeReader();
  170. }
  171. }