From 7955c855bc5ef709e2e85596608f1247337252c6 Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Thu, 29 Jan 2026 00:05:56 +0700 Subject: [PATCH] fix: extend OrgStructureCache TTL and add graceful shutdown cleanup - Extended OrgStructureCache TTL from 10 to 30 minutes (reduce cleanup frequency) - Added orgStructureCache.destroy() in graceful shutdown handler - Updated documentation to reflect changes Co-Authored-By: Claude (glm-4.7) --- CHANGELOG.md | 5 +++++ docs/SUMMARY_OPTIMIZATION-fix-optimization.md | 10 +++++++--- src/app.ts | 4 ++++ src/utils/OrgStructureCache.ts | 4 ++-- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c4cb1c9..404b5f0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,11 @@ All notable changes to this project will be documented in this file. - แก้ชนิด type ที่ reques +### ⚡ Performance + +- Extended OrgStructureCache TTL from 10 to 30 minutes (reduce cleanup frequency) +- Added OrgStructureCache.destroy() in graceful shutdown handler + ### ⚙️ Miscellaneous Tasks - Git-cliff changelog diff --git a/docs/SUMMARY_OPTIMIZATION-fix-optimization.md b/docs/SUMMARY_OPTIMIZATION-fix-optimization.md index 01dcb2a6..6d4907c7 100644 --- a/docs/SUMMARY_OPTIMIZATION-fix-optimization.md +++ b/docs/SUMMARY_OPTIMIZATION-fix-optimization.md @@ -65,7 +65,11 @@ const gracefulShutdown = async (signal: string) => { // 3. ทำลาย cache instances logMemoryStore.destroy(); + console.log("[APP] LogMemoryStore destroyed"); + + // Destroy OrgStructureCache orgStructureCache.destroy(); + console.log("[APP] OrgStructureCache destroyed"); // 4. บังคับปิดหลังจาก 30 วินาที (หาก shutdown ค้าง) const shutdownTimeout = setTimeout(() => { @@ -134,7 +138,7 @@ const profile = await logMemoryStore.getProfileByKeycloak(keycloak); ```typescript class OrgStructureCache { private cache: Map; - private readonly CACHE_TTL = 10 * 60 * 1000; // 10 นาที + private readonly CACHE_TTL = 30 * 60 * 1000; // 30 นาที // Key format: org-structure-{revisionId}-{rootId} private generateKey(revisionId: string, rootId?: string): string @@ -147,9 +151,9 @@ class OrgStructureCache { **การทำงาน:** - Cache ผลลัพธ์ของ org structure ตาม `revisionId` และ `rootId` -- TTL 10 นาที - ข้อมูลเก่าจะถูกลบอัตโนมัติ +- TTL 30 นาที - ข้อมูลเก่าจะถูกลบอัตโนมัติ (ปรับจาก 10 นาที เพื่อลด cleanup frequency) - Method `invalidate()` - ลบ cache เมื่อมีการอัปเดต revision -- Auto cleanup ทุก 10 นาที +- Auto cleanup ทุก 30 นาที **การใช้งานใน API:** ```typescript diff --git a/src/app.ts b/src/app.ts index a44cace5..578a8e65 100644 --- a/src/app.ts +++ b/src/app.ts @@ -146,6 +146,10 @@ async function main() { logMemoryStore.destroy(); console.log("[APP] LogMemoryStore destroyed"); + // Destroy OrgStructureCache + orgStructureCache.destroy(); + console.log("[APP] OrgStructureCache destroyed"); + clearTimeout(shutdownTimeout); console.log("[APP] Graceful shutdown completed"); process.exit(0); diff --git a/src/utils/OrgStructureCache.ts b/src/utils/OrgStructureCache.ts index 33fa19e2..d4e6c20e 100644 --- a/src/utils/OrgStructureCache.ts +++ b/src/utils/OrgStructureCache.ts @@ -5,7 +5,7 @@ interface CacheEntry { class OrgStructureCache { private cache: Map = new Map(); - private readonly CACHE_TTL = 10 * 60 * 1000; // 10 minutes + private readonly CACHE_TTL = 30 * 60 * 1000; // 30 minutes private isInitialized = false; private cleanupTimer: NodeJS.Timeout | null = null; @@ -17,7 +17,7 @@ class OrgStructureCache { if (this.isInitialized) return; this.isInitialized = true; - // Cleanup expired entries every 10 minutes + // Cleanup expired entries every 30 minutes this.cleanupTimer = setInterval(() => { this.cleanup(); }, this.CACHE_TTL);