Cache query in progress to reduce redundant GeoIP2 queries.

This commit is contained in:
Adam Ierymenko 2015-12-18 09:38:34 -08:00
parent e3eea6fcbd
commit 61dc13a6e1

View file

@ -19,8 +19,8 @@ if (!config.maxmind) {
console.error('FATAL: only MaxMind GeoIP2 is currently supported and is not configured in config.js'); console.error('FATAL: only MaxMind GeoIP2 is currently supported and is not configured in config.js');
process.exit(1); process.exit(1);
} }
var geo = require('geoip2ws')(config.maxmind);
var geo = require('geoip2ws')(config.maxmind);
var cache = require('levelup')(__dirname + '/cache.leveldb'); var cache = require('levelup')(__dirname + '/cache.leveldb');
function lookup(ip,callback) function lookup(ip,callback)
@ -32,29 +32,40 @@ function lookup(ip,callback)
if (cachedEntry) { if (cachedEntry) {
let ts = cachedEntry.ts; let ts = cachedEntry.ts;
let r = cachedEntry.r; let r = cachedEntry.r;
if (ts) { if ((ts)&&((Date.now() - ts) < CACHE_TTL)) {
if ((Date.now() - ts) < CACHE_TTL) //console.error(ip+': cached!');
return callback(null,r); return callback(null,(r) ? r : null);
} }
} }
} catch (e) {} } catch (e) {}
} }
geo(ip,function(err,result) { cache.put(ip,JSON.stringify({
if (err) ts: Date.now() - (CACHE_TTL - 30000), // set ts to expire in 30 seconds while the query is in progress
return callback(err,null); r: null
if (!result) }),function(err) {
result = null;
cache.put(ip,JSON.stringify({ geo(ip,function(err,result) {
ts: Date.now(), if (err) {
r: result //console.error(err);
}),function(err) { return callback(err,null);
if (err) }
console.error('Error saving to cache: '+err);
return callback(null,result); if (!result)
result = null;
cache.put(ip,JSON.stringify({
ts: Date.now(),
r: result
}),function(err) {
if (err)
console.error('Error saving to cache: '+err);
return callback(null,result);
});
}); });
}); });
}); });
}; };