PageRenderTime 26ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/core/resources/libraries/phpfastcache/phpfastcache/_extensions/predis-1.0/src/Connection/Aggregate/PredisCluster.php

https://github.com/otto-torino/gino
PHP | 237 lines | 123 code | 33 blank | 81 comment | 6 complexity | 1430178bba546bf128088cbbe40a38fe 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\Aggregate;
  11. use ArrayIterator;
  12. use Countable;
  13. use IteratorAggregate;
  14. use Predis\NotSupportedException;
  15. use Predis\Cluster\PredisStrategy;
  16. use Predis\Cluster\StrategyInterface;
  17. use Predis\Command\CommandInterface;
  18. use Predis\Connection\NodeConnectionInterface;
  19. /**
  20. * Abstraction for a cluster of aggregate connections to various Redis servers
  21. * implementing client-side sharding based on pluggable distribution strategies.
  22. *
  23. * @author Daniele Alessandri <suppakilla@gmail.com>
  24. * @todo Add the ability to remove connections from pool.
  25. */
  26. class PredisCluster implements ClusterInterface, IteratorAggregate, Countable
  27. {
  28. private $pool;
  29. private $strategy;
  30. private $distributor;
  31. /**
  32. * @param StrategyInterface $strategy Optional cluster strategy.
  33. */
  34. public function __construct(StrategyInterface $strategy = null)
  35. {
  36. $this->pool = array();
  37. $this->strategy = $strategy ?: new PredisStrategy();
  38. $this->distributor = $this->strategy->getDistributor();
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function isConnected()
  44. {
  45. foreach ($this->pool as $connection) {
  46. if ($connection->isConnected()) {
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function connect()
  56. {
  57. foreach ($this->pool as $connection) {
  58. $connection->connect();
  59. }
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function disconnect()
  65. {
  66. foreach ($this->pool as $connection) {
  67. $connection->disconnect();
  68. }
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function add(NodeConnectionInterface $connection)
  74. {
  75. $parameters = $connection->getParameters();
  76. if (isset($parameters->alias)) {
  77. $this->pool[$parameters->alias] = $connection;
  78. } else {
  79. $this->pool[] = $connection;
  80. }
  81. $weight = isset($parameters->weight) ? $parameters->weight : null;
  82. $this->distributor->add($connection, $weight);
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function remove(NodeConnectionInterface $connection)
  88. {
  89. if (($id = array_search($connection, $this->pool, true)) !== false) {
  90. unset($this->pool[$id]);
  91. $this->distributor->remove($connection);
  92. return true;
  93. }
  94. return false;
  95. }
  96. /**
  97. * Removes a connection instance using its alias or index.
  98. *
  99. * @param string $connectionID Alias or index of a connection.
  100. *
  101. * @return bool Returns true if the connection was in the pool.
  102. */
  103. public function removeById($connectionID)
  104. {
  105. if ($connection = $this->getConnectionById($connectionID)) {
  106. return $this->remove($connection);
  107. }
  108. return false;
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function getConnection(CommandInterface $command)
  114. {
  115. $slot = $this->strategy->getSlot($command);
  116. if (!isset($slot)) {
  117. throw new NotSupportedException(
  118. "Cannot use '{$command->getId()}' over clusters of connections."
  119. );
  120. }
  121. $node = $this->distributor->getBySlot($slot);
  122. return $node;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function getConnectionById($connectionID)
  128. {
  129. return isset($this->pool[$connectionID]) ? $this->pool[$connectionID] : null;
  130. }
  131. /**
  132. * Retrieves a connection instance from the cluster using a key.
  133. *
  134. * @param string $key Key string.
  135. *
  136. * @return NodeConnectionInterface
  137. */
  138. public function getConnectionByKey($key)
  139. {
  140. $hash = $this->strategy->getSlotByKey($key);
  141. $node = $this->distributor->getBySlot($hash);
  142. return $node;
  143. }
  144. /**
  145. * Returns the underlying command hash strategy used to hash commands by
  146. * using keys found in their arguments.
  147. *
  148. * @return StrategyInterface
  149. */
  150. public function getClusterStrategy()
  151. {
  152. return $this->strategy;
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function count()
  158. {
  159. return count($this->pool);
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function getIterator()
  165. {
  166. return new ArrayIterator($this->pool);
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. public function writeRequest(CommandInterface $command)
  172. {
  173. $this->getConnection($command)->writeRequest($command);
  174. }
  175. /**
  176. * {@inheritdoc}
  177. */
  178. public function readResponse(CommandInterface $command)
  179. {
  180. return $this->getConnection($command)->readResponse($command);
  181. }
  182. /**
  183. * {@inheritdoc}
  184. */
  185. public function executeCommand(CommandInterface $command)
  186. {
  187. return $this->getConnection($command)->executeCommand($command);
  188. }
  189. /**
  190. * Executes the specified Redis command on all the nodes of a cluster.
  191. *
  192. * @param CommandInterface $command A Redis command.
  193. *
  194. * @return array
  195. */
  196. public function executeCommandOnNodes(CommandInterface $command)
  197. {
  198. $responses = array();
  199. foreach ($this->pool as $connection) {
  200. $responses[] = $connection->executeCommand($command);
  201. }
  202. return $responses;
  203. }
  204. }