Skip to content

Instantly share code, notes, and snippets.

@techsin
Last active April 17, 2020 23:45
Show Gist options
  • Save techsin/5597dea4b8df3bd0461396cf8263b0cf to your computer and use it in GitHub Desktop.
Save techsin/5597dea4b8df3bd0461396cf8263b0cf to your computer and use it in GitHub Desktop.
lru cache
var LRUCache = function(capacity) {
this.hash = {};
this.capacity = capacity;
this.counter = 0;
};
LRUCache.prototype.get = function(key) {
const obj = this.hash[key];
if (typeof obj === "undefined") {
return -1;
} else {
obj.index = ++this.counter;
return obj.val;
}
};
LRUCache.prototype.put = function(key, value) {
const {capacity, hash} = this;
if (typeof hash[key] !== "undefined") {
hash[key].val = value
hash[key].index = ++this.counter;
} else {
if (capacity > 0) {
this.capacity--;
} else {
const oldestKey = this.getOldestKey();
delete hash[oldestKey];
}
hash[key] = {val: value, index: ++this.counter };
}
};
LRUCache.prototype.getOldestKey = function(){
let min = Number.POSITIVE_INFINITY;
let tempKey = null;
for (const key in this.hash) {
const {index} = this.hash[key];
if (index < min) {
min = index;
tempKey = key;
}
}
return tempKey;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment