我试图从redis删除大量密钥(~20M),并且由于过多的递归调用而导致错误 RangeError:超出最大调用堆栈大小。 我尝试在递归调用中使用 process.nextTick(),但仍然遇到相同的错误。
count = "100";
cursor = "0";
function scanRedis(key, callback){
redisClient.scan(cursor, "MATCH", key, "COUNT", count, function(err, reply){
if(err){
throw err;
}
cursor = reply[0];
if(cursor === "0" && reply[1].length === 0){
return callback(false, true);
}else if(cursor === "0" && reply[1].length > 0){
redisClient.del(reply[1], function(deleteErr, deleteSuccess){
return callback(false, true);
});
}else{
if(reply[1].length > 0){
delCount += reply[1].length;
//console.log(reply[1]);
redisMulti.del(reply[1]);
}
redisMulti.exec(function(deleteErr, deleteSuccess){
process.nextTick(function(){
scanRedis(key, function(err, reply){ //getting an error here
callback(false, true);
});
});
});
}
});
};
答案 0 :(得分:2)
我通过在function
函数的回调中插入另一个process.nextTick()
来解决这个问题,它对我有用。
scanRedis()