ERP-node/frontend/lib/caching/codeCache.ts

199 lines
4.8 KiB
TypeScript
Raw Normal View History

/**
*
* .
*/
import { commonCodeApi } from "@/lib/api/commonCode";
interface CacheEntry {
data: any;
timestamp: number;
expiry: number;
}
class CodeCache {
private cache = new Map<string, CacheEntry>();
private defaultTTL = 5 * 60 * 1000; // 5분
/**
*
*/
set(key: string, data: any, ttl?: number): void {
const expiry = ttl || this.defaultTTL;
const entry: CacheEntry = {
data,
timestamp: Date.now(),
expiry,
};
this.cache.set(key, entry);
}
/**
*
*/
get(key: string): any | null {
const entry = this.cache.get(key);
if (!entry) {
return null;
}
// TTL 체크
if (Date.now() - entry.timestamp > entry.expiry) {
this.cache.delete(key);
return null;
}
return entry.data;
}
/**
*
*/
delete(key: string): boolean {
return this.cache.delete(key);
}
/**
*
*/
clear(): void {
this.cache.clear();
}
/**
*
*/
cleanup(): void {
const now = Date.now();
for (const [key, entry] of this.cache.entries()) {
if (now - entry.timestamp > entry.expiry) {
this.cache.delete(key);
}
}
}
/**
*
*/
getStats(): { size: number; keys: string[] } {
return {
size: this.cache.size,
keys: Array.from(this.cache.keys()),
};
}
/**
*
*/
createCodeKey(category: string, companyCode?: string): string {
return `code:${category}:${companyCode || "*"}`;
}
/**
*
*/
async preloadCodes(categories: string[]): Promise<void> {
2025-11-28 11:52:23 +09:00
// console.log(`🔄 코드 배치 로딩 시작: ${categories.join(", ")}`);
const promises = categories.map(async (category) => {
try {
const response = await commonCodeApi.codes.getList(category, { isActive: true });
if (response.success && response.data) {
const cacheKey = this.createCodeKey(category);
this.set(cacheKey, response.data, this.defaultTTL);
2025-11-28 11:52:23 +09:00
// console.log(`✅ 코드 로딩 완료: ${category} (${response.data.length}개)`);
}
} catch (error) {
console.error(`❌ 코드 로딩 실패: ${category}`, error);
}
});
await Promise.all(promises);
2025-11-28 11:52:23 +09:00
// console.log(`✅ 코드 배치 로딩 완료: ${categories.length}개 카테고리`);
}
/**
* ()
*/
getCodeSync(category: string, companyCode?: string): any[] | null {
const cacheKey = this.createCodeKey(category, companyCode);
return this.get(cacheKey);
}
/**
* ( API )
*/
async getCodeAsync(category: string, companyCode?: string): Promise<any[]> {
const cached = this.getCodeSync(category, companyCode);
if (cached) {
return cached;
}
try {
const response = await commonCodeApi.codes.getList(category, { isActive: true });
if (response.success && response.data) {
const cacheKey = this.createCodeKey(category, companyCode);
this.set(cacheKey, response.data, this.defaultTTL);
return response.data;
}
} catch (error) {
console.error(`❌ 코드 조회 실패: ${category}`, error);
}
return [];
}
/**
* ( )
*/
getCacheInfo(): {
size: number;
keys: string[];
totalMemoryUsage: number;
hitRate?: number;
} {
const stats = this.getStats();
// 메모리 사용량 추정 (대략적)
let totalMemoryUsage = 0;
for (const [key, entry] of this.cache.entries()) {
// 키 크기 + 데이터 크기 추정
totalMemoryUsage += key.length * 2; // 문자열은 UTF-16이므로 2바이트
if (Array.isArray(entry.data)) {
totalMemoryUsage += entry.data.length * 100; // 각 항목당 대략 100바이트로 추정
} else {
totalMemoryUsage += JSON.stringify(entry.data).length * 2;
}
}
return {
...stats,
totalMemoryUsage,
};
}
/**
*
*/
invalidate(category: string, companyCode?: string): boolean {
const cacheKey = this.createCodeKey(category, companyCode);
return this.delete(cacheKey);
}
}
// 싱글톤 인스턴스 생성
const codeCache = new CodeCache();
// 주기적으로 만료된 캐시 정리 (10분마다)
if (typeof window !== "undefined") {
setInterval(
() => {
codeCache.cleanup();
},
10 * 60 * 1000,
);
}
export default codeCache;
export { CodeCache, codeCache };