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

Commit

Permalink
feat: implement 2d redis geoip cache
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnygleason committed Oct 23, 2019
1 parent 5ab1b19 commit 991ab79
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,20 +390,32 @@ if (fs.existsSync(geoipWhitelistFile)) {
}
}

function geoipLookup(ip) {
const GEOIP_CACHE_TTL = 2 * 24 * 60 * 60; // 2d

async function geoipLookup(ip) {
if (GEOLOCATION_API_KEY == '') {
return null;
}
if (geoipWhitelist[ip]) {
return geoipWhitelist[ip];
}
return new Promise(resolve => {

const val = await getAsync(`!ip:${ip}`);
if (val) {
return JSON.parse(val);
}

const remoteVal = await new Promise(resolve => {
const geolocationParams = new GeolocationParams();
geolocationParams.setIPAddress(ip);
ipgeolocationApi.getGeolocation(json => {
return resolve(json);
}, geolocationParams);
});

await setexAsync(`!ip:${ip}`, GEOIP_CACHE_TTL, JSON.stringify(remoteVal));

return remoteVal;
}

// DEPRECATED (should be unused, performed internally now)
Expand Down

0 comments on commit 991ab79

Please sign in to comment.