Created
March 3, 2014 20:57
-
-
Save timhudson/9334476 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var _ = require('highland'), | |
| redis = require('redis'), | |
| client = redis.createClient(); | |
| var pattern = process.argv[2], | |
| count = process.argv[3] || 10 | |
| client.on('error', function(err) { | |
| throw err | |
| }); | |
| function createScanStream(cursor) { | |
| return _(function(push, next) { | |
| client.scan(cursor, 'MATCH', pattern, 'COUNT', count, function(err, reply) { | |
| if (err) return push(err); | |
| var _cursor = Number(reply.shift()); | |
| if (reply[0].length) reply[0].forEach(function(key) { | |
| push(null, key); | |
| }); | |
| if (_cursor) return next(createScanStream(_cursor)); | |
| push(null, _.nil); | |
| }) | |
| }) | |
| } | |
| createScanStream(0) | |
| .consume(function(err, key, push, next) { | |
| // Close redis client when stream is finished | |
| if (key === _.nil) return client.quit() | |
| push(err, key + '\n') | |
| next() | |
| }) | |
| .pipe(process.stdout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment