RedisStrategy.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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\Cluster;
  11. use Predis\NotSupportedException;
  12. use Predis\Cluster\Hash\HashGeneratorInterface;
  13. use Predis\Cluster\Hash\CRC16;
  14. /**
  15. * Default class used by Predis to calculate hashes out of keys of
  16. * commands supported by redis-cluster.
  17. *
  18. * @author Daniele Alessandri <suppakilla@gmail.com>
  19. */
  20. class RedisStrategy extends ClusterStrategy
  21. {
  22. protected $hashGenerator;
  23. /**
  24. * @param HashGeneratorInterface $hashGenerator Hash generator instance.
  25. */
  26. public function __construct(HashGeneratorInterface $hashGenerator = null)
  27. {
  28. parent::__construct();
  29. $this->hashGenerator = $hashGenerator ?: new CRC16();
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function getSlotByKey($key)
  35. {
  36. $key = $this->extractKeyTag($key);
  37. $slot = $this->hashGenerator->hash($key) & 0x3FFF;
  38. return $slot;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getDistributor()
  44. {
  45. throw new NotSupportedException(
  46. 'This cluster strategy does not provide an external distributor'
  47. );
  48. }
  49. }