You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
set: 新增数据,将数据插入到双联表尾部,同时判断链表元素数量是否超出限制,若超出则移除 head 指针指向的元素
classCache{constructor(limit){this.size=0;this.limit=limit;this.head=this.tail=undefined;this._keymap=Object.create(null);}set(key,value){letentry={ key, value };this._keymap[key]=entry;if(this.tail){this.tail.newer=entry;entry.older=this.tail;}else{this.head=entry;}this.tail=entry;if(this.size===this.limit){returnthis.shift();}else{this.size++;}}shift(){letentry=this.head;if(entry){this.head=this.head.newer;this.head.older=undefined;entry.newer=entry.older=undefined;this._keymap[entry.key]=undefined;}returnentry;}get(key,returnEntry){letentry=this._keymap[key];if(entry===undefined){return;}if(entry===this.tail){returnreturnEntry
? entry
: entry.value;}// HEAD--------------TAIL// <.older .newer>// <--- add direction --// A B C <D> Eif(entry.newer){if(entry===this.head){this.head=entry.newer;}entry.newer.older=entry.older;// C <-- E.}if(entry.older){entry.older.newer=entry.newer;// C. --> E}entry.newer=undefined;// D --xentry.older=this.tail;// D. --> Eif(this.tail){this.tail.newer=entry;// E. <-- D}this.tail=entry;returnreturnEntry
? entry
: entry.value;}}exportdefaultCache;
The text was updated successfully, but these errors were encountered:
LRU 是 Least Recently Used 的缩写,即最近最少使用
The text was updated successfully, but these errors were encountered: