فهرست منبع

Fix implementation for hash tags extraction from keys.

We now fully comply with the specifications defined by Redis.
Daniele Alessandri 11 سال پیش
والد
کامیت
809f76a7b3
2فایلهای تغییر یافته به همراه10 افزوده شده و 3 حذف شده
  1. 2 2
      lib/Predis/Cluster/RedisClusterHashStrategy.php
  2. 8 1
      tests/Predis/Cluster/RedisClusterHashStrategyTest.php

+ 2 - 2
lib/Predis/Cluster/RedisClusterHashStrategy.php

@@ -303,8 +303,8 @@ class RedisClusterHashStrategy implements CommandHashStrategyInterface
     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);
             }
         }
 

+ 8 - 1
tests/Predis/Cluster/RedisClusterHashStrategyTest.php

@@ -29,7 +29,14 @@ class RedisClusterHashStrategyTest 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('{}'));
     }
 
     /**