Skip to content

Commit

Permalink
Better ducmentation for SCAN
Browse files Browse the repository at this point in the history
Fixes #1151
  • Loading branch information
michael-grunder committed Apr 10, 2017
1 parent 520e06a commit f0c25a2
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -1013,13 +1013,14 @@ _**Description**_: Scan the keyspace for keys
##### *Example*
~~~
$it = NULL; /* Initialize our iterator to NULL */
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY); /* retry when we get no keys back */
while($arr_keys = $redis->scan($it)) {
do {
// Use global option $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY); to ignore empty results
$arr_keys = $redis->scan($it);
foreach($arr_keys as $str_key) {
echo "Here is a key: $str_key\n";
}
echo "No more keys to scan!\n";
}
} while ($it > 0);
echo "No more keys to scan!\n";
~~~

### object
Expand Down

4 comments on commit f0c25a2

@yatsukhnenko
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scan may returns false and then you pass it to foreach :trollface:

@michael-grunder
Copy link
Member Author

@michael-grunder michael-grunder commented on f0c25a2 Apr 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really shouldn't merge things before I have had coffee. FFS I'm the one who wrote the SCAN implementation too. I have no excuse 😃

Edit: Although, without a MATCH argument I don't think Redis ever returns empty results. It's only when you MATCH that this can happen.

@ambis
Copy link

@ambis ambis commented on f0c25a2 Apr 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm glad @yatsukhnenko commented on that. I took that into account in my project but forgot to add it to the issue.

Shortest: (Not 100% sure this works, and not very pretty)
$arr_keys = $redis->scan($it) || continue;
Yeah this doesn't work. "Expected: expression"

Bestest:

if (!$arr_keys) {
    continue;
}

What people will end up writing:

if ($arr_keys) {
    foreach ...
}

@michael-grunder
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically I just set the Redis::SCAN_RETRY option so I never have to deal with it at all.

Now it's documented both ways so I think we've got our bases covered. 😀

Please sign in to comment.