Predis\Connection\Aggregate\PredisCluster::getConnection PHP Method

getConnection() public method

public getConnection ( Predis\Command\CommandInterface $command )
$command Predis\Command\CommandInterface
    public function getConnection(CommandInterface $command)
    {
        $slot = $this->strategy->getSlot($command);
        if (!isset($slot)) {
            throw new NotSupportedException("Cannot use '{$command->getId()}' over clusters of connections.");
        }
        $node = $this->distributor->getBySlot($slot);
        return $node;
    }

Usage Example

 /**
  * @group disconnected
  */
 public function testSupportsKeyHashTags()
 {
     $profile = Profile\Factory::getDefault();
     $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
     $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
     $cluster = new PredisCluster();
     $cluster->add($connection1);
     $cluster->add($connection2);
     $set = $profile->createCommand('set', array('{node:1001}:foo', 'foobar'));
     $get = $profile->createCommand('get', array('{node:1001}:foo'));
     $this->assertSame($connection1, $cluster->getConnection($set));
     $this->assertSame($connection1, $cluster->getConnection($get));
     $set = $profile->createCommand('set', array('{node:1001}:bar', 'foobar'));
     $get = $profile->createCommand('get', array('{node:1001}:bar'));
     $this->assertSame($connection1, $cluster->getConnection($set));
     $this->assertSame($connection1, $cluster->getConnection($get));
 }