PhpiredisStreamConnection.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. Everything
  18. * is highly experimental (even the very same phpiredis since it is quite new),
  19. * so use it at your own risk.
  20. *
  21. * This class is mainly intended to provide an optional low-overhead alternative
  22. * for processing replies from Redis compared to the standard pure-PHP classes.
  23. * Differences in speed when dealing with short inline replies are practically
  24. * nonexistent, the actual speed boost is for long multibulk replies when this
  25. * protocol processor can parse and return replies very fast.
  26. *
  27. * For instructions on how to build and install the phpiredis extension, please
  28. * consult the repository of the project.
  29. *
  30. * The connection parameters supported by this class are:
  31. *
  32. * - scheme: it can be either 'tcp' or 'unix'.
  33. * - host: hostname or IP address of the server.
  34. * - port: TCP port of the server.
  35. * - timeout: timeout to perform the connection.
  36. * - read_write_timeout: timeout of read / write operations.
  37. * - async_connect: performs the connection asynchronously.
  38. * - tcp_nodelay: enables or disables Nagle's algorithm for coalescing.
  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->assertExtensions();
  53. parent::__construct($parameters);
  54. $this->reader = $this->createReader();
  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. private function assertExtensions()
  68. {
  69. if (!extension_loaded('phpiredis')) {
  70. throw new NotSupportedException(
  71. 'The "phpiredis" extension is required by this connection backend'
  72. );
  73. }
  74. }
  75. /**
  76. * Creates a new instance of the protocol reader resource.
  77. *
  78. * @return resource
  79. */
  80. private function createReader()
  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. return $reader;
  86. }
  87. /**
  88. * Returns the underlying protocol reader resource.
  89. *
  90. * @return resource
  91. */
  92. protected function getReader()
  93. {
  94. return $this->reader;
  95. }
  96. /**
  97. * Returns the handler used by the protocol reader to handle status replies.
  98. *
  99. * @return \Closure
  100. */
  101. protected function getStatusHandler()
  102. {
  103. return function ($payload) {
  104. return Response\Status::get($payload);
  105. };
  106. }
  107. /**
  108. * Returns the handler used by the protocol reader to handle Redis errors.
  109. *
  110. * @return \Closure
  111. */
  112. protected function getErrorHandler()
  113. {
  114. return function ($errorMessage) {
  115. return new Response\Error($errorMessage);
  116. };
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function read()
  122. {
  123. $socket = $this->getResource();
  124. $reader = $this->reader;
  125. while (PHPIREDIS_READER_STATE_INCOMPLETE === $state = phpiredis_reader_get_state($reader)) {
  126. $buffer = fread($socket, 4096);
  127. if ($buffer === false || $buffer === '') {
  128. $this->onConnectionError('Error while reading bytes from the server');
  129. return;
  130. }
  131. phpiredis_reader_feed($reader, $buffer);
  132. }
  133. if ($state === PHPIREDIS_READER_STATE_COMPLETE) {
  134. return phpiredis_reader_get_reply($reader);
  135. } else {
  136. $this->onProtocolError(phpiredis_reader_get_error($reader));
  137. }
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function writeRequest(CommandInterface $command)
  143. {
  144. $arguments = $command->getArguments();
  145. array_unshift($arguments, $command->getId());
  146. $this->writeBytes(phpiredis_format_command($arguments));
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function __wakeup()
  152. {
  153. $this->assertExtensions();
  154. $this->reader = $this->createReader();
  155. }
  156. }