Skip to content
This repository was archived by the owner on Jul 22, 2020. It is now read-only.

Commit 991ab79

Browse files
committed
feat: implement 2d redis geoip cache
1 parent 5ab1b19 commit 991ab79

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

api/api.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -390,20 +390,32 @@ if (fs.existsSync(geoipWhitelistFile)) {
390390
}
391391
}
392392

393-
function geoipLookup(ip) {
393+
const GEOIP_CACHE_TTL = 2 * 24 * 60 * 60; // 2d
394+
395+
async function geoipLookup(ip) {
394396
if (GEOLOCATION_API_KEY == '') {
395397
return null;
396398
}
397399
if (geoipWhitelist[ip]) {
398400
return geoipWhitelist[ip];
399401
}
400-
return new Promise(resolve => {
402+
403+
const val = await getAsync(`!ip:${ip}`);
404+
if (val) {
405+
return JSON.parse(val);
406+
}
407+
408+
const remoteVal = await new Promise(resolve => {
401409
const geolocationParams = new GeolocationParams();
402410
geolocationParams.setIPAddress(ip);
403411
ipgeolocationApi.getGeolocation(json => {
404412
return resolve(json);
405413
}, geolocationParams);
406414
});
415+
416+
await setexAsync(`!ip:${ip}`, GEOIP_CACHE_TTL, JSON.stringify(remoteVal));
417+
418+
return remoteVal;
407419
}
408420

409421
// DEPRECATED (should be unused, performed internally now)

0 commit comments

Comments
 (0)