本文整理汇总了PHP中Predis\Client::getConnection方法的典型用法代码示例。如果您正苦于以下问题:PHP Client::getConnection方法的具体用法?PHP Client::getConnection怎么用?PHP Client::getConnection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Predis\Client
的用法示例。
在下文中一共展示了Client::getConnection方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getRedisClient
/**
* Lazy connection
*
* Get the connection to Redis server and return it
*
* @throws \Predis\Connection\ConnectionException
*/
public function getRedisClient()
{
if (empty($this->redisHandler)) {
$this->redisHandler = new RedisHandler();
}
return $this->redisHandler->getConnection();
}
示例2: flushNamespacedKeys
public function flushNamespacedKeys()
{
$params = $this->redis->getConnection()->getParameters();
// redis cli connection string
$conn = sprintf("redis-cli -h %s -p %s -n %s --raw", $params->host, $params->port, $params->database);
// remove namespaced keys only
$proc = new Process(sprintf("%s KEYS '*%s*' | xargs --delim='\\n' %s DEL", $conn, $this->ns, $conn));
$proc->run();
return $proc->getOutput();
}
示例3: getParametersAndOptions
protected function getParametersAndOptions(Client $client)
{
$parameters = $client->getConnection()->getParameters();
$options = $client->getOptions();
return array($parameters, $options);
}
示例4: scan
/**
* Scan key-value indices and return the value of all matching keys
*
* @param string $key
*
* @return string[]
*/
public function scan($key)
{
$cursor = 0;
$results = [];
$connection = $this->client->getConnection();
do {
if ($connection instanceof ReplicationInterface) {
/** @var NodeConnectionInterface[] $slaves */
$slaves = $connection->getSlaves();
if (count($slaves) == 1) {
$slave = $slaves[0];
} elseif ($slaves) {
$slave = $slaves[rand(0, count($slaves) - 1)];
} else {
$slave = $connection->getMaster();
}
$cmd = new KeyScan();
$cmd->setArguments([$cursor, 'MATCH', $key]);
$set = $slave->executeCommand($cmd);
} elseif ($connection instanceof PredisCluster) {
$iterator = $connection->getIterator();
/** @var NodeConnectionInterface $node */
$node = $iterator->current();
$cmd = new KeyScan();
$cmd->setArguments([$cursor, 'MATCH', $key]);
$set = $node->executeCommand($cmd);
} else {
$set = $this->clientCmd('scan', [$cursor, ['MATCH' => $key]]);
}
$cursor = $set[0];
$results = array_merge($results, $set[1]);
} while ($cursor != 0);
return $results;
}
示例5: checkCapabilities
/**
* Checks if the passed client instance satisfies the required conditions
* needed to initialize a Publish / Subscribe context.
*
* @param Client Client instance used by the context.
*/
private function checkCapabilities(Client $client)
{
if (Helpers::isCluster($client->getConnection())) {
throw new NotSupportedException('Cannot initialize a PUB/SUB context over a cluster of connections');
}
$commands = array('publish', 'subscribe', 'unsubscribe', 'psubscribe', 'punsubscribe');
if ($client->getProfile()->supportsCommands($commands) === false) {
throw new NotSupportedException('The current profile does not support PUB/SUB related commands');
}
}
示例6: listenToPubSub
/**
* Start listening to subscribed channels of the Redis PubSub mechanism.
* Add a callback to a particular subscription channel.
*
* @param callable $callback
* @return void
*/
public function listenToPubSub(callable $callback)
{
while (1) {
$command = RawCommand::create('PSUBSCRIBE', sprintf('%s-%s', $this->pubsub_channel_prefix, '*'));
$this->client->executeCommand($command);
if ($this->client->getConnection() instanceof MasterSlaveReplication) {
$payload = $this->client->getConnection()->getConnection($command)->read();
} else {
$payload = $this->client->getConnection()->read();
}
$channel = ltrim($payload[2], sprintf('%s%s', $this->pubsub_channel_prefix, '-'));
$message = base64_decode($payload[3]);
call_user_func($callback, ['channel' => $channel, 'message' => $message]);
}
}
示例7: createExecutor
/**
* Returns a pipeline executor depending on the kind of the underlying
* connection and the passed options.
*
* @param Client Client instance used by the context.
* @param array Options for the context initialization.
* @return IPipelineExecutor
*/
protected function createExecutor(Client $client, array $options)
{
if (!$options) {
return new StandardExecutor();
}
if (isset($options['executor'])) {
$executor = $options['executor'];
if (!$executor instanceof IPipelineExecutor) {
throw new \InvalidArgumentException('The executor option accepts only instances ' . 'of Predis\\Pipeline\\IPipelineExecutor');
}
return $executor;
}
if (isset($options['safe']) && $options['safe'] == true) {
$isCluster = Helpers::isCluster($client->getConnection());
return $isCluster ? new SafeClusterExecutor() : new SafeExecutor();
}
return new StandardExecutor();
}
示例8: checkCapabilities
/**
* Checks if the passed client instance satisfies the required conditions
* needed to initialize a transaction context.
*
* @param Client Client instance used by the context.
*/
private function checkCapabilities(Client $client)
{
if (Helpers::isCluster($client->getConnection())) {
throw new NotSupportedException('Cannot initialize a MULTI/EXEC context over a cluster of connections');
}
$profile = $client->getProfile();
if ($profile->supportsCommands(array('multi', 'exec', 'discard')) === false) {
throw new NotSupportedException('The current profile does not support MULTI, EXEC and DISCARD');
}
$this->canWatch = $profile->supportsCommands(array('watch', 'unwatch'));
}
示例9: testCreateClientWithConnectionFromAggregatedConnection
/**
* @group disconnected
*/
public function testCreateClientWithConnectionFromAggregatedConnection()
{
$client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'), array('prefix' => 'pfx:'));
$this->assertInstanceOf('Predis\\Connection\\ClusterConnectionInterface', $cluster = $client->getConnection());
$this->assertInstanceOf('Predis\\Connection\\SingleConnectionInterface', $node01 = $client->getConnectionById('node01'));
$this->assertInstanceOf('Predis\\Connection\\SingleConnectionInterface', $node02 = $client->getConnectionById('node02'));
$clientNode02 = $client->getClientFor('node02');
$this->assertInstanceOf('Predis\\Client', $clientNode02);
$this->assertSame($node02, $clientNode02->getConnection());
$this->assertSame($client->getOptions(), $clientNode02->getOptions());
}
示例10: testGetConnectionWithAliasWorksOnlyWithCluster
/**
* @group disconnected
* @expectedException Predis\NotSupportedException
* @expectedExceptionMessage Retrieving connections by alias is supported only with aggregated connections
*/
public function testGetConnectionWithAliasWorksOnlyWithCluster()
{
$client = new Client();
$client->getConnection('node01');
}
示例11: checkCapabilities
/**
* Checks if the passed client instance satisfies the required conditions
* needed to initialize a monitor context.
*
* @param Client Client instance used by the context.
*/
private function checkCapabilities(Client $client)
{
if (Helpers::isCluster($client->getConnection())) {
throw new ClientException('Cannot initialize a monitor context over a cluster of connections');
}
if ($client->getProfile()->supportsCommand('monitor') === false) {
throw new ClientException('The current profile does not support the MONITOR command');
}
}