You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
Update SCAN example code
This commit is contained in:
@@ -15,6 +15,27 @@ function scan() {
|
|||||||
|
|
||||||
// Update the cursor position for the next scan
|
// Update the cursor position for the next scan
|
||||||
cursor = res[0];
|
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>:
|
// From <http://redis.io/commands/scan>:
|
||||||
// 'An iteration starts when the cursor is set to 0,
|
// 'An iteration starts when the cursor is set to 0,
|
||||||
@@ -22,14 +43,6 @@ function scan() {
|
|||||||
if (cursor === '0') {
|
if (cursor === '0') {
|
||||||
return console.log('Iteration complete');
|
return console.log('Iteration complete');
|
||||||
}
|
}
|
||||||
// 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) {
|
|
||||||
console.log('Array of matching keys', res[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return scan();
|
return scan();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user