Bläddra i källkod

Apply common parameters to connections created on the fly in cluster.

These parameters are applied only to connections being created on the
fly when not part of the current pool. This condition usually happens
upon -MOVE or -ASK responses returned by Redis to redirect client to
different nodes.
Daniele Alessandri 11 år sedan
förälder
incheckning
70df7ca64d
1 ändrade filer med 24 tillägg och 3 borttagningar
  1. 24 3
      lib/Predis/Connection/RedisCluster.php

+ 24 - 3
lib/Predis/Connection/RedisCluster.php

@@ -47,6 +47,7 @@ use Predis\Response;
 class RedisCluster implements ClusterConnectionInterface, IteratorAggregate, Countable
 {
     private $askClusterNodes = false;
+    private $defaultParameters = array();
     private $pool = array();
     private $slots = array();
     private $slotsMap;
@@ -298,11 +299,12 @@ class RedisCluster implements ClusterConnectionInterface, IteratorAggregate, Cou
 
         if (!$connection = $this->getConnectionById($connectionID)) {
             $host = explode(':', $connectionID, 2);
-            $connection = $this->connections->create(array(
+            $parameters = array_merge($this->defaultParameters, array(
                 'host' => $host[0],
                 'port' => $host[1],
             ));
 
+            $connection = $this->connections->create($parameters);
             $this->pool[$connectionID] = $connection;
         }
 
@@ -381,11 +383,12 @@ class RedisCluster implements ClusterConnectionInterface, IteratorAggregate, Cou
 
         if (!$connection) {
             $host = explode(':', $host, 2);
-
-            $connection = $this->connections->create(array(
+            $parameters = array_merge($this->defaultParameters, array(
                 'host' => $host[0],
                 'port' => $host[1],
             ));
+
+            $connection = $this->connections->create($parameters);
         }
 
         switch ($request) {
@@ -484,4 +487,22 @@ class RedisCluster implements ClusterConnectionInterface, IteratorAggregate, Cou
     {
         $this->askClusterNodes = (bool) $value;
     }
+
+    /**
+     * Sets a default array of connection parameters to be applied when creating
+     * new connection instances on the fly when they are not part of the initial
+     * pool supplied upon cluster initialization.
+     *
+     * These parameters are not applied to connections added to the pool using
+     * the add() method.
+     *
+     * @param array $parameters Array of connection parameters.
+     */
+    public function setDefaultParameters(array $parameters)
+    {
+        $this->defaultParameters = array_merge(
+            $this->defaultParameters,
+            $parameters ?: array()
+        );
+    }
 }