Skip to content

Instantly share code, notes, and snippets.

@kellegous
Last active August 29, 2015 13:57
Show Gist options
  • Save kellegous/9381098 to your computer and use it in GitHub Desktop.
Save kellegous/9381098 to your computer and use it in GitHub Desktop.
var Cache = function(n) {
this.items = {};
this.n = n;
};
Cache.prototype.set = function(id, item) {
var items = this.items,
n = this.n;
items[id] = {
item: item,
time: Date.now()
};
if (items.length > n) {
Object.keys(items).sort(function(a, b) {
return items[a].time - items[b].time;
}).slice(n).forEach(function(key) {
delete items[key];
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment