国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JS > 正文

Nodejs基于LRU算法實現的緩存處理操作示例

2024-05-06 16:35:58
字體:
來源:轉載
供稿:網友

本文實例講述了Nodejs基于LRU算法實現的緩存處理操作。分享給大家供大家參考,具體如下:

LRU是Least Recently Used的縮寫,即最近最少使用頁面置換算法,是為虛擬頁式存儲管理服務的,是根據頁面調入內存后的使用情況進行決策了。由于無法預測各頁面將來的使用情況,只能利用“最近的過去”作為“最近的將來”的近似,因此,LRU算法就是將最近最久未使用的頁面予以淘汰。

可以用一個特殊的棧來保存當前正在使用的各個頁面的頁面號。當一個新的進程訪問某頁面時,便將該頁面號壓入棧頂,其他的頁面號往棧底移,如果內存不夠,則將棧底的頁面號移除。這樣,棧頂始終是最新被訪問的頁面的編號,而棧底則是最近最久未訪問的頁面的頁面號。

如輸入以下序列時:4,7,0,7,1,0,1,2,1,2,6

結果為:

4
4        7
4        7        0
4        0        7
4        0        7        1
4        7        1        0
4        7        0        1
4        7        0        1        2
4        7        0        2        1
4        7        0        1        2
7        0        1        2        6

適用于Node.js的一個LRU緩存,capacity為緩存容量,為0時構造一般緩存。

function CacheLRU(capacity) {/* 利用Buffer寫的一個LRU緩存,capacity為緩存容量,為0時不限容量。myCache = new CacheLRU(capacity); //構造緩存myCache.get(key); //讀取名為key的緩存值myCache.put(key, value); //寫入名為key的緩存值myCache.remove(key); //刪除名為key的緩存值myCache.removeAll(); //清空緩存myCache.info(); //返回myCache緩存信息LRU原理:對所有緩存數據的key構建hash鏈表,當對某一數據進行get或put操作時,將其key提到鏈表前端(最新)。當進行put數據超出容量時,刪除鏈表尾端(最舊)的緩存數據。hash鏈表操作可直接定位key,無需歷遍整個hash對象,故讀寫極快。緩存容量不再影響讀寫速度。*/  this.capacity = capacity || Number.MAX_VALUE;  this.data = {};  this.hash = {};  this.linkedList = {    length: 0,    head: null,    end: null  }  if (capacity <= 0) this.capacity = Number.MAX_VALUE;};CacheLRU.prototype.get = function(key) {  key = '_' + key;  var lruEntry = this.hash[key];  if (!lruEntry) return;  refresh(this.linkedList, lruEntry);  return JSON.parse(this.data[key].toString());};CacheLRU.prototype.put = function(key, value) {  key = '_' + key;  var lruEntry = this.hash[key];  if (value === undefined) return this;  if (!lruEntry) {    this.hash[key] = {key: key};    this.linkedList.length += 1;    lruEntry = this.hash[key];  }  refresh(this.linkedList, lruEntry);  this.data[key] = new Buffer(JSON.stringify(value));  if (this.linkedList.length > this.capacity) this.remove(this.linkedList.end.key.slice(1));  return this;};CacheLRU.prototype.remove = function(key) {  key = '_' + key;  var lruEntry = this.hash[key];  if (!lruEntry) return this;  if (lruEntry === this.linkedList.head) this.linkedList.head = lruEntry.p;  if (lruEntry === this.linkedList.end) this.linkedList.end = lruEntry.n;  link(lruEntry.n, lruEntry.p);  delete this.hash[key];  delete this.data[key];  this.linkedList.length -= 1;  return this;};CacheLRU.prototype.removeAll = function() {  this.data = {};  this.hash = {};  this.linkedList = {    length: 0,    head: null,    end: null  }  return this;};CacheLRU.prototype.info = function() {  var size = 0,    data = this.linkedList.head;  while (data){    size += this.data[data.key].length;    data = data.p;  }  return {    capacity: this.capacity,    length: this.linkedList.length,    size: size  };};// 更新鏈表,把get或put方法操作的key提到鏈表head,即表示最新function refresh(linkedList, entry) {  if (entry != linkedList.head) {    if (!linkedList.end) {      linkedList.end = entry;    } else if (linkedList.end == entry) {      linkedList.end = entry.n;    }    link(entry.n, entry.p);    link(entry, linkedList.head);    linkedList.head = entry;    linkedList.head.n = null;  }}// 對兩個鏈表對象建立鏈接,形成一條鏈function link(nextEntry, prevEntry) {  if (nextEntry != prevEntry) {    if (nextEntry) nextEntry.p = prevEntry;    if (prevEntry) prevEntry.n = nextEntry;  }}module.exports = CacheLRU;// test:/*var user = new CacheLRU(5);user.put('user1', {name:'admin', age: 30});user.put('user2', {name:'user', age: 31});user.put('user3', {name:'guest', age: 32});user.put('user4', {name:'guest', age: 34});user.put('user5', {name:'guest', age: 35});console.log(user.get('user1'));console.log(user.get('user2'));console.log(user.get('user3'));user.put('user6', {name:'guest', age: 36});console.log(user.info());*/

LRU算法也可以用于一些實際的應用中,如你要做一個瀏覽器,或類似于淘寶客戶端的應用的就要用到這個原理。大家都知道瀏覽器在瀏覽網頁的時候會把下載的圖片臨時保存在本機的一個文件夾里,下次再訪問時就會,直接從本機臨時文件夾里讀取。但保存圖片的臨時文件夾是有一定容量限制的,如果你瀏覽的網頁太多,就會一些你最不常使用的圖像刪除掉,只保留最近最久使用的一些圖片。這時就可以用到LRU算法 了,這時上面算法里的這個特殊的棧就不是保存頁面的序號了,而是每個圖片的序號或大小;所以上面這個棧的元素都用Object類來表示,這樣的話這個棧就可以保存的對像了。

希望本文所述對大家nodejs程序設計有所幫助。


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 延安市| 大方县| 全南县| 固安县| 丰宁| 大英县| 东港市| 奉化市| 新邵县| 海淀区| 肥城市| 郓城县| 丹东市| 威远县| 衡水市| 南平市| 阿克苏市| 大方县| 延庆县| 辽阳县| 百色市| 杭锦后旗| 呼玛县| 漳浦县| 阿尔山市| 白河县| 区。| 北辰区| 河西区| 隆安县| 平原县| 治县。| 阜宁县| 东至县| 健康| 石屏县| 称多县| 乌拉特中旗| 汕头市| 乌拉特后旗| 襄城县|