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:
Daniele Alessandri
2014-02-11 15:44:37 +01:00
parent ba0dc12d5b
commit 26289f47ec
2 changed files with 27 additions and 6 deletions
+22 -1
View File
@@ -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;
}
}
+5 -5
View File
@@ -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'));
}
/**