PhpiredisStreamConnection.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * Checks if the phpiredis extension is loaded in PHP.
  58. */
  59. protected function checkExtensions()
  60. {
  61. if (!function_exists('phpiredis_reader_create')) {
  62. throw new NotSupportedException(
  63. 'The phpiredis extension must be loaded in order to be able to use this connection class'
  64. );
  65. }
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. protected function checkParameters(ConnectionParametersInterface $parameters)
  71. {
  72. if (isset($parameters->iterable_multibulk)) {
  73. $this->onInvalidOption('iterable_multibulk', $parameters);
  74. }
  75. return parent::checkParameters($parameters);
  76. }
  77. /**
  78. * Initializes the protocol reader resource.
  79. */
  80. protected function initializeReader()
  81. {
  82. $reader = phpiredis_reader_create();
  83. phpiredis_reader_set_status_handler($reader, $this->getStatusHandler());
  84. phpiredis_reader_set_error_handler($reader, $this->getErrorHandler());
  85. $this->reader = $reader;
  86. }
  87. /**
  88. * Gets the handler used by the protocol reader to handle status replies.
  89. *
  90. * @return \Closure
  91. */
  92. protected function getStatusHandler()
  93. {
  94. return function ($payload) {
  95. switch ($payload) {
  96. case 'OK':
  97. return true;
  98. case 'QUEUED':
  99. return new ResponseQueued();
  100. default:
  101. return $payload;
  102. }
  103. };
  104. }
  105. /**
  106. * Gets the handler used by the protocol reader to handle Redis errors.
  107. *
  108. * @param Boolean $throw_errors Specify if Redis errors throw exceptions.
  109. * @return \Closure
  110. */
  111. protected function getErrorHandler()
  112. {
  113. return function ($errorMessage) {
  114. return new ResponseError($errorMessage);
  115. };
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function read()
  121. {
  122. $socket = $this->getResource();
  123. $reader = $this->reader;
  124. while (PHPIREDIS_READER_STATE_INCOMPLETE === $state = phpiredis_reader_get_state($reader)) {
  125. $buffer = fread($socket, 4096);
  126. if ($buffer === false || $buffer === '') {
  127. $this->onConnectionError('Error while reading bytes from the server');
  128. return;
  129. }
  130. phpiredis_reader_feed($reader, $buffer);
  131. }
  132. if ($state === PHPIREDIS_READER_STATE_COMPLETE) {
  133. return phpiredis_reader_get_reply($reader);
  134. } else {
  135. $this->onProtocolError(phpiredis_reader_get_error($reader));
  136. }
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function writeCommand(CommandInterface $command)
  142. {
  143. $cmdargs = $command->getArguments();
  144. array_unshift($cmdargs, $command->getId());
  145. $this->writeBytes(phpiredis_format_command($cmdargs));
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function __sleep()
  151. {
  152. $this->checkExtensions();
  153. $this->initializeReader();
  154. return array_diff(parent::__sleep(), array('mbiterable'));
  155. }
  156. }