Created
May 10, 2012 13:27
-
-
Save cliftonc/2652981 to your computer and use it in GitHub Desktop.
Delete lots of redis keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var smartDelete = function(clientName, queryKey, cb) { | |
var client = redis.getClient(clientName), noneRemaining = false, numDeleted = 0, lastK, duration; | |
async.whilst( | |
function () { return !noneRemaining; }, | |
function (callback) { | |
client.spop(queryKey, function(err, key) { | |
if(key) { | |
client.del(key,function(err) { | |
numDeleted ++; | |
if(numDeleted === 1) { | |
lastK = new Date(); | |
} | |
if(numDeleted % 100000 === 0) { | |
duration = new Date() - lastK; | |
lastK = new Date(); | |
logger.silly(clientName + ' deleted ' + numDeleted + ' @ ' + (duration/100) + '/s'); | |
} | |
callback(err); | |
}); | |
} else { | |
noneRemaining = true; | |
callback(); | |
} | |
}) | |
}, | |
function (err) { | |
logger.silly('Deleted from ' + clientName + ': ' + numDeleted); | |
cb(err); | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment