108 lines
4.0 KiB
JavaScript
108 lines
4.0 KiB
JavaScript
shadow$provide.module$node_modules$tiny_lru$dist$tiny_lru_cjs = function(global, require, module, exports) {
|
|
class LRU {
|
|
constructor(max = 0, ttl = 0, resetTtl = !1) {
|
|
this.first = null;
|
|
this.items = Object.create(null);
|
|
this.last = null;
|
|
this.max = max;
|
|
this.resetTtl = resetTtl;
|
|
this.size = 0;
|
|
this.ttl = ttl;
|
|
}
|
|
clear() {
|
|
this.first = null;
|
|
this.items = Object.create(null);
|
|
this.last = null;
|
|
this.size = 0;
|
|
return this;
|
|
}
|
|
delete(key) {
|
|
if (this.has(key)) {
|
|
const item = this.items[key];
|
|
delete this.items[key];
|
|
this.size--;
|
|
null !== item.prev && (item.prev.next = item.next);
|
|
null !== item.next && (item.next.prev = item.prev);
|
|
this.first === item && (this.first = item.next);
|
|
this.last === item && (this.last = item.prev);
|
|
}
|
|
return this;
|
|
}
|
|
entries(keys = this.keys()) {
|
|
const result = Array(keys.length);
|
|
for (let i = 0; i < keys.length; i++) {
|
|
const key = keys[i];
|
|
result[i] = [key, this.get(key)];
|
|
}
|
|
return result;
|
|
}
|
|
evict(bypass = !1) {
|
|
if (bypass || 0 < this.size) {
|
|
bypass = this.first, delete this.items[bypass.key], 0 === --this.size ? this.last = this.first = null : (this.first = bypass.next, this.first.prev = null);
|
|
}
|
|
return this;
|
|
}
|
|
expiresAt(key) {
|
|
let result;
|
|
this.has(key) && (result = this.items[key].expiry);
|
|
return result;
|
|
}
|
|
get(key) {
|
|
const item = this.items[key];
|
|
if (void 0 !== item) {
|
|
if (0 < this.ttl && item.expiry <= Date.now()) {
|
|
this.delete(key);
|
|
} else {
|
|
return this.moveToEnd(item), item.value;
|
|
}
|
|
}
|
|
}
|
|
has(key) {
|
|
return key in this.items;
|
|
}
|
|
moveToEnd(item) {
|
|
this.last !== item && (null !== item.prev && (item.prev.next = item.next), null !== item.next && (item.next.prev = item.prev), this.first === item && (this.first = item.next), item.prev = this.last, item.next = null, null !== this.last && (this.last.next = item), this.last = item, null === this.first && (this.first = item));
|
|
}
|
|
keys() {
|
|
const result = Array(this.size);
|
|
let x = this.first, i = 0;
|
|
for (; null !== x;) {
|
|
result[i++] = x.key, x = x.next;
|
|
}
|
|
return result;
|
|
}
|
|
setWithEvicted(key, value, resetTtl = this.resetTtl) {
|
|
let evicted = null;
|
|
this.has(key) ? this.set(key, value, !0, resetTtl) : (0 < this.max && this.size === this.max && (evicted = {...this.first}, this.evict(!0)), key = this.items[key] = {expiry:0 < this.ttl ? Date.now() + this.ttl : this.ttl, key, prev:this.last, next:null, value}, 1 === ++this.size ? this.first = key : this.last.next = key, this.last = key);
|
|
return evicted;
|
|
}
|
|
set(key, value, bypass = !1, resetTtl = this.resetTtl) {
|
|
let item = this.items[key];
|
|
bypass || void 0 !== item ? (item.value = value, !1 === bypass && resetTtl && (item.expiry = 0 < this.ttl ? Date.now() + this.ttl : this.ttl), this.moveToEnd(item)) : (0 < this.max && this.size === this.max && this.evict(!0), item = this.items[key] = {expiry:0 < this.ttl ? Date.now() + this.ttl : this.ttl, key, prev:this.last, next:null, value}, 1 === ++this.size ? this.first = item : this.last.next = item, this.last = item);
|
|
return this;
|
|
}
|
|
values(keys = this.keys()) {
|
|
const result = Array(keys.length);
|
|
for (let i = 0; i < keys.length; i++) {
|
|
result[i] = this.get(keys[i]);
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
exports.LRU = LRU;
|
|
exports.lru = function(max = 1000, ttl = 0, resetTtl = !1) {
|
|
if (isNaN(max) || 0 > max) {
|
|
throw new TypeError("Invalid max value");
|
|
}
|
|
if (isNaN(ttl) || 0 > ttl) {
|
|
throw new TypeError("Invalid ttl value");
|
|
}
|
|
if ("boolean" !== typeof resetTtl) {
|
|
throw new TypeError("Invalid resetTtl value");
|
|
}
|
|
return new LRU(max, ttl, resetTtl);
|
|
};
|
|
};
|
|
|
|
//# sourceMappingURL=module$node_modules$tiny_lru$dist$tiny_lru_cjs.js.map
|