Ver código fonte

User-configurable key distribution algorithm to be used for clustered connections.

Daniele Alessandri 15 anos atrás
pai
commit
8f50c5f69a
1 arquivos alterados com 21 adições e 1 exclusões
  1. 21 1
      lib/Predis.php

+ 21 - 1
lib/Predis.php

@@ -93,7 +93,7 @@ class Client {
         }
 
         if (is_array($parameters) && isset($parameters[0])) {
-            $cluster = new ConnectionCluster();
+            $cluster = new ConnectionCluster($this->_options->key_distribution);
             foreach ($parameters as $shardParams) {
                 $cluster->add($this->createConnection($shardParams));
             }
@@ -228,6 +228,25 @@ class ClientOptionsProfile implements IClientOptionsHandler {
     }
 }
 
+class ClientOptionsKeyDistribution implements IClientOptionsHandler {
+    public function validate($option, $value) {
+        if ($value instanceof \Predis\Utilities\IRing) {
+            return $value;
+        }
+        if (is_string($value)) {
+            $valueReflection = new \ReflectionClass($value);
+            if ($valueReflection->isSubclassOf('\Predis\Utilities\IRing')) {
+                return new $value;
+            }
+        }
+        throw new \InvalidArgumentException("Invalid value for option $option");
+    }
+
+    public function getDefault() {
+        return new \Predis\Utilities\HashRing();
+    }
+}
+
 class ClientOptionsIterableMultiBulk implements IClientOptionsHandler {
     public function validate($option, $value) {
         return (bool) $value;
@@ -266,6 +285,7 @@ class ClientOptions {
     private static function getOptionsHandlers() {
         return array(
             'profile'    => new \Predis\ClientOptionsProfile(),
+            'key_distribution' => new \Predis\ClientOptionsKeyDistribution(),
             'iterable_multibulk' => new \Predis\ClientOptionsIterableMultiBulk(),
             'throw_on_error' => new \Predis\ClientOptionsThrowOnError(),
         );