/** * 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(); }
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(); }
protected function getParametersAndOptions(Client $client) { $parameters = $client->getConnection()->getParameters(); $options = $client->getOptions(); return array($parameters, $options); }
/** * 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; }
/** * 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'); } }
/** * 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]); } }
/** * 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(); }
/** * 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')); }
/** * @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()); }
/** * @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'); }
/** * 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'); } }