Skip to content
Browse files

added support for 'stats' operations

  • Loading branch information...
1 parent 973c883 commit 53e5a7c34a286dd9126d385d19da3032d3ec8336 @iamcal iamcal committed Aug 27, 2010
Showing with 94 additions and 4 deletions.
  1. +7 −0 README.md
  2. +48 −2 lib/memcache.js
  3. +39 −2 test/test-memcache.js
View
7 README.md
@@ -84,6 +84,13 @@ After connecting, you can start to make requests.
client.increment('key', value, callback);
client.decrement('key', value, callback);
+ // statistics. the success argument to the callback
+ // is a key=>value object
+ client.stats(callback);
+ client.stats('settings', callback);
+ client.stats('items', callback);
+ client.stats('mongeese', callback);
+
Once you're done, close the connection.
client.close();
View
50 lib/memcache.js
@@ -168,13 +168,27 @@ Client.prototype.decrement = function(key, value, callback) {
return this.query('decr ' + key + ' ' + value, 'simple', callback);
};
+Client.prototype.stats = function(type, callback){
+
+ if (typeof(type) == 'function'){
+ callback = type;
+ type = null;
+ }
+
+ if (type){
+ return this.query('stats '+type, 'stats', callback);
+ }else{
+ return this.query('stats', 'stats', callback);
+ }
+}
+
Client.prototype.handle_received_data = function(){
- while (this.buffer.length > 0) {
+ while (this.buffer.length > 0){
var result = this.determine_reply_handler(this.buffer);
- if (result == null) {
+ if (result == null){
break;
}
@@ -239,6 +253,38 @@ Client.prototype.handle_get = function(buffer) {
}
};
+Client.prototype.handle_stats = function(buffer){
+
+ // special case - no stats at all
+ if (buffer.indexOf('END') == 0){
+ return [{}, 5];
+ }
+
+ // find the terminator
+ var idx = buffer.indexOf('\r\nEND\r\n');
+ if (idx == -1){
+ // wait for more data if we don't have an end yet
+ return null;
+ }
+
+ // read the lines
+ var our_data = buffer.substr(0, idx+2);
+ var out = {};
+ var line = null;
+ var i=0;
+ while (line = readLine(our_data)){
+ our_data = our_data.substr(line.length + 2);
+ if (line.substr(0, 5) == 'STAT '){
+ var idx2 = line.indexOf(' ', 5);
+ var k = line.substr(5, idx2-5);
+ var v = line.substr(idx2+1);
+ out[k] = v;
+ }
+ }
+
+ return [out, idx + 7, null];
+};
+
Client.prototype.handle_simple = function(buffer){
var line = readLine(buffer);
return [line, (line.length + crlf_len), null];
View
41 test/test-memcache.js
@@ -170,13 +170,50 @@ mc.addHandler(function() {
};
exports['version'] = function(assert, beforeExit){
+ var n = 0;
mc.version(function(success, error){
-
+ n++;
assert.equal(error, null);
assert.length(success, 5);
});
- }
+ beforeExit(function(){
+ assert.equal(1, n);
+ });
+ };
+
+ exports['stats'] = function(assert, beforeExit){
+ var n = 0;
+
+ mc.stats(function(success, error){
+ n++;
+ assert.ok(success.pid, "server has a pid");
+ });
+
+ mc.stats('settings', function(success, error){
+ n++;
+ assert.ok(success.maxconns);
+ });
+
+ mc.stats('items', function(success, error){ n++; assert.ok(num_keys(success)); });
+ mc.stats('sizes', function(success, error){ n++; assert.ok(num_keys(success)); });
+ mc.stats('slabs', function(success, error){ n++; assert.ok(num_keys(success)); });
+
+ mc.stats('notreal', function(success, error){
+ n++;
+ assert.equal(error, 'ERROR');
+ });
+
+ beforeExit(function(){
+ assert.equal(6, n);
+ });
+ };
});
+
+function num_keys(a){
+ var i=0;
+ for (var k in a) i++;
+ return i;
+}

0 comments on commit 53e5a7c

Please sign in to comment.
Something went wrong with that request. Please try again.