forked from redis/node-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.js
51 lines (42 loc) · 1.78 KB
/
scan.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';
var redis = require('redis');
var client = redis.createClient();
var cursor = '0';
function scan () {
client.scan(
cursor,
'MATCH', 'q:job:*',
'COUNT', '10',
function (err, res) {
if (err) throw err;
// Update the cursor position for the next scan
cursor = res[0];
// get the SCAN result for this iteration
var keys = res[1];
// Remember: more or less than COUNT or no keys may be returned
// See http://redis.io/commands/scan#the-count-option
// Also, SCAN may return the same key multiple times
// See http://redis.io/commands/scan#scan-guarantees
// Additionally, you should always have the code that uses the keys
// before the code checking the cursor.
if (keys.length > 0) {
console.log('Array of matching keys', keys);
}
// It's important to note that the cursor and returned keys
// vary independently. The scan is never complete until redis
// returns a non-zero cursor. However, with MATCH and large
// collections, most iterations will return an empty keys array.
// Still, a cursor of zero DOES NOT mean that there are no keys.
// A zero cursor just means that the SCAN is complete, but there
// might be one last batch of results to process.
// From <http://redis.io/commands/scan>:
// 'An iteration starts when the cursor is set to 0,
// and terminates when the cursor returned by the server is 0.'
if (cursor === '0') {
return console.log('Iteration complete');
}
return scan();
}
);
}
scan();