2014-04-28 4 views
1

Redis (데이터베이스 '10')의 모든 데이터를 JSON 변수로 가져 오려고합니다. ...Redis에서 JSON으로 모든 데이터를 가져 오는 방법

redis = require('redis'); 
client = redis.createClient(); 
client.send_command('select', [10], redis.print); 
var r = {}; 
client.keys('*', function (err, keys) { 
    keys.forEach(function(c){  
     client.get(c, function(err, v){  
      r[c] = v;       
     });  
    }); 
}); 
console.log(JSON.stringify(r)); 
client.quit(); 

하지만 내 'R'JSON 변수가 비어 내가 콜백 메소드를 사용하여 할 수있는 방법이나 동기 코드 :하지만 비동기 호출과 혼동 조금 ... 나는이 시도했다 해요 ?

감사합니다.

+1

node.js 부분은 말할 수 없지만 KEYS 대신 SCAN을 사용하는 것이 좋습니다. –

답변

1

비동기 모듈 https://github.com/caolan/async을 사용하여 비동기 모듈을 사용해 보았습니다.

redis = require('redis'); 
client = redis.createClient(); 
client.send_command('select', [10], redis.print); 
var r = {}; 

client.keys('*', function(err, keys) { 
    async.each(keys, function(key, callback) { 
    client.get(key, function(err, value) { 
     r[key] = value; 
     callback(err); 
    }); 
    }, function() { 
    // when callback is finished 
    console.log(JSON.stringify(r)); 
    client.quit(); 
    }); 
}); 

또한 https://github.com/goodeggs/fibrous

동기 코드는하지만 위험 섬유 모듈을 사용하여, 그것은 동기 만들 수 있습니다!

+0

잘 작동하고 키/값 JSON 변수를 가져올 수 있습니다. 감사. – user3582562

관련 문제