leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
2622.js (481B)
0 class TimeLimitedCache {
1 cache = new Map();
3 set(key, value, duration) {
4 const valueInCache = this.cache.get(key);
5 if (valueInCache) {
6 clearTimeout(valueInCache.timeout);
7 }
8 const timeout = setTimeout(() => this.cache.delete(key), duration);
9 this.cache.set(key, { value, timeout });
10 return Boolean(valueInCache);
11 }
13 get(key) {
14 return this.cache.has(key) ? this.cache.get(key).value : -1;
15 }
17 count() {
18 return this.cache.size;
19 }
20 };