42 lines
1.4 KiB
JavaScript
42 lines
1.4 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.LRUCache = void 0;
|
|
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
/**
|
|
* Simple LRU (Least Recently Used) cache implementation
|
|
*/
|
|
class LRUCache {
|
|
constructor(maxSize) {
|
|
_defineProperty(this, "cache", new Map());
|
|
this.maxSize = maxSize;
|
|
}
|
|
get(key) {
|
|
var value = this.cache.get(key);
|
|
if (value !== undefined) {
|
|
this.cache.delete(key);
|
|
this.cache.set(key, value);
|
|
}
|
|
return value;
|
|
}
|
|
set(key, value) {
|
|
if (this.cache.has(key)) {
|
|
this.cache.delete(key);
|
|
} else if (this.cache.size >= this.maxSize) {
|
|
var firstKey = this.cache.keys().next().value;
|
|
this.cache.delete(firstKey);
|
|
}
|
|
this.cache.set(key, value);
|
|
}
|
|
clear() {
|
|
this.cache.clear();
|
|
}
|
|
size() {
|
|
return this.cache.size;
|
|
}
|
|
}
|
|
exports.LRUCache = LRUCache; |