Created
September 5, 2014 17:24
-
-
Save grimen/a0a1bc8336f5740fa698 to your computer and use it in GitHub Desktop.
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
Memcached.prototype.keys = function (keys, done) { | |
var self = this; | |
// https://github.com/3rd-Eden/node-memcached/blob/master/examples/simple_cachedump.js | |
var findMatchingKeys = function (search_key, done) { | |
var glob = require('minimatch'); | |
var matching_keys = [] | |
var index_of_key = search_key.replace('*', ''); | |
self.client.items(function (err, items) { | |
if (err) return done(err); | |
if (!items.length) { | |
return done(null, matching_keys) | |
} | |
items.forEach(function (item, i) { | |
var slab_ids = Object.keys(item); | |
slab_ids.pop(); // remove "server" key | |
if (!slab_ids.length) { | |
return done(null, matching_keys) | |
} | |
slab_ids.forEach(function(slab_id, j) { | |
slab_id = parseInt(slab_id); | |
self.client.cachedump(item.server, slab_id, item[slab_id].number, function(err, dumps) { | |
if (err) return done(err); | |
dumps = dumps || []; | |
dumps.each(function (dump) { | |
// console.log("\nKEY: ", dump.key, ', GLOB:', search_key, ' => ', glob(dump.key, search_key)) | |
var is_matching = !!~dump.key.indexOf(index_of_key) | |
if (is_matching) { | |
matching_keys.push(dump.key); | |
} | |
}); | |
if (slab_id == slab_ids.last()) { | |
return done(null, matching_keys) | |
} | |
}); | |
}); | |
}); | |
}); | |
}; | |
var info = self.keyInfo(keys); | |
var operation = function (keys) { | |
done(keys, info.is_wildcard); | |
} | |
if (info.is_wildcard) { | |
var search_key = [self.resource(info.wildcard_key).trimmed.db, info.wildcard_key].join('/'); | |
findMatchingKeys(search_key, function (err, matching_keys) { | |
matching_keys = matching_keys.sort(); | |
operation(matching_keys); | |
}); | |
} else { | |
keys = keys.map(function(k) { | |
return [self.resource(k).trimmed.db, k].join('/'); | |
}); | |
operation(keys); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment