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

getConnectionByKey() public method

Retrieves a connection instance from the cluster using a key.
public getConnectionByKey ( string $key ) : Predis\Connection\NodeConnectionInterface
$key string Key string.
return Predis\Connection\NodeConnectionInterface
    public function getConnectionByKey($key)
    {
        $hash = $this->strategy->getSlotByKey($key);
        $node = $this->distributor->getBySlot($hash);
        return $node;
    }

Usage Example

 /**
  * @group disconnected
  */
 public function testCanBeSerialized()
 {
     $connection1 = $this->getMockConnection('tcp://host1?alias=first');
     $connection2 = $this->getMockConnection('tcp://host2?alias=second');
     $cluster = new PredisCluster();
     $cluster->add($connection1);
     $cluster->add($connection2);
     // We use the following line to initialize the underlying hashring.
     $cluster->getConnectionByKey('foo');
     $unserialized = unserialize(serialize($cluster));
     $this->assertEquals($cluster, $unserialized);
 }