Skip to content

Commit 010a2ac

Browse files
author
Jeremiah Lee Cohick
committed
Added example of a Redis SCAN command
1 parent eb03bb9 commit 010a2ac

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

examples/scan.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 keys than COUNT or no keys may be returned
24+
// See http://redis.io/commands/scan#the-count-option
25+
if (res[1].length > 0) {
26+
return console.log('Array of matching keys', res[1]);
27+
} else {
28+
// No keys were returned in this scan, but more keys exist.
29+
return scan();
30+
}
31+
}
32+
}
33+
);
34+
}

0 commit comments

Comments
 (0)