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.
This commit is contained in:
Daniele Alessandri
2016-05-10 14:19:05 +02:00
parent d8da74adac
commit 7664f1f29b
3 changed files with 19 additions and 11 deletions
+1 -5
View File
@@ -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.
+11 -6
View File
@@ -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) {
$connection = $replication;
} else {
$connection = $options->cluster;
}
} elseif ($options->defined('replication')) {
$replication = $options->replication;
if ($replication instanceof AggregateConnectionInterface) {
$connection = $replication;
$options->connections->aggregate($connection, $parameters);
} else {
$initializer = $this->getConnectionInitializerWrapper($replication);
$connection = $initializer($parameters, $options);
}
} else {
$connection = $options->cluster;
$options->connections->aggregate($connection, $parameters);
}
+7
View File
@@ -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)