RedisClusterHashStrategy.php 1.3 KB

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