var TimeLimitedCache = function() {
this.ourMap = new Map();
};
/**
* @param {number} key
* @param {number} value
* @param {number} duration time until expiration in ms
* @return {boolean} if un-expired key already existed
*/
TimeLimitedCache.prototype.set = function(key, value, duration) {
if(this.ourMap.has(key)) {
clearTimeout(timeOutid);
this.ourMap.set(key,value);
timeOutid = setTimeout(()=>{this.ourMap.delete(key)},duration);
return true;
}
else {
this.ourMap.set(key,value);
timeOutid = setTimeout(()=>{this.ourMap.delete(key)},duration);
return false;
}
};
/**
* @param {number} key
* @return {number} value associated with key
*/
TimeLimitedCache.prototype.get = function(key) {
console.log(performance.now())
if(this.ourMap.has(key)) return this.ourMap.get(key);
else return -1;
};
/**
* @return {number} count of non-expired keys
*/
TimeLimitedCache.prototype.count = function() {
return this.ourMap.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
*/