fix: performance related fixes
This commit is contained in:
@@ -7,6 +7,7 @@ const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
import type { ReaderModel } from '@maxmind/geoip2-node';
|
||||
import { Reader } from '@maxmind/geoip2-node';
|
||||
import { LRUCache } from 'lru-cache';
|
||||
|
||||
const filename = 'GeoLite2-City.mmdb';
|
||||
// From api or worker package
|
||||
@@ -50,11 +51,22 @@ const DEFAULT_GEO: GeoLocation = {
|
||||
|
||||
const ignore = ['127.0.0.1', '::1'];
|
||||
|
||||
const cache = new LRUCache<string, GeoLocation>({
|
||||
max: 1000,
|
||||
ttl: 1000 * 60 * 5,
|
||||
ttlAutopurge: true,
|
||||
});
|
||||
|
||||
export async function getGeoLocation(ip?: string): Promise<GeoLocation> {
|
||||
if (!ip || ignore.includes(ip)) {
|
||||
return DEFAULT_GEO;
|
||||
}
|
||||
|
||||
const cached = cache.get(ip);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
if (!reader) {
|
||||
await loadDatabase(dbPath);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user