PredisCluster.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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\Network;
  11. use Predis\Commands\ICommand;
  12. use Predis\Distribution\IDistributionStrategy;
  13. use Predis\Helpers;
  14. use Predis\ClientException;
  15. use Predis\NotSupportedException;
  16. use Predis\Distribution\HashRing;
  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 IConnectionCluster, \IteratorAggregate, \Countable
  25. {
  26. private $pool;
  27. private $distributor;
  28. /**
  29. * @param IDistributionStrategy $distributor Distribution strategy used by the cluster.
  30. */
  31. public function __construct(IDistributionStrategy $distributor = null)
  32. {
  33. $this->pool = array();
  34. $this->distributor = $distributor ?: new HashRing();
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function isConnected()
  40. {
  41. foreach ($this->pool as $connection) {
  42. if ($connection->isConnected()) {
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function connect()
  52. {
  53. foreach ($this->pool as $connection) {
  54. $connection->connect();
  55. }
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function disconnect()
  61. {
  62. foreach ($this->pool as $connection) {
  63. $connection->disconnect();
  64. }
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function add(IConnectionSingle $connection)
  70. {
  71. $parameters = $connection->getParameters();
  72. if (isset($parameters->alias)) {
  73. $this->pool[$parameters->alias] = $connection;
  74. }
  75. else {
  76. $this->pool[] = $connection;
  77. }
  78. $weight = isset($parameters->weight) ? $parameters->weight : null;
  79. $this->distributor->add($connection, $weight);
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function remove(IConnectionSingle $connection)
  85. {
  86. if (($id = array_search($connection, $this->pool, true)) !== false) {
  87. unset($this->pool[$id]);
  88. $this->distributor->remove($connection);
  89. return true;
  90. }
  91. return false;
  92. }
  93. /**
  94. * Removes a connection instance using its alias or index.
  95. *
  96. * @param string $connectionId Alias or index of a connection.
  97. * @return Boolean Returns true if the connection was in the pool.
  98. */
  99. public function removeById($connectionId)
  100. {
  101. if ($connection = $this->getConnectionById($connectionId)) {
  102. return $this->remove($connection);
  103. }
  104. return false;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function getConnection(ICommand $command)
  110. {
  111. $cmdHash = $command->getHash($this->distributor);
  112. if (isset($cmdHash)) {
  113. return $this->distributor->get($cmdHash);
  114. }
  115. $message = sprintf("Cannot send '%s' commands to a cluster of connections", $command->getId());
  116. throw new NotSupportedException($message);
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function getConnectionById($id = null)
  122. {
  123. $alias = $id ?: 0;
  124. return isset($this->pool[$alias]) ? $this->pool[$alias] : null;
  125. }
  126. /**
  127. * Retrieves a connection instance from the cluster using a key.
  128. *
  129. * @param string $key Key of a Redis value.
  130. * @return IConnectionSingle
  131. */
  132. public function getConnectionByKey($key)
  133. {
  134. $hashablePart = Helpers::extractKeyTag($key);
  135. $keyHash = $this->distributor->generateKey($hashablePart);
  136. return $this->distributor->get($keyHash);
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function count()
  142. {
  143. return count($this->pool);
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function getIterator()
  149. {
  150. return new \ArrayIterator($this->pool);
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function writeCommand(ICommand $command)
  156. {
  157. $this->getConnection($command)->writeCommand($command);
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function readResponse(ICommand $command)
  163. {
  164. return $this->getConnection($command)->readResponse($command);
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function executeCommand(ICommand $command)
  170. {
  171. return $this->getConnection($command)->executeCommand($command);
  172. }
  173. }