From 010a2acf3369382db8ccb3b3e67da98c4af9a1ce Mon Sep 17 00:00:00 2001 From: Jeremiah Lee Cohick Date: Fri, 10 Jul 2015 16:10:00 -0700 Subject: [PATCH 1/3] Added example of a Redis SCAN command --- examples/scan.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 examples/scan.js diff --git a/examples/scan.js b/examples/scan.js new file mode 100644 index 0000000..36f6872 --- /dev/null +++ b/examples/scan.js @@ -0,0 +1,34 @@ +var redis = require("redis"), + 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]; + + // From : + // 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'); + } else { + // Remember, more keys than COUNT or no keys may be returned + // See http://redis.io/commands/scan#the-count-option + if (res[1].length > 0) { + return console.log('Array of matching keys', res[1]); + } else { + // No keys were returned in this scan, but more keys exist. + return scan(); + } + } + } + ); +} From 6f28c6acf5ae626b53fa93441ad8eb89360938ba Mon Sep 17 00:00:00 2001 From: Jeremiah Lee Cohick Date: Fri, 10 Jul 2015 16:25:52 -0700 Subject: [PATCH 2/3] Cursor should be a string --- examples/scan.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/scan.js b/examples/scan.js index 36f6872..35238b6 100644 --- a/examples/scan.js +++ b/examples/scan.js @@ -1,7 +1,7 @@ var redis = require("redis"), client = redis.createClient(); -var cursor = 0; +var cursor = '0'; function scan() { client.scan( @@ -17,7 +17,7 @@ function scan() { // From : // An iteration starts when the cursor is set to 0, // and terminates when the cursor returned by the server is 0. - if (cursor === 0) { + if (cursor === '0') { return console.log('Iteration complete'); } else { // Remember, more keys than COUNT or no keys may be returned From ff020d37f34ab503e9cfeac7490c6773eb3f93ee Mon Sep 17 00:00:00 2001 From: Jeremiah Lee Cohick Date: Fri, 10 Jul 2015 18:04:38 -0700 Subject: [PATCH 3/3] Clarified COUNT's behavior. scan() now will run until cursor reaches 0. --- examples/scan.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/examples/scan.js b/examples/scan.js index 35238b6..0302843 100644 --- a/examples/scan.js +++ b/examples/scan.js @@ -15,19 +15,21 @@ function scan() { cursor = res[0]; // From : - // An iteration starts when the cursor is set to 0, - // and terminates when the cursor returned by the server is 0. + // "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'); } else { - // Remember, more keys than COUNT or no keys may be returned + // 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 + if (res[1].length > 0) { - return console.log('Array of matching keys', res[1]); - } else { - // No keys were returned in this scan, but more keys exist. - return scan(); + console.log('Array of matching keys', res[1]); } + + return scan(); } } );