Quellcode durchsuchen

Minor optimization in the HashRing class when adding/removing nodes from the ring

Daniele Alessandri vor 15 Jahren
Ursprung
Commit
7dde9ff93c
1 geänderte Dateien mit 4 neuen und 2 gelöschten Zeilen
  1. 4 2
      lib/Predis.php

+ 4 - 2
lib/Predis.php

@@ -679,8 +679,9 @@ class HashRing {
     }
 
     public function add($node) {
+        $nodeHash = (string) $node;
         for ($i = 0; $i < self::NUMBER_OF_REPLICAS; $i++) {
-            $key = crc32((string)$node . ':' . $i);
+            $key = crc32($nodeHash . ':' . $i);
             $this->_ring[$key] = $node;
         }
         ksort($this->_ring, SORT_NUMERIC);
@@ -688,8 +689,9 @@ class HashRing {
     }
 
     public function remove($node) {
+        $nodeHash = (string) $node;
         for ($i = 0; $i < self::NUMBER_OF_REPLICAS; $i++) {
-            $key = crc32((string)$node . '_' . $i);
+            $key = crc32($nodeHash . '_' . $i);
             unset($this->_ring[$key]);
             $this->_ringKeys = array_filter($this->_ringKeys, function($rk) use($key) {
                 return $rk !== $key;