mirror of
https://github.com/predis/predis.git
synced 2026-09-05 07:16:44 +00:00
Add support for key hash tags in redis-cluster (Redis 3.0.0b1).
Multi-keys operations are not allowed even when keys generate the same hash but this will probably be supported in later betas of Redis which means we will basically end up reusing the whole strategy used for client-side sharding. Ported from the v0.8 branch.
This commit is contained in:
@@ -289,6 +289,27 @@ class RedisStrategy implements StrategyInterface
|
||||
*/
|
||||
public function getKeyHash($key)
|
||||
{
|
||||
return $this->hashGenerator->hash($key);
|
||||
$key = $this->extractKeyTag($key);
|
||||
$hash = $this->hashGenerator->hash($key);
|
||||
|
||||
return $hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns only the hashable part of a key (delimited by "{...}"), or the
|
||||
* whole key if a key tag is not found in the string.
|
||||
*
|
||||
* @param string $key A key.
|
||||
* @return string
|
||||
*/
|
||||
protected function extractKeyTag($key)
|
||||
{
|
||||
if (false !== $start = strpos($key, '{')) {
|
||||
if (false !== $end = strpos($key, '}', $start)) {
|
||||
$key = substr($key, ++$start, $end - $start);
|
||||
}
|
||||
}
|
||||
|
||||
return $key;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,14 +22,14 @@ class RedisStrategyTest extends PredisTestCase
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testDoesNotSupportKeyTags()
|
||||
public function testSupportsKeyTags()
|
||||
{
|
||||
$strategy = $this->getClusterStrategy();
|
||||
|
||||
$this->assertSame(35910, $strategy->getKeyHash('{foo}'));
|
||||
$this->assertSame(60032, $strategy->getKeyHash('{foo}:bar'));
|
||||
$this->assertSame(27528, $strategy->getKeyHash('{foo}:baz'));
|
||||
$this->assertSame(34064, $strategy->getKeyHash('bar:{foo}:bar'));
|
||||
$this->assertSame(44950, $strategy->getKeyHash('{foo}'));
|
||||
$this->assertSame(44950, $strategy->getKeyHash('{foo}:bar'));
|
||||
$this->assertSame(44950, $strategy->getKeyHash('{foo}:baz'));
|
||||
$this->assertSame(44950, $strategy->getKeyHash('bar:{foo}:bar'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user