Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
parse and filter redis clients
querystring = require('querystring')
crypto = require('crypto')
parseRedisClients = ( raw, filter={} )->
clients = []
for line in raw.split( "\n" ) when line.length > 1
cl = querystring.parse( line, " ", "=" )
if Object.keys( filter ).length
_match = true
for _k, _v of filter when cl[ _k ] isnt _v
_match = false
clients.push cl if _match
else
clients.push cl
return clients
getSelfRedisClient = ( redis, cb )->
rCliName = "client-" + crypto.randomBytes( 5 ).toString( "hex" )
redis.client "SETNAME", rCliName, ( err, info )->
if err
cb( err )
return
redis.client "list", ( err, clients )->
if err
cb( err )
return
[ selfCli ] = parseRedisClients( clients, { name: rCliName } )
if not selfCli?
cb( new Error( "client-not-found" ) )
return
cb( null, selfCli )
return
return
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment