Credis_Sentinel::createCluster PHP Method

createCluster() public method

When $selectRandomSlave is false, all clients are passed and hashing is applied in Credis_Cluster When $writeOnly is false, the master server will also be used for read commands.
Deprecation:
public createCluster ( string $name, integer $db, integer $replicas = 128, boolean $selectRandomSlave = true, boolean $writeOnly = false ) : Credis_Cluster
$name string
$db integer
$replicas integer
$selectRandomSlave boolean
$writeOnly boolean
return Credis_Cluster
    public function createCluster($name, $db = 0, $replicas = 128, $selectRandomSlave = true, $writeOnly = false)
    {
        $clients = array();
        $workingClients = array();
        $master = $this->master($name);
        if (strstr($master[9], 's_down') || strstr($master[9], 'disconnected')) {
            throw new CredisException('The master is down');
        }
        $slaves = $this->slaves($name);
        foreach ($slaves as $slave) {
            if (!strstr($slave[9], 's_down') && !strstr($slave[9], 'disconnected')) {
                $workingClients[] = array('host' => $slave[3], 'port' => $slave[5], 'master' => false, 'db' => $db, 'password' => $this->_password);
            }
        }
        if (count($workingClients) > 0) {
            if ($selectRandomSlave) {
                if (!$writeOnly) {
                    $workingClients[] = array('host' => $master[3], 'port' => $master[5], 'master' => false, 'db' => $db, 'password' => $this->_password);
                }
                $clients[] = $workingClients[rand(0, count($workingClients) - 1)];
            } else {
                $clients = $workingClients;
            }
        }
        $clients[] = array('host' => $master[3], 'port' => $master[5], 'db' => $db, 'master' => true, 'write_only' => $writeOnly, 'password' => $this->_password);
        return new Credis_Cluster($clients, $replicas, $this->_standAlone);
    }

Usage Example

Exemplo n.º 1
0
 public function testCreateCluster()
 {
     $cluster = $this->sentinel->createCluster($this->sentinelConfig->clustername);
     $this->assertInstanceOf('Credis_Cluster', $cluster);
     $this->assertCount(2, $cluster->clients());
     $cluster = $this->sentinel->createCluster($this->sentinelConfig->clustername, 0, 1, false);
     $this->assertInstanceOf('Credis_Cluster', $cluster);
     $this->assertCount(2, $cluster->clients());
     $this->setExpectedException('CredisException', 'The master is down');
     $this->sentinel->createCluster($this->sentinelConfig->downclustername);
 }