Improve Edge Script cache: 5min TTL for clean IPs, 1min for blocked

Reduces DB queries ~80% by caching clean IPs longer. Blocked IPs keep
short TTL for quick unblock propagation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
kappa
2026-02-13 10:35:24 +09:00
parent 3f8bf5e4e2
commit 8a015b8bd2

View File

@@ -21,7 +21,8 @@ interface CacheEntry {
} }
const cache = new Map<string, CacheEntry>(); const cache = new Map<string, CacheEntry>();
const CACHE_TTL_MS = 60_000; const CACHE_TTL_BLOCKED_MS = 60_000; // 차단 IP: 1분 (빠른 해제 반영)
const CACHE_TTL_CLEAN_MS = 300_000; // 정상 IP: 5분 (DB 부하 감소)
const CACHE_MAX_SIZE = 50_000; const CACHE_MAX_SIZE = 50_000;
function cacheGet(ip: string): boolean | null { function cacheGet(ip: string): boolean | null {
@@ -44,7 +45,8 @@ function cacheSet(ip: string, blocked: boolean): void {
// If still too large, clear entirely // If still too large, clear entirely
if (cache.size >= CACHE_MAX_SIZE) cache.clear(); if (cache.size >= CACHE_MAX_SIZE) cache.clear();
} }
cache.set(ip, { blocked, expiresAt: Date.now() + CACHE_TTL_MS }); const ttl = blocked ? CACHE_TTL_BLOCKED_MS : CACHE_TTL_CLEAN_MS;
cache.set(ip, { blocked, expiresAt: Date.now() + ttl });
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------