PageRenderTime 75ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/predis/predis/lib/Predis/Connection/PhpiredisStreamConnection.php

https://bitbucket.org/larryg/powerhut
PHP | 197 lines | 95 code | 26 blank | 76 comment | 8 complexity | 6705eae91b94994f8fdfd120f2584a2f MD5 | raw file
  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. * {@inheritdoc}
  78. */
  79. protected function checkParameters(ConnectionParametersInterface $parameters)
  80. {
  81. if (isset($parameters->iterable_multibulk)) {
  82. $this->onInvalidOption('iterable_multibulk', $parameters);
  83. }
  84. return parent::checkParameters($parameters);
  85. }
  86. /**
  87. * Initializes the protocol reader resource.
  88. */
  89. protected function initializeReader()
  90. {
  91. $reader = phpiredis_reader_create();
  92. phpiredis_reader_set_status_handler($reader, $this->getStatusHandler());
  93. phpiredis_reader_set_error_handler($reader, $this->getErrorHandler());
  94. $this->reader = $reader;
  95. }
  96. /**
  97. * Gets 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. switch ($payload) {
  105. case 'OK':
  106. return true;
  107. case 'QUEUED':
  108. return new ResponseQueued();
  109. default:
  110. return $payload;
  111. }
  112. };
  113. }
  114. /**
  115. * Gets the handler used by the protocol reader to handle Redis errors.
  116. *
  117. * @param Boolean $throw_errors Specify if Redis errors throw exceptions.
  118. * @return \Closure
  119. */
  120. protected function getErrorHandler()
  121. {
  122. return function ($errorMessage) {
  123. return new ResponseError($errorMessage);
  124. };
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function read()
  130. {
  131. $socket = $this->getResource();
  132. $reader = $this->reader;
  133. while (PHPIREDIS_READER_STATE_INCOMPLETE === $state = phpiredis_reader_get_state($reader)) {
  134. $buffer = fread($socket, 4096);
  135. if ($buffer === false || $buffer === '') {
  136. $this->onConnectionError('Error while reading bytes from the server');
  137. return;
  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. }