forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
36 lines (29 loc) · 987 Bytes
/
Solution.ts
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
class TimeLimitedCache {
#cache: Map<number, [value: number, expire: number]> = new Map();
set(key: number, value: number, duration: number): boolean {
const isExist = this.#cache.has(key);
if (!this.#isExpired(key)) {
this.#cache.set(key, [value, Date.now() + duration]);
}
return isExist;
}
get(key: number): number {
if (this.#isExpired(key)) return -1;
const res = this.#cache.get(key)?.[0] ?? -1;
return res;
}
count(): number {
const xs = Array.from(this.#cache).filter(([key]) => !this.#isExpired(key));
return xs.length;
}
#isExpired = (key: number) =>
this.#cache.has(key) &&
(this.#cache.get(key)?.[1] ?? Number.NEGATIVE_INFINITY) < Date.now();
}
/**
* Your TimeLimitedCache object will be instantiated and called as such:
* var obj = new TimeLimitedCache()
* obj.set(1, 42, 1000); // false
* obj.get(1) // 42
* obj.count() // 1
*/