custom_cluster_distributor.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. require __DIR__.'/shared.php';
  11. // Developers can implement Predis\Distribution\DistributorInterface to create
  12. // their own distributors used by the client to distribute keys among a cluster
  13. // of servers.
  14. use Predis\Cluster\PredisStrategy;
  15. use Predis\Cluster\Distributor\DistributorInterface;
  16. use Predis\Cluster\Hash\HashGeneratorInterface;
  17. use Predis\Connection\Aggregate\PredisCluster;
  18. class NaiveDistributor implements DistributorInterface, HashGeneratorInterface
  19. {
  20. private $nodes;
  21. private $nodesCount;
  22. public function __construct()
  23. {
  24. $this->nodes = array();
  25. $this->nodesCount = 0;
  26. }
  27. public function add($node, $weight = null)
  28. {
  29. $this->nodes[] = $node;
  30. $this->nodesCount++;
  31. }
  32. public function remove($node)
  33. {
  34. $this->nodes = array_filter($this->nodes, function ($n) use ($node) {
  35. return $n !== $node;
  36. });
  37. $this->nodesCount = count($this->nodes);
  38. }
  39. public function getSlot($hash)
  40. {
  41. return $this->nodesCount > 1 ? abs($hash % $this->nodesCount) : 0;
  42. }
  43. public function getBySlot($slot)
  44. {
  45. if (isset($this->nodes[$slot])) {
  46. return $this->nodes[$slot];
  47. }
  48. }
  49. public function getByHash($hash)
  50. {
  51. if (!$this->nodesCount) {
  52. throw new RuntimeException('No connections.');
  53. }
  54. $slot = $this->getSlot($hash);
  55. $node = $this->getBySlot($slot);
  56. return $node;
  57. }
  58. public function get($value)
  59. {
  60. $hash = $this->hash($value);
  61. $node = $this->getByHash($hash);
  62. return $node;
  63. }
  64. public function hash($value)
  65. {
  66. return crc32($value);
  67. }
  68. public function getHashGenerator()
  69. {
  70. return $this;
  71. }
  72. }
  73. $options = array(
  74. 'cluster' => function () {
  75. $distributor = new NaiveDistributor();
  76. $strategy = new PredisStrategy($distributor);
  77. $cluster = new PredisCluster($strategy);
  78. return $cluster;
  79. },
  80. );
  81. $client = new Predis\Client($multiple_servers, $options);
  82. for ($i = 0; $i < 100; $i++) {
  83. $client->set("key:$i", str_pad($i, 4, '0', 0));
  84. $client->get("key:$i");
  85. }
  86. $server1 = $client->getClientFor('first')->info();
  87. $server2 = $client->getClientFor('second')->info();
  88. if (isset($server1['Keyspace'], $server2['Keyspace'])) {
  89. $server1 = $server1['Keyspace'];
  90. $server2 = $server2['Keyspace'];
  91. }
  92. printf("Server '%s' has %d keys while server '%s' has %d keys.\n",
  93. 'first', $server1['db15']['keys'], 'second', $server2['db15']['keys']
  94. );