/** * The scan command allows for efficient, non-blocking iteration of the entire keyspace. It works by using a cursor * which is repeatedly passed to the command and updated by the Redis instance. * * See information at the official redis website: * * @link http://redis.io/commands/scan * * @param int $iterator a reference to the cursor, this variable will be updated automatically with the new cursor value once * the method returns. when no more keys are available to the cursor it is reset to 0. this should be used as the indication * to end an iteration. * @param string $pattern an optional pattern which the returned keys have to match, defaults to no pattern at all * @param int $count the optional maximal number of keys to return, defaults to 10 * @return string[] returns an array of strings (which may be empty if a pattern is used) * @example * <pre> * $cursor = 0; * while ($cursor != 0) { * $result = $redis->scan($cursor) * print_r($result); * } */ public function scan(&$iterator, $pattern = '', $count = 10) { try { return $this->client->scan($iterator, $pattern, $count); } catch (Exception $e) { // func_get_args() apparently resolves references in arguments. we however need to make sure to preserve it. $args = func_get_args(); $args[0] =& $iterator; return $this->handleException($e, __FUNCTION__, $args); } }
/** * Test the scan family of commands */ public function testScan() { $this->redis->flushdb(); $keys = ['testScan', 'testScanHello', 'testScanHallo', 'testScanHaallo', 'testScanHoula', 'testScan2', 'testScanHello2', 'testScanHallo2', 'testScanHaallo2', 'testScanHoula2', 'testScan3', 'testScanHello3', 'testScanHallo3', 'testScanHaallo3', 'testScanHoula3']; foreach ($keys as $key) { $this->redis->set($key, 'someValue'); } // a bit tricky to test, since we can't determine the outcome of a single iteration (or can we?) // so we just iterate until the iterator is exhausted and then compare the final results $iterator = 0; // we unroll the iteration here, to have better control over the involved values $result = $this->redis->scan($iterator); $this->assertNotEmpty($result); //$this->assertSame(10, count($result)); $this->assertNotSame(0, $iterator); $result = array_merge($result, $this->redis->scan($iterator)); foreach ($keys as $key) { $this->assertContains($key, $result); } $this->assertSame(15, count($result)); $this->assertSame(0, $iterator); $result1 = $this->redis->scan($iterator, 'testScanH?llo', 20); $this->assertContains('testScanHello', $result1); $this->assertContains('testScanHallo', $result1); $this->assertEquals(2, count($result1)); $this->assertSame(0, $iterator); $result2 = $this->redis->scan($iterator, 'testScanH*llo', 20); $this->assertContains('testScanHello', $result2); $this->assertContains('testScanHallo', $result2); $this->assertContains('testScanHaallo', $result2); $this->assertEquals(3, count($result2)); $this->assertSame(0, $iterator); $result3 = $this->redis->scan($iterator, 'testScanH[ae]llo', 20); $this->assertContains('testScanHello', $result3); $this->assertContains('testScanHallo', $result3); $this->assertEquals(2, count($result3)); $this->assertSame(0, $iterator); $result4 = $this->redis->scan($iterator, '*', 20); $this->assertEquals(15, count($result4)); $this->assertSame(0, $iterator); }