-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTimedCache.mjs
More file actions
38 lines (32 loc) · 629 Bytes
/
TimedCache.mjs
File metadata and controls
38 lines (32 loc) · 629 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
export default class TimedCache {
constructor(ttl) {
this.ttl = ttl
this.cache = {}
setInterval(() => {
Object.keys(this.cache).forEach(key => {
this.get(key)
})
}, 1000 * 60)
}
get(key) {
let holder = this.cache[key]
if (holder) {
if (new Date() - holder.created > this.ttl) {
delete this.cache[key]
return null
} return this.cache[key].obj
} else return null
}
put(key, value) {
this.cache[key] = {
created: new Date(),
obj: value
}
}
getTTL() {
return this.ttl
}
setTTL(ttl) {
this.ttl = ttl
}
}