Skip to content

Commit 93db3b1

Browse files
Merge pull request #775 from jeremiahlee/master
Added example of a Redis SCAN command
2 parents 131f92b + ff020d3 commit 93db3b1

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

examples/scan.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
var redis = require("redis"),
2+
client = redis.createClient();
3+
4+
var cursor = '0';
5+
6+
function scan() {
7+
client.scan(
8+
cursor,
9+
"MATCH", "q:job:*",
10+
"COUNT", "10",
11+
function(err, res) {
12+
if (err) throw err;
13+
14+
// Update the cursor position for the next scan
15+
cursor = res[0];
16+
17+
// From <http://redis.io/commands/scan>:
18+
// "An iteration starts when the cursor is set to 0,
19+
// and terminates when the cursor returned by the server is 0."
20+
if (cursor === '0') {
21+
return console.log('Iteration complete');
22+
} else {
23+
// Remember: more or less than COUNT or no keys may be returned
24+
// See http://redis.io/commands/scan#the-count-option
25+
// Also, SCAN may return the same key multiple times
26+
// See http://redis.io/commands/scan#scan-guarantees
27+
28+
if (res[1].length > 0) {
29+
console.log('Array of matching keys', res[1]);
30+
}
31+
32+
return scan();
33+
}
34+
}
35+
);
36+
}

0 commit comments

Comments
 (0)