mirror of
https://github.com/predis/predis.git
synced 2026-09-14 20:37:29 +00:00
Remove the key_distribution option and allow user-defined callbacks to initialize a cluster connection.
This commit is contained in:
@@ -7,6 +7,7 @@ require 'SharedConfigurations.php';
|
||||
// that implements the Predis\Distribution\IDistributionStrategy interface.
|
||||
|
||||
use Predis\Distribution\IDistributionStrategy;
|
||||
use Predis\Network\ConnectionCluster;
|
||||
|
||||
class NaiveDistributionStrategy implements IDistributionStrategy {
|
||||
private $_nodes, $_nodesCount;
|
||||
@@ -42,7 +43,9 @@ class NaiveDistributionStrategy implements IDistributionStrategy {
|
||||
}
|
||||
|
||||
$options = array(
|
||||
'key_distribution' => new NaiveDistributionStrategy(),
|
||||
'cluster' => function() {
|
||||
return new ConnectionCluster(new NaiveDistributionStrategy());
|
||||
},
|
||||
);
|
||||
|
||||
$redis = new Predis\Client($multiple_servers, $options);
|
||||
|
||||
@@ -46,8 +46,7 @@ class Client {
|
||||
}
|
||||
if (is_array($parameters)) {
|
||||
if (isset($parameters[0])) {
|
||||
$clusterClass = $this->_options->cluster;
|
||||
$cluster = new $clusterClass($this->_options->key_distribution);
|
||||
$cluster = $this->_options->cluster;
|
||||
foreach ($parameters as $single) {
|
||||
$cluster->add($single instanceof IConnectionSingle
|
||||
? $single : $this->createConnection($single)
|
||||
|
||||
@@ -4,8 +4,7 @@ namespace Predis;
|
||||
|
||||
use Predis\Options\IOption;
|
||||
use Predis\Options\ClientProfile;
|
||||
use Predis\Options\ClientClusterType;
|
||||
use Predis\Options\ClientKeyDistribution;
|
||||
use Predis\Options\ClientCluster;
|
||||
use Predis\Options\ClientConnectionFactory;
|
||||
|
||||
class ClientOptions {
|
||||
@@ -22,9 +21,8 @@ class ClientOptions {
|
||||
}
|
||||
self::$_sharedOptions = array(
|
||||
'profile' => new ClientProfile(),
|
||||
'key_distribution' => new ClientKeyDistribution(),
|
||||
'connections' => new ClientConnectionFactory(),
|
||||
'cluster' => new ClientClusterType(),
|
||||
'cluster' => new ClientCluster(),
|
||||
);
|
||||
return self::$_sharedOptions;
|
||||
}
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Predis\Options;
|
||||
|
||||
use Predis\ClientException;
|
||||
|
||||
class ClientClusterType extends Option {
|
||||
const CLUSTER_INTERFACE = '\Predis\Network\IConnectionCluster';
|
||||
const CLUSTER_PREDIS = '\Predis\Network\ConnectionCluster';
|
||||
|
||||
public function validate($value) {
|
||||
switch ($value) {
|
||||
case 'client':
|
||||
return self::CLUSTER_PREDIS;
|
||||
default:
|
||||
return $this->checkClass($value);
|
||||
}
|
||||
}
|
||||
|
||||
private function checkClass($class) {
|
||||
$reflection = new \ReflectionClass($class);
|
||||
if (!$reflection->isSubclassOf(self::CLUSTER_INTERFACE)) {
|
||||
throw new ClientException(
|
||||
"The class $class is not a valid cluster connection"
|
||||
);
|
||||
}
|
||||
return $class;
|
||||
}
|
||||
|
||||
public function getDefault() {
|
||||
return self::CLUSTER_PREDIS;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Predis\Options;
|
||||
|
||||
use Predis\Distribution\IDistributionStrategy;
|
||||
use Predis\Distribution\HashRing;
|
||||
|
||||
class ClientKeyDistribution extends Option {
|
||||
public function validate($value) {
|
||||
if ($value instanceof IDistributionStrategy) {
|
||||
return $value;
|
||||
}
|
||||
if (is_string($value)) {
|
||||
$valueReflection = new \ReflectionClass($value);
|
||||
if ($valueReflection->isSubclassOf('\Predis\Distribution\IDistributionStrategy')) {
|
||||
return new $value;
|
||||
}
|
||||
}
|
||||
throw new \InvalidArgumentException('Invalid value for key distribution');
|
||||
}
|
||||
|
||||
public function getDefault() {
|
||||
return new HashRing();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user