redis DB 내의 데이터를 검색하기 위해 node.js 코드를 사용했지만 실제로는 행복하지 않았으므로 개선하고 싶습니다 ...
기본적으로, 내 redis db에 "사람"이라는 기록이 있습니다. "people :"에서 "person : i"(i는 정수) 목록을 얻고 "person : i"마다 "person : i"키를 사용하여 해시를 검색하는 추가 쿼리를 만듭니다.node.js에서 여러 개의 redis 쿼리를 수행하는 더 깨끗한 방법
db.smembers("people", function(err1, people){
var jsonObj;
var jsonArr = [];
if(!err1) {
var i = 0;
res.writeHead(200, {'content-type': 'application/json'});
// people will provide a list like [person:1, person:2, ..., person:n]
people.forEach(function(person){
// In redis I get the hash with key "person:i"
db.hgetall(person, function(err2,obj){
if(!err2){
// Add person into array
jsonArr.push({ "id" : person.substring(7), "lastname" : obj["lastname"], "firstname" : obj["firstname"]});
// I'm not happy with this part where I check if I reached the last item of people array....
i = i + 1;
if(i == people.length){
res.write(JSON.stringify(jsonArr));
res.end();
}
} else {
var jsonObj = { "error" : "database error", "message" : "Cannot get hash " + person};
res.write(JSON.stringify(jsonObj));
res.end();
}
});
});
} else {
jsonObj = { "error" : "database error", "message" : err1.message };
res.writeHead(200, {'content-type': 'application/json'});
res.write(JSON.stringify(jsonObj));
res.end();
}
});
는 무엇을 할 수있는 깨끗한 (적어도 청소기) 방법이 될 것이다 :
이 내가 그것을 어떻게?
제안 해 주셔서 감사합니다. 것은 지금 스키마를 쉽게 수정할 수 없다는 것입니다. node.js의 흐름 제어에서 모범 사례를 찾고 있었는데, 클리너 코드가 필요한 곳이 여러 곳 있기 때문입니다. 도움을 주셔서 감사합니다 (그리고 제가 설명했던 해결 방법을 확실히 볼 것입니다). – Luc
답변 해 주셔서 감사합니다! 그것은 내가 나를 위해 최선의 방법을 찾아 낼 수있게 해줬 다.'마지막 메시지 10 개를 얻기 위해'sort messages desc limit 0 10 get message : * -> text'. –