-
Notifications
You must be signed in to change notification settings - Fork 0
/
2622-cache-with-time-limit.ts
58 lines (46 loc) · 1.74 KB
/
2622-cache-with-time-limit.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Title: Cache With Time Limit
// Description:
// Write a class that allows getting and setting key-value pairs, however a time until expiration is associated with each key.
// The class has three public methods:
// set(key, value, duration):
// accepts an integer key, an integer value, and a duration in milliseconds.
// Once the duration has elapsed, the key should be inaccessible.
// The method should return true if the same un-expired key already exists and false otherwise.
// Both the value and duration should be overwritten if the key already exists.
// get(key): if an un-expired key exists, it should return the associated value. Otherwise it should return -1.
// count(): returns the count of un-expired keys.
// Link: https://leetcode.com/problems/cache-with-time-limit/
class TimeLimitedCache {
private data: Map<number, { value: number, timeoutId: ReturnType<typeof setTimeout> }>;
constructor() {
this.data = new Map();
}
set(key: number, value: number, duration: number): boolean {
const isExisted = this.data.has(key);
if (isExisted) {
clearTimeout(this.data.get(key).timeoutId);
}
const timeoutId = setTimeout(() => {
this.data.delete(key);
}, duration);
this.data.set(key, { value, timeoutId });
return isExisted;
}
get(key: number): number {
const isExisted = this.data.has(key);
if (isExisted) {
return this.data.get(key).value;
}
return -1;
}
count(): number {
return this.data.size;
}
}
/**
* 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
*/