PhpiredisStreamConnection.php 5.0 KB

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