js 简单实现 LRU
2017-11-01 16:16
435 查看
/* ======================================== 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<ind){ index[k] = ++index[k]; } } return result; }; this.set = function(key,value){ if(_store.length>=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); }; }
相关文章推荐
- js实现简单网速测试方法
- js实现在网页上简单显示时间的方法
- js实现带简单弹性运动的导航条
- 简单JS实现走马灯效果的文字(无需jQuery)
- JS+CSS简单实现DIV遮罩层显示隐藏
- 利用js简单实现图片的放大缩小功能
- js实现简单的省市县三级联动效果实例
- JS简单实现动画弹出层效果
- json对象与数组以及转换成js对象的简单实现方法
- Phaser.js实现简单的跑酷游戏附源码下载
- JS实现页面内跳转的简单代码
- js简单实现标签云效果实例
- js+css实现的简单的下拉菜单。兼容性好。
- JS实现简单的时分秒倒计时代码
- js简单实现HTML标签Select联动带跳转
- js实现简单登录功能的实例代码
- 教你用js超简单实现页面自动控制图片的宽度和高度
- 使用go语言的list实现一个简单的LRU缓存
- JS实现超简单的鼠标拖动效果
- js实现简单易用的上下无缝滚动效果