Skip to content

Instantly share code, notes, and snippets.

@jrgm
Last active August 29, 2015 14:06
Show Gist options
  • Save jrgm/8d82041470849f1c0c97 to your computer and use it in GitHub Desktop.
Save jrgm/8d82041470849f1c0c97 to your computer and use it in GitHub Desktop.
memcached-dump.js
var P = require('bluebird')
var util = require('util')
var memcached = require('memcached')
var underscore = require('underscore')
function getItems(mc) {
var dfd = P.defer()
mc.items(function(err, result) {
if (err) return dfd.reject(err)
dfd.resolve(result)
})
return dfd.promise
}
function cacheDump(mc, server, slabid, number) {
var dfd = P.defer()
mc.cachedump(server, slabid, number, function(err, result) {
if (err) return dfd.reject(err)
dfd.resolve(underscore.clone(result))
})
return dfd.promise
}
function getValue(mc, key) {
var dfd = P.defer()
mc.get(key, function(err, result) {
if (err) return dfd.reject(err)
dfd.resolve([key, result])
})
return dfd.promise
}
function fetchAll(mc, options) {
return getItems(mc).then(function(items) {
var dumps = []
items.forEach(function(itemSet) {
var server = itemSet.server
delete itemSet.server
Object.keys(itemSet).forEach(function(stats) {
var slabid = parseInt(stats)
var count = itemSet[stats].number
dumps.push(cacheDump(mc, server, slabid, count))
})
})
return P.all(dumps)
.then(function(items) {
var data = {}
underscore.flatten(items)
.forEach(function(item) {
data[item.key] = {
expiry: new Date(item.s*1000).toISOString(),
bytes: item.b
}
})
return data
})
})
}
function run(options) {
var mc = new memcached(options.memcache)
//mc.debug = true
return fetchAll(mc, options)
.then(function(args) {
var keys = Object.keys(args)
keys.forEach(function(key) {
var ttl = new Date(args[key].expiry).getTime() - Date.now()
if (ttl < 0) return
//console.log(ttl)
getValue(mc, key).then(function(blob) {
console.log(args[key].expiry, key, '\t', blob[1])
})
})
})
}
// call with arg of 'hostname:portnum'
module.exports = run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment