Changed LogMemoryStore from active refresh (setInterval) to passive refresh on-access (60 min TTL)
This commit is contained in:
parent
7955c855bc
commit
656c2e7341
3 changed files with 32 additions and 22 deletions
|
|
@ -17,10 +17,8 @@ class LogMemoryStore {
|
|||
rootIdCache: new Map(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
private readonly REFRESH_INTERVAL = 10 * 60 * 1000; // 10 minutes
|
||||
private isRefreshing = false;
|
||||
private readonly CACHE_TTL = 60 * 60 * 1000; // 60 minutes
|
||||
private isInitialized = false;
|
||||
private refreshTimer: NodeJS.Timeout | null = null;
|
||||
|
||||
constructor() {
|
||||
// ไม่ refresh ทันที - รอให้เรียก initialize() หลัง TypeORM ready
|
||||
|
|
@ -35,24 +33,15 @@ class LogMemoryStore {
|
|||
|
||||
this.isInitialized = true;
|
||||
this.refreshCache();
|
||||
this.refreshTimer = setInterval(() => {
|
||||
this.refreshCache();
|
||||
}, this.REFRESH_INTERVAL);
|
||||
|
||||
console.log(
|
||||
"[LogMemoryStore] Initialized with",
|
||||
this.REFRESH_INTERVAL / 1000,
|
||||
"second refresh interval",
|
||||
this.CACHE_TTL / 1000 / 60,
|
||||
"minute TTL (passive cleanup on access)",
|
||||
);
|
||||
}
|
||||
|
||||
private async refreshCache() {
|
||||
if (this.isRefreshing) {
|
||||
console.log("[LogMemoryStore] Already refreshing, skipping...");
|
||||
return;
|
||||
}
|
||||
|
||||
this.isRefreshing = true;
|
||||
try {
|
||||
// Refresh revision cache
|
||||
const repoRevision = AppDataSource.getRepository(OrgRevision);
|
||||
|
|
@ -72,12 +61,26 @@ class LogMemoryStore {
|
|||
console.log("[LogMemoryStore] Cache refreshed at", this.cache.updatedAt.toISOString());
|
||||
} catch (error) {
|
||||
console.error("[LogMemoryStore] Error refreshing cache:", error);
|
||||
} finally {
|
||||
this.isRefreshing = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if cache is stale and refresh if needed
|
||||
private async checkAndRefreshIfNeeded() {
|
||||
const now = new Date();
|
||||
const age = now.getTime() - this.cache.updatedAt.getTime();
|
||||
if (age > this.CACHE_TTL) {
|
||||
console.log(
|
||||
"[LogMemoryStore] Cache is stale (age:",
|
||||
Math.round(age / 1000 / 60),
|
||||
"minutes), refreshing...",
|
||||
);
|
||||
await this.refreshCache();
|
||||
}
|
||||
}
|
||||
|
||||
getCurrentRevision(): OrgRevision | null {
|
||||
// Check for stale data (fire and forget)
|
||||
this.checkAndRefreshIfNeeded();
|
||||
return this.cache.currentRevision;
|
||||
}
|
||||
|
||||
|
|
@ -89,6 +92,9 @@ class LogMemoryStore {
|
|||
* Get Profile by keycloak ID with caching
|
||||
*/
|
||||
async getProfileByKeycloak(keycloak: string): Promise<Profile | null> {
|
||||
// Check for stale data
|
||||
await this.checkAndRefreshIfNeeded();
|
||||
|
||||
// Check cache first
|
||||
if (this.cache.profileCache.has(keycloak)) {
|
||||
return this.cache.profileCache.get(keycloak)!;
|
||||
|
|
@ -114,6 +120,9 @@ class LogMemoryStore {
|
|||
async getRootIdByProfileId(profileId: string | undefined): Promise<string | null> {
|
||||
if (!profileId) return null;
|
||||
|
||||
// Check for stale data
|
||||
await this.checkAndRefreshIfNeeded();
|
||||
|
||||
// Check cache first
|
||||
if (this.cache.rootIdCache.has(profileId)) {
|
||||
return this.cache.rootIdCache.get(profileId)!;
|
||||
|
|
@ -148,10 +157,8 @@ class LogMemoryStore {
|
|||
|
||||
// สำหรับ shutdown
|
||||
destroy() {
|
||||
if (this.refreshTimer) {
|
||||
clearInterval(this.refreshTimer);
|
||||
this.refreshTimer = null;
|
||||
}
|
||||
// No active timer to clear
|
||||
console.log("[LogMemoryStore] Destroyed");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue