Skip to content

Commit

Permalink
[6.x] Fix *scan methods for phpredis (#32336)
Browse files Browse the repository at this point in the history
* [6.x] Fix *scan methods for phpredis (#24222)

* style(whitespace): add whitespace after operator
  • Loading branch information
shufo authored Apr 13, 2020
1 parent 0eb3787 commit fbb9b17
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/Illuminate/Redis/Connections/PhpRedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,77 @@ public function zunionstore($output, $keys, $options = [])
]);
}

/**
* Scans the all keys based on options.
*
* @param mixed $cursor
* @param array $options
* @return mixed
*/
public function scan($cursor, $options = [])
{
$result = $this->client->scan($cursor,
$options['match'] ?? '*',
$options['count'] ?? 10
);

return empty($result) ? $result : [$cursor, $result];
}

/**
* Scans the given set for all values based on options.
*
* @param string $key
* @param mixed $cursor
* @param array $options
* @return mixed
*/
public function zscan($key, $cursor, $options = [])
{
$result = $this->client->zscan($key, $cursor,
$options['match'] ?? '*',
$options['count'] ?? 10
);

return $result === false ? [0, []] : [$cursor, $result];
}

/**
* Scans the given set for all values based on options.
*
* @param string $key
* @param mixed $cursor
* @param array $options
* @return mixed
*/
public function hscan($key, $cursor, $options = [])
{
$result = $this->client->hscan($key, $cursor,
$options['match'] ?? '*',
$options['count'] ?? 10
);

return $result === false ? [0, []] : [$cursor, $result];
}

/**
* Scans the given set for all values based on options.
*
* @param string $key
* @param mixed $cursor
* @param array $options
* @return mixed
*/
public function sscan($key, $cursor, $options = [])
{
$result = $this->client->sscan($key, $cursor,
$options['match'] ?? '*',
$options['count'] ?? 10
);

return $result === false ? [0, []] : [$cursor, $result];
}

/**
* Execute commands in a pipeline.
*
Expand Down
113 changes: 113 additions & 0 deletions tests/Redis/RedisConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,119 @@ public function testItScansForKeys()
}
}

public function testItZscansForKeys()
{
foreach ($this->connections() as $redis) {
$members = [100 => 'test:zscan:1', 200 => 'test:zscan:2'];

foreach ($members as $score => $member) {
$redis->zadd('set', $score, $member);
}

$iterator = null;
$result = [];

do {
[$iterator, $returnedMembers] = $redis->zscan('set', $iterator);

if (! is_array($returnedMembers)) {
$returnedMembers = [$returnedMembers];
}

foreach ($returnedMembers as $member => $score) {
$this->assertArrayHasKey((int) $score, $members);
$this->assertContains($member, $members);
}

$result += $returnedMembers;
} while ($iterator > 0);

$this->assertCount(2, $result);

$iterator = null;
[$iterator, $returned] = $redis->zscan('set', $iterator, ['match' => 'test:unmatch:*']);
$this->assertEmpty($returned);

$iterator = null;
[$iterator, $returned] = $redis->zscan('set', $iterator, ['count' => 5]);
$this->assertCount(2, $returned);

$redis->flushAll();
}
}

public function testItHscansForKeys()
{
foreach ($this->connections() as $redis) {
$fields = ['name' => 'mohamed', 'hobby' => 'diving'];

foreach ($fields as $field => $value) {
$redis->hset('hash', $field, $value);
}

$iterator = null;
$result = [];

do {
[$iterator, $returnedFields] = $redis->hscan('hash', $iterator);

foreach ($returnedFields as $field => $value) {
$this->assertArrayHasKey($field, $fields);
$this->assertContains($value, $fields);
}

$result += $returnedFields;
} while ($iterator > 0);

$this->assertCount(2, $result);

$iterator = null;
[$iterator, $returned] = $redis->hscan('hash', $iterator, ['match' => 'test:unmatch:*']);
$this->assertEmpty($returned);

$iterator = null;
[$iterator, $returned] = $redis->hscan('hash', $iterator, ['count' => 5]);
$this->assertCount(2, $returned);

$redis->flushAll();
}
}

public function testItSscansForKeys()
{
foreach ($this->connections() as $redis) {
$members = ['test:sscan:1', 'test:sscan:2'];

foreach ($members as $member) {
$redis->sadd('set', $member);
}

$iterator = null;
$result = [];

do {
[$iterator, $returnedMembers] = $redis->sscan('set', $iterator);

foreach ($returnedMembers as $member) {
$this->assertContains($member, $members);
array_push($result, $member);
}
} while ($iterator > 0);

$this->assertCount(2, $result);

$iterator = null;
[$iterator, $returned] = $redis->sscan('set', $iterator, ['match' => 'test:unmatch:*']);
$this->assertEmpty($returned);

$iterator = null;
[$iterator, $returned] = $redis->sscan('set', $iterator, ['count' => 5]);
$this->assertCount(2, $returned);

$redis->flushAll();
}
}

public function testPhpRedisScanOption()
{
foreach ($this->connections() as $redis) {
Expand Down

0 comments on commit fbb9b17

Please sign in to comment.