Author: Daniele Alessandri (suppakilla@gmail.com)
Inheritance: extends Predis\Command\AbstractCommand
Beispiel #1
0
 /**
  * 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;
 }