//#region src/infra/rate-limit/keyed-store.ts
var KeyedStore = class {
	map = /* @__PURE__ */ new Map();
	clock;
	staleAfterMs;
	cleanupTimer;
	constructor(opts) {
		this.clock = opts.clock ?? Date.now;
		this.staleAfterMs = Math.max(1e3, opts.staleAfterMs);
		const interval = Math.min(600 * 1e3, Math.max(1e3, opts.cleanupIntervalMs ?? Math.floor(this.staleAfterMs / 4)));
		this.cleanupTimer = setInterval(() => this.sweep(), interval);
		this.cleanupTimer.unref?.();
	}
	get(key) {
		return this.map.get(key);
	}
	set(key, value) {
		this.map.set(key, value);
	}
	delete(key) {
		this.map.delete(key);
	}
	size() {
		return this.map.size;
	}
	clear() {
		this.map.clear();
	}
	destroy() {
		if (this.cleanupTimer) {
			clearInterval(this.cleanupTimer);
			this.cleanupTimer = void 0;
		}
		this.map.clear();
	}
	sweep() {
		const now = this.clock();
		for (const [k, v] of this.map.entries()) if (now - v.lastTouchedMs > this.staleAfterMs) this.map.delete(k);
	}
};
//#endregion
export { KeyedStore };

//# sourceMappingURL=keyed-store.js.map