-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeOffCache.ts
More file actions
43 lines (34 loc) · 1009 Bytes
/
TimeOffCache.ts
File metadata and controls
43 lines (34 loc) · 1009 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { ITimeOff } from './interfaces/ITimeOff';
export class TimeOffCache {
private static instance: TimeOffCache;
private cache: Map<string, { data: ITimeOff; expiresAt: number }>;
private TTL = 60 * 60 * 1000; // 1 hour cache
private constructor() {
this.cache = new Map();
}
public static getInstance(): TimeOffCache {
if (!TimeOffCache.instance) {
TimeOffCache.instance = new TimeOffCache();
}
return TimeOffCache.instance;
}
public set(userId: string, timeOffData: ITimeOff): void {
const expiresAt = Date.now() + this.TTL;
this.cache.set(userId, { data: timeOffData, expiresAt });
}
public get(userId: string): ITimeOff | undefined {
const cached = this.cache.get(userId);
if (!cached) return undefined;
if (Date.now() > cached.expiresAt) {
this.cache.delete(userId);
return undefined;
}
return cached.data;
}
public invalidateUser(userId: string): void {
this.cache.delete(userId);
}
public invalidateCache(): void {
this.cache.clear();
}
}