运用javascript模拟指针构建LRU缓冲池

运用javascript的Map对象、双向链表实现LRU算法,其中map中存储的是id及索引,而业务数据是存储在isOwnArr索引数组中,map中元素的索引与isOwnArr数组中元素的索引一一对应,需要更新视图时优先检查map中是否已存在该对象,若不存在则将id发往后台,返回新的数据,将id对象添加到map中,将返回的新数据添加到isOwnArr数组中,此缓冲池手动定义了数据存储量200。

function DoubleLinkedList(){
var that=this;
that.map=new Map();//实例化Map对象
that.isOwnArr=[];//存储已经'缓存的'数据(索引数组)
that.loadHasIndex=[];//存储将要加载的索引
that.deleteHasIndex=[];//存储将要删除的索引
that.overflow=false;//溢出
var Obj=function(id){
this.current=id;
this.prev=null;
this.next=null;
this.index=null;
this.display='block';
}
that.head=null;//head指向最新的对象
that.tail=null;//tail指向最旧的对象
that.NotFound=[];//存储需要发送到后台的id
that.willChange=[];//当数据溢出时,溢出部分所对应的index
that.NotOverflow=[];//存储未溢出部分的id
that.HasOverflow=[];//存储溢出部分的id
//插入
that.put=function(id){
var obj=new Obj(id);
if(that.map.size==200){//检查溢出
that.overflow=true;
obj.index=that.tail.index;
obj.next=that.head.current;
that.map.set(id,obj);

that.willChange.push(obj.index);
that.head.prev=id;
that.map.set(that.head.current,that.head);
that.head=obj;

var finalSecond;
finalSecond=that.map.get(that.tail.prev);
finalSecond.next=null;
that.map.set(that.tail.prev,finalSecond);

that.map.delete(that.tail.current);//删除尾部
that.tail=finalSecond;

that.HasOverflow.push(id);
return;
}
that.NotOverflow.push(id);
obj.index=that.map.size;
//map为空
if(that.map.size==0){
that.head=obj;
that.tail=obj;
that.map.set(id,obj);
}else{//map不为空
obj.next=that.head.current;
that.map.set(id,obj);

that.head.prev=id;
that.map.set(that.head.current,that.head);

if(that.map.size==1){
that.tail=that.head;
}
that.head=obj;
}
}
//查找
that.get=function(id){
//存在则改变指针,head引用该对象
if(that.map.has(id)){
var obj=that.map.get(id);
obj.display='block';
if(that.map.size==1){
that.head=obj;
that.tail=obj;
that.map.set(id,obj);
}else if(that.map.size>1){
if(that.head.current==id){//头部
that.head=obj;
that.map.set(id,obj);
}else if(that.tail.current==id){//尾部
var previous;
that.head.prev=id;
that.map.set(that.head.current,that.head);

previous=that.map.get(obj.prev);
previous.next=null;
that.map.set(obj.prev,previous);

obj.next=that.head.current;
obj.prev=null;
that.map.set(id,obj);

that.head=obj;
that.tail=previous;
}else{//中间
var previous;
var pnext;
previous=that.map.get(obj.prev);
pnext=that.map.get(obj.next);

obj.next=that.head.current;
obj.prev=null;
that.map.set(id,obj);

that.head.prev=id;
that.map.set(that.head.current,that.head);

previous.next=pnext.current;
pnext.prev=previous.current;
that.map.set(previous.current,previous);
that.map.set(pnext.current,pnext);
if(pnext.current==that.tail.id){
that.tail=pnext;
}
that.head=obj;
}
}
}else{
//存入不存在的id
that.NotFound.push(id);
//不存在则新添加到map中
that.put(id);
}

}

        //WillLoad为需要加载到视图的数据的id(在缓冲池中可能部分id已存在)

//遍历,将需要重载的索引存入loadHasIndex
//将需要删除的索引存入deleteHasIndex
that.search=function(WillLoad){
//找出存在的
for(var key of WillLoad){
if(that.map.has(key)){
var obj=that.map.get(key);
if(obj.display=='none'){
that.loadHasIndex.push(obj.index);
}
obj.display='none';
that.map.set(key,obj);
}
}
//将display为block的对象的display赋值为none
that.map.forEach(function (v, k) {
//v-->value,k-->key
if(v.display=='block'){
v.display='none';
that.map.set(k,v);
that.deleteHasIndex.push(v.index);
}
});
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值