From 8fbe658ca825692dbc2c464692bcfa2fe150a01d Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Tue, 11 Feb 2014 18:22:03 +0100 Subject: [PATCH] Fix implementation for hash tags extraction from keys. We now fully comply with the specifications defined by Redis. --- lib/Predis/Cluster/RedisStrategy.php | 4 ++-- tests/Predis/Cluster/RedisStrategyTest.php | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/Predis/Cluster/RedisStrategy.php b/lib/Predis/Cluster/RedisStrategy.php index 8974e759..6b508745 100644 --- a/lib/Predis/Cluster/RedisStrategy.php +++ b/lib/Predis/Cluster/RedisStrategy.php @@ -305,8 +305,8 @@ class RedisStrategy implements StrategyInterface protected function extractKeyTag($key) { if (false !== $start = strpos($key, '{')) { - if (false !== $end = strpos($key, '}', $start)) { - $key = substr($key, ++$start, $end - $start); + if (false !== ($end = strpos($key, '}', $start)) && $end !== ++$start) { + $key = substr($key, $start, $end - $start); } } diff --git a/tests/Predis/Cluster/RedisStrategyTest.php b/tests/Predis/Cluster/RedisStrategyTest.php index 667971c1..f26417ef 100644 --- a/tests/Predis/Cluster/RedisStrategyTest.php +++ b/tests/Predis/Cluster/RedisStrategyTest.php @@ -29,7 +29,14 @@ class RedisStrategyTest extends PredisTestCase $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')); + $this->assertSame(44950, $strategy->getKeyHash('bar:{foo}:baz')); + $this->assertSame(44950, $strategy->getKeyHash('bar:{foo}:{baz}')); + + $this->assertSame(44950, $strategy->getKeyHash('bar:{foo}:baz{}')); + $this->assertSame(9415, $strategy->getKeyHash('{}bar:{foo}:baz')); + + $this->assertSame(0, $strategy->getKeyHash('')); + $this->assertSame(31641, $strategy->getKeyHash('{}')); } /**