RedisClusterHashStrategy.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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\Cluster\Hash\CRC16HashGenerator;
  12. use Predis\Cluster\Hash\HashGeneratorInterface;
  13. use Predis\Command\CommandInterface;
  14. use Predis\Command\ScriptedCommand;
  15. /**
  16. * Default class used by Predis to calculate hashes out of keys of
  17. * commands supported by redis-cluster.
  18. *
  19. * @author Daniele Alessandri <suppakilla@gmail.com>
  20. */
  21. class RedisClusterHashStrategy extends PredisClusterHashStrategy
  22. {
  23. /**
  24. *
  25. */
  26. public function __construct(HashGeneratorInterface $hashGenerator = null)
  27. {
  28. parent::__construct($hashGenerator ?: new CRC16HashGenerator());
  29. }
  30. /**
  31. * Returns only the hashable part of a key (delimited by "{...}"), or the
  32. * whole key if a key tag is not found in the string.
  33. *
  34. * @param string $key A key.
  35. * @return string
  36. */
  37. protected function extractKeyTag($key)
  38. {
  39. if (false !== $start = strpos($key, '{')) {
  40. if (false !== ($end = strpos($key, '}', $start)) && $end !== ++$start) {
  41. $key = substr($key, $start, $end - $start);
  42. }
  43. }
  44. return $key;
  45. }
  46. }