ERP-node/backend-node/src/services/riskAlertCacheService.ts

101 lines
2.7 KiB
TypeScript
Raw Normal View History

/**
* /
* - 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;
}
}