Skip to content

Commit

Permalink
Support getting cache statistics by /cache/statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Mar 24, 2014
1 parent 39f1ef6 commit 0b604fa
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/response-cache/cache.js
Expand Up @@ -40,10 +40,18 @@ function Cache(options) {
size: options.size
});
this.rules = options.rules;
this.nGets = 0;
this.nHits = 0;
}
Cache.prototype = {
'get': function(key, callback) {
return this.cache.get(key, callback);
return this.cache.get(key, function(error, cachedResponse) {
this.nGets++;
if (cachedResponse) {
this.nHits++;
}
callback(error, cachedResponse);
}.bind(this));
},

'set': function(key, value, ttl, callback) {
Expand All @@ -60,6 +68,20 @@ Cache.prototype = {
return foundRule = rule;
});
return foundRule;
},

getStatistics: function() {
var hitRatio;
if (this.nGets == 0) {
hitRatio = 0.0;
} else {
hitRatio = (this.nHits / this.nGets) * 100;
}
return {
"nGets": this.nGets,
"nHits": this.nHits,
"hitRatio": hitRatio
};
}
};
exports = module.exports = Cache;
6 changes: 6 additions & 0 deletions lib/response-cache/index.js
Expand Up @@ -21,6 +21,12 @@ exports = module.exports = function(options) {
var cache = new Cache(options);

return function(request, response, next) {
if (request.url == '/cache/statistics' && request.method == 'GET') {
response.jsonp(200, cache.getStatistics());
response.end();
return;
}

var rule = cache.getRule(request);
if (!rule) {
next();
Expand Down
47 changes: 47 additions & 0 deletions test/response-cache/cache.test.js
Expand Up @@ -85,5 +85,52 @@ suite('Response Cache', function() {
assert.deepEqual(rule.regex, primaryRegex);
});
});

suite('statistics', function() {
var cache;
setup(function() {
cache = new Cache({
rules: [
{ regex: /.*/ }
]
});
});

test('nGets', function() {
assert.equal(cache.getStatistics().nGets, 0);
cache.get('key', function(error, cachedResponse) {
});
assert.equal(cache.getStatistics().nGets, 1);
});

test('nHits', function() {
cache.set('key', 'value');
assert.equal(cache.getStatistics().nHits, 0);
cache.get('key', function(error, cachedResponse) {
});
assert.equal(cache.getStatistics().nHits, 1);
});

suite('hitRatio', function() {
test('0 gets', function() {
assert.equal(cache.getStatistics().hitRatio, 0.0);
});

test('0 hits', function() {
cache.get('key', function(error, cachedResponse) {
});
assert.equal(cache.getStatistics().hitRatio, 0.0);
});

test('1/2 hits', function() {
cache.get('key', function(error, cachedResponse) {
});
cache.set('key', 'value');
cache.get('key', function(error, cachedResponse) {
});
assert.equal(cache.getStatistics().hitRatio, 50.0);
});
});
});
});

24 changes: 24 additions & 0 deletions test/response-cache/middleware.test.js
Expand Up @@ -336,5 +336,29 @@ suite('Response Cache Middleware', function() {
});
});
});

suite('statistics', function() {
test('json', function(done) {
client(application)
.get('/cache/statistics')
.expect(200)
.end(function(error, response) {
if (error)
return done(error);

var statistics = {
nGets: 0,
nHits: 0,
hitRatio: 0.0
};
try {
assert.deepEqual(response.body, statistics);
} catch (error) {
return done(error);
}
done();
});
});
});
});

0 comments on commit 0b604fa

Please sign in to comment.