我正在尝试从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);
});
});
});
}
});
};
最佳答案
我通过在process.nextTick()
函数的回调中插入另一个scanRedis()
来解决此问题,它对我有用。
redisMulti.exec(function(deleteErr, deleteSuccess){
process.nextTick(function(){
scanRedis(key, function(err, reply){
process.nextTick(function(){
callback(false, true);
});
});
});
});
关于node.js - RangeError : Maximum call stack size exceeded - nodejs, Redis,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36907943/