Skip to content

Instantly share code, notes, and snippets.

@timhudson
Created March 3, 2014 20:57
Show Gist options
  • Select an option

  • Save timhudson/9334476 to your computer and use it in GitHub Desktop.

Select an option

Save timhudson/9334476 to your computer and use it in GitHub Desktop.
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