PredisCluster.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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
  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 getConnection(ICommand $command)
  85. {
  86. $cmdHash = $command->getHash($this->distributor);
  87. if (isset($cmdHash)) {
  88. return $this->distributor->get($cmdHash);
  89. }
  90. $message = sprintf("Cannot send '%s' commands to a cluster of connections", $command->getId());
  91. throw new NotSupportedException($message);
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function getConnectionById($id = null)
  97. {
  98. $alias = $id ?: 0;
  99. return isset($this->pool[$alias]) ? $this->pool[$alias] : null;
  100. }
  101. /**
  102. * Retrieves a connection instance from the cluster using a key.
  103. *
  104. * @param string $key Key of a Redis value.
  105. * @return IConnectionSingle
  106. */
  107. public function getConnectionByKey($key)
  108. {
  109. $hashablePart = Helpers::extractKeyTag($key);
  110. $keyHash = $this->distributor->generateKey($hashablePart);
  111. return $this->distributor->get($keyHash);
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function getIterator()
  117. {
  118. return new \ArrayIterator($this->pool);
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function writeCommand(ICommand $command)
  124. {
  125. $this->getConnection($command)->writeCommand($command);
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. public function readResponse(ICommand $command)
  131. {
  132. return $this->getConnection($command)->readResponse($command);
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. public function executeCommand(ICommand $command)
  138. {
  139. return $this->getConnection($command)->executeCommand($command);
  140. }
  141. }