js簡単LRU実現
789 ワード
/* ======================================== LRU ======================================== */
function LRUCache(limit){
limit = limit||10;
var _store = [];
var index = {};
this.get = function(key){
var ind = index[key];
if(_store[ind]==null){ //
return null;
}
var newArr = _store.splice(0,ind+1);
var result = newArr.pop();
_store = newArr.concat(_store);
_store.unshift(result);
for(var k in index){
var i = index[k];
if(i==ind){
index[k] = 0;
}else if(i=limit){
_store.pop();
}
for(var k in index){
index[k] = index[k]+1;
}
_store.unshift(value);
index[key] = 0;;
};
this.list = function(){
console.log(_store);
};
}