@@ -15,20 +15,33 @@ function scan() {
15
15
16
16
// Update the cursor position for the next scan
17
17
cursor = res [ 0 ] ;
18
+ // get the SCAN result for this iteration
19
+ var keys = res [ 1 ] ;
18
20
19
- // From <http://redis.io/commands/scan>:
20
- // 'An iteration starts when the cursor is set to 0,
21
- // and terminates when the cursor returned by the server is 0.'
22
- if ( cursor === '0' ) {
23
- return console . log ( 'Iteration complete' ) ;
24
- }
25
21
// Remember: more or less than COUNT or no keys may be returned
26
22
// See http://redis.io/commands/scan#the-count-option
27
23
// Also, SCAN may return the same key multiple times
28
24
// See http://redis.io/commands/scan#scan-guarantees
25
+ // Additionally, you should always have the code that uses the keys
26
+ // before the code checking the cursor.
27
+ if ( keys . length > 0 ) {
28
+ console . log ( 'Array of matching keys' , keys ) ;
29
+ }
29
30
30
- if ( res [ 1 ] . length > 0 ) {
31
- console . log ( 'Array of matching keys' , res [ 1 ] ) ;
31
+ // It's important to note that the cursor and returned keys
32
+ // vary independently. The scan is never complete until redis
33
+ // returns a non-zero cursor. However, with MATCH and large
34
+ // collections, most iterations will return an empty keys array.
35
+
36
+ // Still, a cursor of zero DOES NOT mean that there are no keys.
37
+ // A zero cursor just means that the SCAN is complete, but there
38
+ // might be one last batch of results to process.
39
+
40
+ // From <http://redis.io/commands/scan>:
41
+ // 'An iteration starts when the cursor is set to 0,
42
+ // and terminates when the cursor returned by the server is 0.'
43
+ if ( cursor === '0' ) {
44
+ return console . log ( 'Iteration complete' ) ;
32
45
}
33
46
34
47
return scan ( ) ;
0 commit comments