فهرست منبع

Improve client configuration for redis-sentinel.

Predis\Client now requires a list of connection parameters pointing to
sentinel instances and mandatory options "replication" and "service" set
respectively to "sentinel" and the chosen name for the master instance.

  $sentinels = ['tcp://127.0.0.1:5381', 'tcp://127.0.0.1:5382'];
  $options   = ['replication' => 'sentinel', 'service' => 'mymaster'];
  $client    = new Predis\Client($sentinels, $options);

Despite being nice and clean on the outside I am not really fond of the
code being used internally to make this kind of configuration possible.
Improvements in this respect would require a few breaking changes (not
even an option for a minor release) so things will change for the good
with Predis 2.0.
Daniele Alessandri 8 سال پیش
والد
کامیت
7664f1f29b
3فایلهای تغییر یافته به همراه17 افزوده شده و 9 حذف شده
  1. 1 5
      examples/replication_sentinel.php
  2. 9 4
      src/Client.php
  3. 7 0
      src/Configuration/ReplicationOption.php

+ 1 - 5
examples/replication_sentinel.php

@@ -34,12 +34,8 @@ $sentinels = array(
 );
 
 $client = new Predis\Client($sentinels, array(
+    'replication' => 'sentinel',
     'service' => 'mymaster',
-    'aggregate' => function () {
-        return function ($sentinels, $options) {
-            return new SentinelReplication($sentinels, $options->service, $options->connections);
-        };
-    },
 ));
 
 // Read operation.

+ 9 - 4
src/Client.php

@@ -120,13 +120,18 @@ class Client implements ClientInterface
             if ($options->defined('aggregate')) {
                 $initializer = $this->getConnectionInitializerWrapper($options->aggregate);
                 $connection = $initializer($parameters, $options);
-            } else {
-                if ($options->defined('replication') && $replication = $options->replication) {
+            } elseif ($options->defined('replication')) {
+                $replication = $options->replication;
+
+                if ($replication instanceof AggregateConnectionInterface) {
                     $connection = $replication;
+                    $options->connections->aggregate($connection, $parameters);
                 } else {
-                    $connection = $options->cluster;
+                    $initializer = $this->getConnectionInitializerWrapper($replication);
+                    $connection = $initializer($parameters, $options);
                 }
-
+            } else {
+                $connection = $options->cluster;
                 $options->connections->aggregate($connection, $parameters);
             }
 

+ 7 - 0
src/Configuration/ReplicationOption.php

@@ -12,6 +12,7 @@
 namespace Predis\Configuration;
 
 use Predis\Connection\Aggregate\MasterSlaveReplication;
+use Predis\Connection\Aggregate\SentinelReplication;
 use Predis\Connection\Aggregate\ReplicationInterface;
 
 /**
@@ -39,6 +40,12 @@ class ReplicationOption implements OptionInterface
             return $value ? $this->getDefault($options) : null;
         }
 
+        if ($value === 'sentinel') {
+            return function ($sentinels, $options) {
+                return new SentinelReplication($sentinels, $options->service, $options->connections);
+            };
+        }
+
         if (
             !is_object($value) &&
             null !== $asbool = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)