Skip to content

Instantly share code, notes, and snippets.

@twalk4821
Created April 20, 2017 16:54
Show Gist options
  • Save twalk4821/c5f2a3d34d5f7d8f97d02fa5659d69ff to your computer and use it in GitHub Desktop.
Save twalk4821/c5f2a3d34d5f7d8f97d02fa5659d69ff to your computer and use it in GitHub Desktop.
const LFUcache = (capacity) => {
let box = {}
box.cache = {}
for (var i = 0; i<capacity; i++) {
box[i] = [];
}
box.put = function (k, v) {
if (box.cache.keys.length <capacity) {
if (!(k in cache)) {
var item = [k,v, 0]
this[0].push(item);
this.cache[k] = item;
}
} else {
let LFU = box[0].splice(box[0].length-1)
delete box[LFU[0]]
}
}
box.get = function(k) {
let item = box.cache[k]
if (item) {
box[item[2]].splice(box[item[2]].indexOf(item))
item[2] += 1;
box[item[2]].unshift(item)
return item
} else {
return -1
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment