PhpiredisStreamConnection.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\Command\CommandInterface;
  13. use Predis\Response;
  14. /**
  15. * This class provides the implementation of a Predis connection that uses PHP's
  16. * streams for network communication and wraps the phpiredis C extension (PHP
  17. * bindings for hiredis) to parse and serialize the Redis protocol.
  18. *
  19. * This class is intended to provide an optional low-overhead alternative for
  20. * processing responses from Redis compared to the standard pure-PHP classes.
  21. * Differences in speed when dealing with short inline responses are practically
  22. * nonexistent, the actual speed boost is for big multibulk responses when this
  23. * protocol processor can parse and return responses very fast.
  24. *
  25. * For instructions on how to build and install the phpiredis extension, please
  26. * consult the repository of the project.
  27. *
  28. * The connection parameters supported by this class are:
  29. *
  30. * - scheme: it can be either 'tcp' or 'unix'.
  31. * - host: hostname or IP address of the server.
  32. * - port: TCP port of the server.
  33. * - timeout: timeout to perform the connection.
  34. * - read_write_timeout: timeout of read / write operations.
  35. * - async_connect: performs the connection asynchronously.
  36. * - tcp_nodelay: enables or disables Nagle's algorithm for coalescing.
  37. * - persistent: the connection is left intact after a GC collection.
  38. *
  39. * @link https://github.com/nrk/phpiredis
  40. * @author Daniele Alessandri <suppakilla@gmail.com>
  41. */
  42. class PhpiredisStreamConnection extends StreamConnection
  43. {
  44. private $reader;
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function __construct(ConnectionParametersInterface $parameters)
  49. {
  50. $this->assertExtensions();
  51. parent::__construct($parameters);
  52. $this->reader = $this->createReader();
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function __destruct()
  58. {
  59. phpiredis_reader_destroy($this->reader);
  60. parent::__destruct();
  61. }
  62. /**
  63. * Checks if the phpiredis extension is loaded in PHP.
  64. */
  65. private function assertExtensions()
  66. {
  67. if (!extension_loaded('phpiredis')) {
  68. throw new NotSupportedException(
  69. 'The "phpiredis" extension is required by this connection backend'
  70. );
  71. }
  72. }
  73. /**
  74. * Creates a new instance of the protocol reader resource.
  75. *
  76. * @return resource
  77. */
  78. private function createReader()
  79. {
  80. $reader = phpiredis_reader_create();
  81. phpiredis_reader_set_status_handler($reader, $this->getStatusHandler());
  82. phpiredis_reader_set_error_handler($reader, $this->getErrorHandler());
  83. return $reader;
  84. }
  85. /**
  86. * Returns the underlying protocol reader resource.
  87. *
  88. * @return resource
  89. */
  90. protected function getReader()
  91. {
  92. return $this->reader;
  93. }
  94. /**
  95. * Returns the handler used by the protocol reader for inline responses.
  96. *
  97. * @return \Closure
  98. */
  99. protected function getStatusHandler()
  100. {
  101. return function ($payload) {
  102. return Response\Status::get($payload);
  103. };
  104. }
  105. /**
  106. * Returns the handler used by the protocol reader for error responses.
  107. *
  108. * @return \Closure
  109. */
  110. protected function getErrorHandler()
  111. {
  112. return function ($errorMessage) {
  113. return new Response\Error($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 writeRequest(CommandInterface $command)
  141. {
  142. $arguments = $command->getArguments();
  143. array_unshift($arguments, $command->getId());
  144. $this->writeBytes(phpiredis_format_command($arguments));
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function __wakeup()
  150. {
  151. $this->assertExtensions();
  152. $this->reader = $this->createReader();
  153. }
  154. }