n8n
Version:
n8n Workflow Automation Tool
80 lines • 2.05 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TtlMap = void 0;
class TtlMap {
constructor(ttlMs, sweepIntervalMs = ttlMs) {
this.ttlMs = ttlMs;
this.store = new Map();
this.sweepTimer = null;
if (sweepIntervalMs > 0) {
this.sweepTimer = setInterval(() => this.sweep(), sweepIntervalMs).unref();
}
}
set(key, value) {
this.store.set(key, { value, expiresAt: Date.now() + this.ttlMs });
return this;
}
get(key) {
const entry = this.store.get(key);
if (!entry)
return undefined;
if (Date.now() > entry.expiresAt) {
this.store.delete(key);
return undefined;
}
return entry.value;
}
has(key) {
if (!this.store.has(key))
return false;
const expiresAt = this.store.get(key)?.expiresAt;
if (!expiresAt)
return false;
if (Date.now() > expiresAt) {
this.store.delete(key);
return false;
}
return true;
}
delete(key) {
return this.store.delete(key);
}
keys() {
this.sweep();
return this.store.keys();
}
*entries() {
const now = Date.now();
for (const [key, entry] of this.store) {
if (now <= entry.expiresAt) {
yield [key, entry.value];
}
}
}
[Symbol.iterator]() {
return this.entries();
}
get size() {
this.sweep();
return this.store.size;
}
clear() {
this.store.clear();
}
sweep() {
const now = Date.now();
for (const [key, entry] of this.store) {
if (now > entry.expiresAt) {
this.store.delete(key);
}
}
}
dispose() {
if (this.sweepTimer) {
clearInterval(this.sweepTimer);
this.sweepTimer = null;
}
}
}
exports.TtlMap = TtlMap;
//# sourceMappingURL=ttl-map.js.map