배송/화물현황과 리스크/알림(api 활용, 공공데이터 복구시 대체될 가능성 있음)

This commit is contained in:
leeheejin
2025-10-14 16:36:00 +09:00
parent 909024b635
commit c6930a4e66
20 changed files with 2819 additions and 165 deletions

View File

@@ -0,0 +1,100 @@
/**
* 리스크/알림 캐시 서비스
* - 10분마다 자동 갱신
* - 메모리 캐시로 빠른 응답
*/
import { RiskAlertService, Alert } from './riskAlertService';
export class RiskAlertCacheService {
private static instance: RiskAlertCacheService;
private riskAlertService: RiskAlertService;
// 메모리 캐시
private cachedAlerts: Alert[] = [];
private lastUpdated: Date | null = null;
private updateInterval: NodeJS.Timeout | null = null;
private constructor() {
this.riskAlertService = new RiskAlertService();
}
/**
* 싱글톤 인스턴스
*/
public static getInstance(): RiskAlertCacheService {
if (!RiskAlertCacheService.instance) {
RiskAlertCacheService.instance = new RiskAlertCacheService();
}
return RiskAlertCacheService.instance;
}
/**
* 자동 갱신 시작 (10분 간격)
*/
public startAutoRefresh(): void {
console.log('🔄 리스크/알림 자동 갱신 시작 (10분 간격)');
// 즉시 첫 갱신
this.refreshCache();
// 10분마다 갱신 (600,000ms)
this.updateInterval = setInterval(() => {
this.refreshCache();
}, 10 * 60 * 1000);
}
/**
* 자동 갱신 중지
*/
public stopAutoRefresh(): void {
if (this.updateInterval) {
clearInterval(this.updateInterval);
this.updateInterval = null;
console.log('⏸️ 리스크/알림 자동 갱신 중지');
}
}
/**
* 캐시 갱신
*/
private async refreshCache(): Promise<void> {
try {
console.log('🔄 리스크/알림 캐시 갱신 중...');
const startTime = Date.now();
const alerts = await this.riskAlertService.getAllAlerts();
this.cachedAlerts = alerts;
this.lastUpdated = new Date();
const duration = Date.now() - startTime;
console.log(`✅ 리스크/알림 캐시 갱신 완료! (${duration}ms)`);
console.log(` - 총 ${alerts.length}건의 알림`);
console.log(` - 기상특보: ${alerts.filter(a => a.type === 'weather').length}`);
console.log(` - 교통사고: ${alerts.filter(a => a.type === 'accident').length}`);
console.log(` - 도로공사: ${alerts.filter(a => a.type === 'construction').length}`);
} catch (error: any) {
console.error('❌ 리스크/알림 캐시 갱신 실패:', error.message);
}
}
/**
* 캐시된 알림 조회 (빠름!)
*/
public getCachedAlerts(): { alerts: Alert[]; lastUpdated: Date | null } {
return {
alerts: this.cachedAlerts,
lastUpdated: this.lastUpdated,
};
}
/**
* 수동 갱신 (필요 시)
*/
public async forceRefresh(): Promise<Alert[]> {
await this.refreshCache();
return this.cachedAlerts;
}
}