leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2622.js (481B)


      1 class TimeLimitedCache {
      2   cache = new Map();
      3 
      4   set(key, value, duration) {
      5     const valueInCache = this.cache.get(key);
      6     if (valueInCache) {
      7       clearTimeout(valueInCache.timeout);
      8     }
      9     const timeout = setTimeout(() => this.cache.delete(key), duration);
     10     this.cache.set(key, { value, timeout });
     11     return Boolean(valueInCache);
     12   }
     13 
     14   get(key) {
     15     return this.cache.has(key) ? this.cache.get(key).value : -1;
     16   }
     17 
     18   count() {
     19     return this.cache.size;
     20   }
     21 };