利用闭包实现缓存
var cacheTest = function(){
var cache = {}, count=[];
return {
attachSearch: function(id){
if(id in cache){
return cache[id];
}
if(count.length > 2){
delete cache[count.shift()];
}
cache[id] = id;
count.push(id);
return id;
}
}
}();
发布时间: 2012-11-06 14:07:00 作者: rapoo
利用闭包实现缓存
var cacheTest = function(){
var cache = {}, count=[];
return {
attachSearch: function(id){
if(id in cache){
return cache[id];
}
if(count.length > 2){
delete cache[count.shift()];
}
cache[id] = id;
count.push(id);
return id;
}
}
}();