PredisCluster.php 5.5 KB

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