Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function LRUCache(capacity, init)
- {
- var capacity = capacity;
- var data = init || {};
- var timestamps = {};
- var getTimestamp = function()
- {
- var time = Date.now();
- while(time === Date.now());
- return Date.now();
- };
- var findLRU = function()
- {
- var keys = Object.keys(timestamps);
- return keys
- .reduce(function(acc, x)
- {
- if (timestamps[x] < acc.time)
- {
- return {key: x, time: timestamps[x]} ;
- } else { return acc;}
- }, {key: keys[0], time: timestamps[keys[0]] });
- };
- var cache = function(key, val)
- {
- if (!key) return data;
- if (key && val)
- {
- setCache(key, val);
- }
- if (this.size > this.capacity)
- {
- var lruItem = findLRU();
- deleteCache(lruItem.key);
- }
- return this;
- }.bind(this);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement