public function __construct($servers, $replicas = 128, $standAlone = false)
{
$this->clients = array();
$this->masterClient = null;
$this->aliases = array();
$this->ring = array();
$this->replicas = (int) $replicas;
$client = null;
foreach ($servers as $server) {
if (is_array($server)) {
$client = new Credis_Client($server['host'], $server['port'], isset($server['timeout']) ? $server['timeout'] : 2.5, isset($server['persistent']) ? $server['persistent'] : '', isset($server['db']) ? $server['db'] : 0, isset($server['password']) ? $server['password'] : null);
if (isset($server['alias'])) {
$this->aliases[$server['alias']] = $client;
}
if (isset($server['master']) && $server['master'] === true) {
$this->masterClient = $client;
if (isset($server['write_only']) && $server['write_only'] === true) {
continue;
}
}
} elseif ($server instanceof Credis_Client) {
$client = $server;
} else {
throw new CredisException('Server should either be an array or an instance of Credis_Client');
}
if ($standAlone) {
$client->forceStandalone();
}
$this->clients[] = $client;
for ($replica = 0; $replica <= $this->replicas; $replica++) {
$md5num = hexdec(substr(md5($client->getHost() . ':' . $client->getPort() . '-' . $replica), 0, 7));
$this->ring[$md5num] = count($this->clients) - 1;
}
}
ksort($this->ring, SORT_NUMERIC);
$this->nodes = array_keys($this->ring);
$this->dont_hash = array_flip(array('RANDOMKEY', 'DBSIZE', 'PIPELINE', 'EXEC', 'SELECT', 'MOVE', 'FLUSHDB', 'FLUSHALL', 'SAVE', 'BGSAVE', 'LASTSAVE', 'SHUTDOWN', 'INFO', 'MONITOR', 'SLAVEOF'));
if ($this->masterClient !== null && count($this->clients()) == 0) {
$this->clients[] = $this->masterClient;
for ($replica = 0; $replica <= $this->replicas; $replica++) {
$md5num = hexdec(substr(md5($this->masterClient->getHost() . ':' . $this->masterClient->getHost() . '-' . $replica), 0, 7));
$this->ring[$md5num] = count($this->clients) - 1;
}
$this->nodes = array_keys($this->ring);
}
}