Permalink
Browse files

Add config options for the stat-output interval and modify config par…

…sing to allow comments in the JSON.
  • Loading branch information...
1 parent ca6367d commit c0ffb3fc4dc49f5df5ff275e64d63363a3de72e6 @dansimau committed May 20, 2011
Showing with 31 additions and 15 deletions.
  1. +22 −14 lib/cache.js
  2. +9 −1 proxy.js
View
@@ -2,6 +2,7 @@ var console = require('console');
var fs = require('fs');
var util = require('util');
var events = require('events');
+var util = require('util');
var proxyUtils = require('./utils');
function MemoryCache(options) {
@@ -19,13 +20,17 @@ function MemoryCache(options) {
this.addListener('memorycleanup', cleanMemory);
- setInterval(function() {
- self._printMemoryUsage();
- }, 300000); // 5 mins
+ if (this.options.stats > 0) {
+ setInterval(function() {
+ self._printMemoryUsage();
+ }, this.options.stats * 1000);
+ }
- setInterval(function() {
- self.emit('memorycleanup', self);
- }, 30000);
+ if (this.options.cleanup > 0) {
+ setInterval(function() {
+ self.emit('memorycleanup', self);
+ }, this.options.cleanup * 1000);
+ }
};
util.inherits(MemoryCache, events.EventEmitter);
@@ -68,9 +73,9 @@ MemoryCache.prototype.del = function(key) {
};
MemoryCache.prototype._printMemoryUsage = function() {
- console.log("\nCache memory stats\n------------------");
- console.log("items: " + self.keyCount + "; size: " + (new Number(self.bufferBytes / 1024 / 1024)).toFixed(2) + " MB; max: " + (new Number(self.options.maxmem / 1024 / 1024)).toFixed(2));
- console.log("\nProcess memory usage\n--------------------");
+ util.log("\nCache memory stats\n------------------");
+ util.log("items: " + self.keyCount + "; size: " + (new Number(self.bufferBytes / 1024 / 1024)).toFixed(2) + " MB; max: " + (new Number(self.options.maxmem / 1024 / 1024)).toFixed(2));
+ util.log("\nProcess memory usage\n--------------------");
proxyUtils.printMemoryUsage();
}
@@ -127,6 +132,7 @@ function Cache(options) {
events.EventEmitter.call(this);
if (!options) options = {};
+ this.options = options;
this.memory = new MemoryCache(options.memory);
this.disk = new DiskCache(options.disk);
@@ -144,9 +150,11 @@ function Cache(options) {
}
};
- setInterval(function() {
- self._printStats();
- }, 300000); //5 mins
+ if (this.options.stats > 0) {
+ setInterval(function() {
+ self._printStats();
+ }, this.options.stats * 1000);
+ }
}
exports.Cache = Cache;
@@ -190,7 +198,7 @@ Cache.prototype.set = function(key, value) {
Cache.prototype._printStats = function() {
- var o = 'Cache stats' + "\n" + '-----------';
+ var o = 'Cache stats' + "\n" + '-----------' + "\n";
o += 'memory hits: ' + this.stats.memory.hits + "\n";
o += 'memory misses: ' + this.stats.memory.misses + "\n";
@@ -202,5 +210,5 @@ Cache.prototype._printStats = function() {
o += 'cache misses: ' + this.stats.misses + "\n";
o += 'cache hit rate: ' + (new Number(this.stats.hits / (this.stats.hits + this.stats.misses))).toFixed(2);
- console.log(o);
+ util.log(o);
}
View
@@ -18,7 +18,15 @@ for (var i = 0, l = process.argv.length; i < l; i++) {
if (typeof(configFile) === 'undefined') configFile = __dirname + '/proxy.conf';
try {
- conf = JSON.parse(fs.readFileSync(configFile, 'utf8'));
+ var lines = fs.readFileSync(configFile, 'utf8').split("\n");
+
+ for (var i=0, l=lines.length; i<l; i++) {
+ if (lines[i].match(/^\s*\/\//)) {
+ delete lines[i];
+ }
+ }
+ lines = lines.join("\n");
+ conf = JSON.parse(lines);
}
catch (err) {
util.log('Unable to read configuration from file (' + configFile + ').');

0 comments on commit c0ffb3f

Please sign in to comment.