cione-comp-lib
Version:
`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`
46 lines (45 loc) • 1.17 kB
JavaScript
import LinkedList from "./LinkedList.mjs";
var LRU = function(maxSize) {
this._list = new LinkedList();
this._map = {};
this._maxSize = maxSize || 10;
};
LRU.prototype.setMaxSize = function(size) {
this._maxSize = size;
};
LRU.prototype.put = function(key, value) {
if (!this._map.hasOwnProperty(key)) {
var len = this._list.length();
if (len >= this._maxSize && len > 0) {
var leastUsedEntry = this._list.head;
this._list.remove(leastUsedEntry);
delete this._map[leastUsedEntry.key];
}
var entry = this._list.insert(value);
entry.key = key;
this._map[key] = entry;
}
};
LRU.prototype.get = function(key) {
var entry = this._map[key];
if (this._map.hasOwnProperty(key)) {
if (entry !== this._list.tail) {
this._list.remove(entry);
this._list.insertEntry(entry);
}
return entry.value;
}
};
LRU.prototype.remove = function(key) {
var entry = this._map[key];
if (typeof entry !== "undefined") {
delete this._map[key];
this._list.remove(entry);
}
};
LRU.prototype.clear = function() {
this._list.clear();
this._map = {};
};
var LRUCache = LRU;
export { LRUCache as default };