fix: performance related fixes

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-10-22 12:29:56 +02:00
parent 8bb0c87ec9
commit 1285ad85a2
60 changed files with 4264 additions and 2959 deletions

View File

@@ -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);
}