2016-04-28 108 views
2

我試圖從redis中刪除大量的密鑰(〜20M),並且出現錯誤RangeError:由於過度遞歸導致最大調用堆棧大小超過調用。 我試圖在遞歸調用中使用process.nextTick(),但仍然收到相同的錯誤。RangeError:超過最大調用堆棧大小 - nodejs,redis

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); 
       }); 
      }); 
     }); 
    } 
}); 
}; 

回答

1

我解決了這個由scanRedis()功能的回調插入另一process.nextTick(),它爲我工作。

redisMulti.exec(function(deleteErr, deleteSuccess){ 
    process.nextTick(function(){ 
    scanRedis(key, function(err, reply){ 
     process.nextTick(function(){ 
     callback(false, true); 
     }); 
    }); 
    }); 
}); 
相關問題