@@ -1008,19 +1008,38 @@ _**Description**_: Scan the keyspace for keys
1008
1008
* LONG, Optional* : Count of keys per iteration (only a suggestion to Redis)
1009
1009
1010
1010
##### * Return value*
1011
- * Array, boolean* : This function will return an array of keys or FALSE if there are no more keys
1011
+ * Array, boolean* : This function will return an array of keys or FALSE if Redis returned zero keys
1012
1012
1013
1013
##### * Example*
1014
1014
~~~
1015
- $it = NULL; /* Initialize our iterator to NULL */
1015
+
1016
+ /* Without enabling Redis::SCAN_RETRY (default condition) */
1017
+ $it = NULL;
1016
1018
do {
1017
- // Use global option $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY); to ignore empty results
1019
+ // Scan for some keys
1018
1020
$arr_keys = $redis->scan($it);
1019
- foreach($arr_keys as $str_key) {
1020
- echo "Here is a key: $str_key\n";
1021
+
1022
+ // Redis may return empty results, so protect against that
1023
+ if ($arr_keys !== FALSE) {
1024
+ foreach($arr_keys as $str_key) {
1025
+ echo "Here is a key: $str_key\n";
1026
+ }
1021
1027
}
1022
1028
} while ($it > 0);
1023
1029
echo "No more keys to scan!\n";
1030
+
1031
+ /* With Redis::SCAN_RETRY enabled */
1032
+ $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
1033
+ $it = NULL;
1034
+
1035
+ /* phpredis will retry the SCAN command if empty results are returned from the
1036
+ server, so no empty results check is required. */
1037
+ while ($arr_keys = $redis->scan($it)) {
1038
+ foreach ($arr_keys as $str_key) {
1039
+ echo "Here is a key: $str_key\n";
1040
+ }
1041
+ }
1042
+ echo "No more keys to scan!\n";
1024
1043
~~~
1025
1044
1026
1045
### object
0 commit comments