@neezco/cache 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1287 @@
1
+ import fs from "fs";
2
+ import v8 from "v8";
3
+ import { performance } from "perf_hooks";
4
+
5
+ //#region src/cache/clear.ts
6
+ /**
7
+ * Clears all entries from the cache without invoking callbacks.
8
+ *
9
+ * @note The `onDelete` callback is NOT invoked during a clear operation.
10
+ * This is intentional to avoid unnecessary overhead when bulk-removing entries.
11
+ *
12
+ * @param state - The cache state.
13
+ * @returns void
14
+ */
15
+ const clear = (state) => {
16
+ state.store.clear();
17
+ };
18
+
19
+ //#endregion
20
+ //#region src/defaults.ts
21
+ const ONE_SECOND = 1e3;
22
+ const ONE_MINUTE = 60 * ONE_SECOND;
23
+ /**
24
+ * ===================================================================
25
+ * Cache Entry Lifecycle
26
+ * Default TTL and stale window settings for short-lived cache entries.
27
+ * ===================================================================
28
+ */
29
+ /**
30
+ * Default Time-To-Live in milliseconds for cache entries.
31
+ * @default 1_800_000 (30 minutes)
32
+ */
33
+ const DEFAULT_TTL = 30 * ONE_MINUTE;
34
+ /**
35
+ * Maximum number of entries the cache can hold.
36
+ * Beyond this limit, new entries are ignored.
37
+ */
38
+ const DEFAULT_MAX_SIZE = Infinity;
39
+ /**
40
+ * Default maximum memory size in MB the cache can use.
41
+ * Beyond this limit, new entries are ignored.
42
+ * @default Infinite (unlimited)
43
+ */
44
+ const DEFAULT_MAX_MEMORY_SIZE = Infinity;
45
+ /**
46
+ * ===================================================================
47
+ * Sweep & Cleanup Operations
48
+ * Parameters controlling how and when expired entries are removed.
49
+ * ===================================================================
50
+ */
51
+ /**
52
+ * Maximum number of keys to process in a single sweep batch.
53
+ * Higher values = more aggressive cleanup, lower latency overhead.
54
+ */
55
+ const MAX_KEYS_PER_BATCH = 1e3;
56
+ /**
57
+ * Minimal expired ratio enforced during sweeps.
58
+ */
59
+ const MINIMAL_EXPIRED_RATIO = .05;
60
+ /**
61
+ * Maximum allowed expired ratio when memory usage is low.
62
+ * Upper bound for interpolation with MINIMAL_EXPIRED_RATIO.
63
+ * Recommended range: `0.3 – 0.5` .
64
+ */
65
+ const DEFAULT_MAX_EXPIRED_RATIO = .4;
66
+ /**
67
+ * ===================================================================
68
+ * Sweep Intervals & Timing
69
+ * Frequency and time budgets for cleanup operations.
70
+ * ===================================================================
71
+ */
72
+ /**
73
+ * Optimal interval in milliseconds between sweeps.
74
+ * Used when system load is minimal and metrics are available.
75
+ */
76
+ const OPTIMAL_SWEEP_INTERVAL = 2 * ONE_SECOND;
77
+ /**
78
+ * ===================================================================
79
+ * Memory Management
80
+ * Process limits and memory-safe thresholds.
81
+ * ===================================================================
82
+ */
83
+ /**
84
+ * Default maximum process memory limit in megabytes.
85
+ * Acts as fallback when environment detection is unavailable.
86
+ * NOTE: Overridable via environment detection at runtime.
87
+ */
88
+ const DEFAULT_MAX_PROCESS_MEMORY_MB = 1024;
89
+ /**
90
+ * Weight applied to CPU utilization in sweep calculations.
91
+ * Combined with event-loop weight to balance CPU-related pressure.
92
+ */
93
+ const DEFAULT_CPU_WEIGHT = 8.5;
94
+ /**
95
+ * Weight applied to event-loop utilization in sweep calculations.
96
+ * Complements CPU weight to assess overall processing capacity.
97
+ */
98
+ const DEFAULT_LOOP_WEIGHT = 6.5;
99
+ /**
100
+ * Default threshold for purging stale entries on get operations (backend with limits).
101
+ * Stale entries are purged when resource usage exceeds 80%.
102
+ *
103
+ * Note: This is used when limits are configured.
104
+ * When no limits are defined, purgeStaleOnGet defaults to false.
105
+ */
106
+ const DEFAULT_PURGE_STALE_ON_GET_THRESHOLD = .8;
107
+ /**
108
+ * Default threshold for purging stale entries during sweep operations (backend with limits).
109
+ * Stale entries are purged when resource usage exceeds 50%.
110
+ *
111
+ * Note: This is used when limits are configured.
112
+ * When no limits are defined, purgeStaleOnSweep defaults to true.
113
+ */
114
+ const DEFAULT_PURGE_STALE_ON_SWEEP_THRESHOLD = .5;
115
+
116
+ //#endregion
117
+ //#region src/resolve-purge-config/validators.ts
118
+ /**
119
+ * Validates if a numeric value is a valid positive limit.
120
+ * @internal
121
+ */
122
+ const isValidLimit = (value) => Number.isFinite(value) && value > 0;
123
+ /**
124
+ * Checks if the required limits are configured for the given metric.
125
+ * @internal
126
+ */
127
+ const checkRequiredLimits = (metric, limitStatus) => {
128
+ if (metric === "fixed") return false;
129
+ if (metric === "size") return limitStatus.hasSizeLimit;
130
+ if (metric === "memory") return limitStatus.hasMemoryLimit;
131
+ if (metric === "higher") return limitStatus.hasSizeLimit && limitStatus.hasMemoryLimit;
132
+ return false;
133
+ };
134
+
135
+ //#endregion
136
+ //#region src/resolve-purge-config/formatters.ts
137
+ /**
138
+ * Gets the requirement text for a metric when limits are missing.
139
+ * @internal
140
+ */
141
+ const getLimitRequirementText = (metric) => {
142
+ if (metric === "fixed") return "Numeric thresholds are not supported (metric is 'fixed')";
143
+ if (metric === "size") return "'maxSize' must be a valid positive number";
144
+ if (metric === "memory") return "'maxMemorySize' must be a valid positive number";
145
+ if (metric === "higher") return "both 'maxSize' and 'maxMemorySize' must be valid positive numbers";
146
+ return "required configuration";
147
+ };
148
+ /**
149
+ * Formats a purge mode value for display.
150
+ * @internal
151
+ */
152
+ const formatPurgeValue = (mode) => {
153
+ if (typeof mode === "number") return `threshold ${(mode * 100).toFixed(0)}%`;
154
+ return `${mode}`;
155
+ };
156
+
157
+ //#endregion
158
+ //#region src/resolve-purge-config/warnings.ts
159
+ /**
160
+ * Warns user about invalid purge configuration.
161
+ * Only called when user-provided threshold value is invalid.
162
+ *
163
+ * @internal
164
+ */
165
+ const warnInvalidPurgeMode = (config, invalidConditions) => {
166
+ if (invalidConditions.isOutOfRange) {
167
+ console.warn(`[Cache] ${config.operation}: Set to ${formatPurgeValue(config.mode)} with purgeResourceMetric '${config.metric}'.\n ⚠ Invalid: Numeric threshold must be between 0 (exclusive) and 1 (inclusive).\n ✓ Fallback: ${config.operation} = ${formatPurgeValue(config.fallback)}, purgeResourceMetric = '${config.metric}'`);
168
+ return;
169
+ }
170
+ if (invalidConditions.isIncompatibleWithMetric) {
171
+ console.warn(`[Cache] ${config.operation}: Set to ${formatPurgeValue(config.mode)} with purgeResourceMetric '${config.metric}'.\n ⚠ Not supported: Numeric thresholds don't work with purgeResourceMetric 'fixed'.\n ✓ Fallback: ${config.operation} = ${formatPurgeValue(config.fallback)}, purgeResourceMetric = '${config.metric}'`);
172
+ return;
173
+ }
174
+ if (invalidConditions.isMissingLimits) {
175
+ const requirement = getLimitRequirementText(config.metric);
176
+ console.warn(`[Cache] ${config.operation}: Set to ${formatPurgeValue(config.mode)} with purgeResourceMetric '${config.metric}'.\n ⚠ Not supported: ${requirement}\n ✓ Fallback: ${config.operation} = ${formatPurgeValue(config.fallback)}, purgeResourceMetric = '${config.metric}'`);
177
+ }
178
+ };
179
+
180
+ //#endregion
181
+ //#region src/resolve-purge-config/core.ts
182
+ /**
183
+ * Generic purge mode resolver that handles both get and sweep operations.
184
+ *
185
+ * Resolves valid user values or returns appropriate defaults based on:
186
+ * - Available configuration limits (maxSize, maxMemorySize)
187
+ * - Purge resource metric support (size, memory, higher, fixed)
188
+ * - User-provided threshold validity (0 < value ≤ 1)
189
+ *
190
+ * Behavior:
191
+ * - Boolean values (true/false): always valid, returns as-is
192
+ * - Numeric thresholds (0-1): validated against 3 conditions:
193
+ * 1. Range validation: must be 0 < value ≤ 1
194
+ * 2. Metric compatibility: metric must support thresholds (not 'fixed')
195
+ * 3. Configuration requirement: metric's required limits must be set
196
+ * - Invalid numerics: logs warning and returns configuration default
197
+ *
198
+ * Defaults:
199
+ * - With required limits: threshold-based (0.80 for get, 0.5 for sweep)
200
+ * - Without required limits: boolean (false for get, true for sweep)
201
+ *
202
+ * @internal
203
+ */
204
+ const resolvePurgeMode = (limits, config, defaults, userValue) => {
205
+ const hasSizeLimit = isValidLimit(limits.maxSize);
206
+ const hasMemoryLimit = isValidLimit(limits.maxMemorySize);
207
+ const hasRequiredLimits = checkRequiredLimits(config.purgeResourceMetric, {
208
+ hasSizeLimit,
209
+ hasMemoryLimit
210
+ });
211
+ const fallback = hasRequiredLimits ? defaults.withLimits : defaults.withoutLimits;
212
+ if (userValue !== void 0) {
213
+ const isNumeric = typeof userValue === "number";
214
+ const isOutOfRange = isNumeric && (userValue <= 0 || userValue > 1);
215
+ const isIncompatibleWithMetric = isNumeric && config.purgeResourceMetric === "fixed";
216
+ const isMissingLimits = isNumeric && !hasRequiredLimits;
217
+ if (isOutOfRange || isIncompatibleWithMetric || isMissingLimits) {
218
+ warnInvalidPurgeMode({
219
+ mode: userValue,
220
+ metric: config.purgeResourceMetric,
221
+ operation: config.operation,
222
+ fallback
223
+ }, {
224
+ isOutOfRange,
225
+ isIncompatibleWithMetric,
226
+ isMissingLimits
227
+ });
228
+ return fallback;
229
+ }
230
+ return userValue;
231
+ }
232
+ return fallback;
233
+ };
234
+
235
+ //#endregion
236
+ //#region src/resolve-purge-config/get.ts
237
+ /**
238
+ * Resolves the purgeStaleOnGet mode based on available configuration.
239
+ *
240
+ * Returns:
241
+ * - User value if valid (boolean always valid; numeric must satisfy all conditions)
242
+ * - Configuration default if user value is invalid
243
+ *
244
+ * Validation for numeric user values (0-1 thresholds):
245
+ * - Must be in range: 0 < value ≤ 1
246
+ * - Metric must support thresholds: not 'fixed'
247
+ * - Metric must have required limits: 'size' needs maxSize, 'memory' needs maxMemorySize, 'higher' needs both
248
+ *
249
+ * Configuration defaults:
250
+ * - With limits matching metric: 0.80 (80% purge threshold)
251
+ * - Without matching limits: false (preserve stale entries)
252
+ *
253
+ * @param config - Configuration with limits, purgeResourceMetric, and optional userValue
254
+ * @returns Valid purgeStaleOnGet value (boolean or threshold 0-1)
255
+ *
256
+ * @internal
257
+ */
258
+ const resolvePurgeStaleOnGet = (config) => resolvePurgeMode(config.limits, {
259
+ purgeResourceMetric: config.purgeResourceMetric,
260
+ operation: "purgeStaleOnGet"
261
+ }, {
262
+ withLimits: DEFAULT_PURGE_STALE_ON_GET_THRESHOLD,
263
+ withoutLimits: false
264
+ }, config.userValue);
265
+
266
+ //#endregion
267
+ //#region src/resolve-purge-config/metric.ts
268
+ /**
269
+ * Resolves the purge resource metric based on available limits and environment.
270
+ *
271
+ * - Browser: returns "size" if maxSize is valid, otherwise "fixed"
272
+ * - Backend with both maxSize and maxMemorySize: returns "higher"
273
+ * - Backend with only maxMemorySize: returns "memory"
274
+ * - Backend with only maxSize: returns "size"
275
+ * - Backend with no limits: returns "fixed"
276
+ *
277
+ * @param config - Configuration object with maxSize and maxMemorySize limits
278
+ * @returns The appropriate purge resource metric for this configuration
279
+ *
280
+ * @internal
281
+ */
282
+ const resolvePurgeResourceMetric = (config) => {
283
+ const limitStatus = {
284
+ hasSizeLimit: isValidLimit(config.maxSize),
285
+ hasMemoryLimit: isValidLimit(config.maxMemorySize)
286
+ };
287
+ if (limitStatus.hasSizeLimit && limitStatus.hasMemoryLimit) return "higher";
288
+ if (limitStatus.hasMemoryLimit) return "memory";
289
+ if (limitStatus.hasSizeLimit) return "size";
290
+ return "fixed";
291
+ };
292
+
293
+ //#endregion
294
+ //#region src/resolve-purge-config/sweep.ts
295
+ /**
296
+ * Resolves the purgeStaleOnSweep mode based on available configuration.
297
+ *
298
+ * Returns:
299
+ * - User value if valid (boolean always valid; numeric must satisfy all conditions)
300
+ * - Configuration default if user value is invalid
301
+ *
302
+ * Validation for numeric user values (0-1 thresholds):
303
+ * - Must be in range: 0 < value ≤ 1
304
+ * - Metric must support thresholds: not 'fixed'
305
+ * - Metric must have required limits: 'size' needs maxSize, 'memory' needs maxMemorySize, 'higher' needs both
306
+ *
307
+ * Configuration defaults:
308
+ * - With limits matching metric: 0.5 (50% purge threshold)
309
+ * - Without matching limits: true (always purge to prevent unbounded growth)
310
+ *
311
+ * @param config - Configuration with limits, purgeResourceMetric, and optional userValue
312
+ * @returns Valid purgeStaleOnSweep value (boolean or threshold 0-1)
313
+ *
314
+ * @internal
315
+ */
316
+ const resolvePurgeStaleOnSweep = (config) => resolvePurgeMode(config.limits, {
317
+ purgeResourceMetric: config.purgeResourceMetric,
318
+ operation: "purgeStaleOnSweep"
319
+ }, {
320
+ withLimits: DEFAULT_PURGE_STALE_ON_SWEEP_THRESHOLD,
321
+ withoutLimits: true
322
+ }, config.userValue);
323
+
324
+ //#endregion
325
+ //#region src/utils/get-process-memory-limit.ts
326
+ /**
327
+ * Reads a number from a file.
328
+ * @param path File path to read the number from.
329
+ * @returns The number read from the file, or null if reading fails.
330
+ */
331
+ function readNumber(path) {
332
+ try {
333
+ const raw = fs.readFileSync(path, "utf8").trim();
334
+ const n = Number(raw);
335
+ return Number.isFinite(n) ? n : null;
336
+ } catch {
337
+ return null;
338
+ }
339
+ }
340
+ /**
341
+ * Gets the memory limit imposed by cgroups, if any.
342
+ * @return The memory limit in bytes, or null if no limit is found.
343
+ */
344
+ function getCgroupLimit() {
345
+ const v2 = readNumber("/sys/fs/cgroup/memory.max");
346
+ if (v2 !== null) return v2;
347
+ const v1 = readNumber("/sys/fs/cgroup/memory/memory.limit_in_bytes");
348
+ if (v1 !== null) return v1;
349
+ return null;
350
+ }
351
+ /**
352
+ * Gets the effective memory limit for the current process, considering both V8 heap limits and cgroup limits.
353
+ * @returns The effective memory limit in bytes.
354
+ */
355
+ function getProcessMemoryLimit() {
356
+ const heapLimit = v8.getHeapStatistics().heap_size_limit;
357
+ const cgroupLimit = getCgroupLimit();
358
+ if (cgroupLimit && cgroupLimit > 0 && cgroupLimit < Infinity) return Math.min(heapLimit, cgroupLimit);
359
+ return heapLimit;
360
+ }
361
+
362
+ //#endregion
363
+ //#region src/utils/process-monitor.ts
364
+ /**
365
+ * Creates a performance monitor that periodically samples memory usage,
366
+ * CPU usage, and event loop utilization for the current Node.js process.
367
+ *
368
+ * The monitor runs on a configurable interval and optionally invokes a
369
+ * callback with the collected metrics on each cycle. It also exposes
370
+ * methods to start and stop monitoring, retrieve the latest metrics,
371
+ * and update configuration dynamically.
372
+ *
373
+ * @param options Configuration options for the monitor, including sampling
374
+ * interval, maximum thresholds for normalization, and an optional callback.
375
+ * @returns An API object that allows controlling the monitor lifecycle.
376
+ */
377
+ function createMonitorObserver(options) {
378
+ let intervalId = null;
379
+ let lastMetrics = null;
380
+ let prevHrtime = process.hrtime.bigint();
381
+ let prevMem = process.memoryUsage();
382
+ let prevCpu = process.cpuUsage();
383
+ let prevLoop = performance.eventLoopUtilization();
384
+ let lastCollectedAt = Date.now();
385
+ const config = {
386
+ interval: options?.interval ?? 500,
387
+ maxMemory: (options?.maxMemory ?? 512) * 1024 * 1024
388
+ };
389
+ function start() {
390
+ if (intervalId) return;
391
+ intervalId = setInterval(() => {
392
+ try {
393
+ const now = Date.now();
394
+ const metrics = collectMetrics({
395
+ prevCpu,
396
+ prevHrtime,
397
+ prevMem,
398
+ prevLoop,
399
+ maxMemory: config.maxMemory,
400
+ collectedAtMs: now,
401
+ previousCollectedAtMs: lastCollectedAt,
402
+ interval: config.interval
403
+ });
404
+ lastMetrics = metrics;
405
+ options?.callback?.(metrics);
406
+ prevCpu = metrics.cpu.total;
407
+ prevLoop = metrics.loop.total;
408
+ prevMem = metrics.memory.total;
409
+ prevHrtime = process.hrtime.bigint();
410
+ lastCollectedAt = now;
411
+ } catch (e) {
412
+ stop();
413
+ throw new Error("MonitorObserver: Not available", { cause: e });
414
+ }
415
+ }, config.interval);
416
+ if (typeof intervalId.unref === "function") intervalId.unref();
417
+ }
418
+ function stop() {
419
+ if (intervalId) {
420
+ clearInterval(intervalId);
421
+ intervalId = null;
422
+ }
423
+ }
424
+ function getMetrics() {
425
+ if (lastMetrics) return lastMetrics;
426
+ return null;
427
+ }
428
+ function updateConfig(newConfig) {
429
+ if (newConfig.maxMemory !== void 0) config.maxMemory = newConfig.maxMemory * 1024 * 1024;
430
+ if (newConfig.interval !== void 0) {
431
+ config.interval = newConfig.interval;
432
+ if (intervalId) {
433
+ stop();
434
+ start();
435
+ }
436
+ }
437
+ }
438
+ return {
439
+ start,
440
+ stop,
441
+ getMetrics,
442
+ updateConfig
443
+ };
444
+ }
445
+ /**
446
+ * Collects and normalizes performance metrics for the current process,
447
+ * including memory usage, CPU usage, and event loop utilization.
448
+ *
449
+ * CPU and event loop metrics are computed as deltas relative to previously
450
+ * recorded values. All metrics are normalized into a utilization between 0 and 1
451
+ * based on the configured maximum thresholds.
452
+ *
453
+ * @param props Previous metric snapshots and normalization limits.
454
+ * @returns A structured object containing normalized performance metrics.
455
+ */
456
+ function collectMetrics(props) {
457
+ const nowHrtime = process.hrtime.bigint();
458
+ const elapsedMs = Number(nowHrtime - props.prevHrtime) / 1e6;
459
+ const actualElapsed = props.collectedAtMs - props.previousCollectedAtMs;
460
+ const mem = process.memoryUsage();
461
+ const deltaMem = {
462
+ rss: mem.rss - props.prevMem.rss,
463
+ heapTotal: mem.heapTotal - props.prevMem.heapTotal,
464
+ heapUsed: mem.heapUsed - props.prevMem.heapUsed,
465
+ external: mem.external - props.prevMem.external,
466
+ arrayBuffers: mem.arrayBuffers - props.prevMem.arrayBuffers
467
+ };
468
+ const memRatio = Math.min(1, mem.rss / props.maxMemory);
469
+ const cpuDelta = process.cpuUsage(props.prevCpu);
470
+ const cpuRatio = (cpuDelta.system + cpuDelta.user) / 1e3 / elapsedMs;
471
+ const loop = performance.eventLoopUtilization(props.prevLoop);
472
+ return {
473
+ cpu: {
474
+ utilization: cpuRatio,
475
+ delta: cpuDelta,
476
+ total: process.cpuUsage()
477
+ },
478
+ loop: {
479
+ utilization: loop.utilization,
480
+ delta: loop,
481
+ total: performance.eventLoopUtilization()
482
+ },
483
+ memory: {
484
+ utilization: memRatio,
485
+ delta: deltaMem,
486
+ total: mem
487
+ },
488
+ collectedAt: props.collectedAtMs,
489
+ previousCollectedAt: props.previousCollectedAtMs,
490
+ interval: props.interval,
491
+ actualElapsed
492
+ };
493
+ }
494
+
495
+ //#endregion
496
+ //#region src/utils/start-monitor.ts
497
+ let _monitorInstance = null;
498
+ /** Latest collected metrics from the monitor */
499
+ let _metrics;
500
+ /** Maximum memory limit for the monitor (in MB) */
501
+ let maxMemoryLimit = DEFAULT_MAX_PROCESS_MEMORY_MB;
502
+ function startMonitor() {
503
+ if (!_monitorInstance) {
504
+ try {
505
+ const processMemoryLimit = getProcessMemoryLimit();
506
+ if (processMemoryLimit) maxMemoryLimit = processMemoryLimit / 1024 / 1024;
507
+ } catch {}
508
+ _monitorInstance = createMonitorObserver({
509
+ callback(metrics) {
510
+ _metrics = metrics;
511
+ },
512
+ interval: 200,
513
+ maxMemory: maxMemoryLimit
514
+ });
515
+ _monitorInstance.start();
516
+ }
517
+ }
518
+
519
+ //#endregion
520
+ //#region src/sweep/batchUpdateExpiredRatio.ts
521
+ /**
522
+ * Updates the expired ratio for each cache instance based on the collected ratios.
523
+ * @param currentExpiredRatios - An array of arrays containing expired ratios for each cache instance.
524
+ * @internal
525
+ */
526
+ function _batchUpdateExpiredRatio(currentExpiredRatios) {
527
+ for (const inst of _instancesCache) {
528
+ const ratios = currentExpiredRatios[inst._instanceIndexState];
529
+ if (ratios && ratios.length > 0) {
530
+ const avgRatio = ratios.reduce((sum, val) => sum + val, 0) / ratios.length;
531
+ const alpha = .6;
532
+ inst._expiredRatio = inst._expiredRatio * (1 - alpha) + avgRatio * alpha;
533
+ }
534
+ }
535
+ }
536
+
537
+ //#endregion
538
+ //#region src/utils/interpolate.ts
539
+ /**
540
+ * Interpolates a value between two numeric ranges.
541
+ *
542
+ * Maps `value` from [fromStart, fromEnd] to [toStart, toEnd].
543
+ * Works with inverted ranges, negative values, and any numeric input.
544
+ */
545
+ function interpolate({ value, fromStart, fromEnd, toStart, toEnd }) {
546
+ if (fromStart === fromEnd) return toStart;
547
+ return toStart + (value - fromStart) / (fromEnd - fromStart) * (toEnd - toStart);
548
+ }
549
+
550
+ //#endregion
551
+ //#region src/sweep/calculate-optimal-sweep-params.ts
552
+ /**
553
+ * Calculates adaptive sweep parameters based on real-time system utilization.
554
+ *
555
+ * Memory utilization is used as-is: higher memory usage → more conservative sweeps.
556
+ * CPU and event loop utilization are inverted: lower usage → more conservative sweeps.
557
+ *
558
+ * This inversion ensures:
559
+ * - When CPU and loop are *free*, sweeping becomes more aggressive (worst-case behavior).
560
+ * - When CPU and loop are *busy*, sweeping becomes more conservative (optimal behavior).
561
+ *
562
+ * The final ratio is a weighted average of the three metrics, clamped to [0, 1].
563
+ * This ratio is then used to interpolate between optimal and worst-case sweep settings.
564
+ *
565
+ * @param options - Optional configuration for weights and sweep bounds.
566
+ * @returns Interpolated sweep interval, time budget, and the ratio used.
567
+ */
568
+ const calculateOptimalSweepParams = (options) => {
569
+ const { metrics, weights = {}, optimalSweepIntervalMs = OPTIMAL_SWEEP_INTERVAL, worstSweepIntervalMs = 200, worstSweepTimeBudgetMs = 40 } = options;
570
+ const memoryWeight = weights.memory ?? 10;
571
+ const cpuWeight = weights.cpu ?? 8.5;
572
+ const loopWeight = weights.loop ?? 6.5;
573
+ const memoryUtilization = metrics?.memory.utilization ?? 0;
574
+ const cpuUtilizationRaw = metrics?.cpu.utilization ?? 0;
575
+ const loopUtilizationRaw = metrics?.loop.utilization ?? 0;
576
+ const cpuUtilization = 1 - cpuUtilizationRaw;
577
+ const loopUtilization = 1 - loopUtilizationRaw;
578
+ const weightedSum = memoryUtilization * memoryWeight + cpuUtilization * cpuWeight + loopUtilization * loopWeight;
579
+ const totalWeight = memoryWeight + cpuWeight + loopWeight;
580
+ const ratio = Math.min(1, Math.max(0, weightedSum / totalWeight));
581
+ return {
582
+ sweepIntervalMs: interpolate({
583
+ value: ratio,
584
+ fromStart: 0,
585
+ fromEnd: 1,
586
+ toStart: optimalSweepIntervalMs,
587
+ toEnd: worstSweepIntervalMs
588
+ }),
589
+ sweepTimeBudgetMs: interpolate({
590
+ value: ratio,
591
+ fromStart: 0,
592
+ fromEnd: 1,
593
+ toStart: 0,
594
+ toEnd: worstSweepTimeBudgetMs
595
+ })
596
+ };
597
+ };
598
+
599
+ //#endregion
600
+ //#region src/sweep/select-instance-to-sweep.ts
601
+ /**
602
+ * Selects a cache instance to sweep based on sweep weights or round‑robin order.
603
+ *
604
+ * Two selection modes are supported:
605
+ * - **Round‑robin mode**: If `totalSweepWeight` ≤ 0, instances are selected
606
+ * deterministically in sequence using `batchSweep`. Once all instances
607
+ * have been processed, returns `null`.
608
+ * - **Weighted mode**: If sweep weights are available, performs a probabilistic
609
+ * selection. Each instance’s `_sweepWeight` contributes proportionally to its
610
+ * chance of being chosen.
611
+ *
612
+ * This function depends on `_updateWeightSweep` to maintain accurate sweep weights.
613
+ *
614
+ * @param totalSweepWeight - Sum of all sweep weights across instances.
615
+ * @param batchSweep - Current batch index used for round‑robin selection.
616
+ * @returns The selected `CacheState` instance, `null` if no instance remains,
617
+ * or `undefined` if the cache is empty.
618
+ */
619
+ function _selectInstanceToSweep({ totalSweepWeight, batchSweep }) {
620
+ if (totalSweepWeight <= 0) {
621
+ if (batchSweep > _instancesCache.length) return null;
622
+ return _instancesCache[batchSweep - 1];
623
+ }
624
+ let threshold = Math.random() * totalSweepWeight;
625
+ for (const inst of _instancesCache) {
626
+ threshold -= inst._sweepWeight;
627
+ if (threshold <= 0) return inst;
628
+ }
629
+ return _instancesCache[0];
630
+ }
631
+
632
+ //#endregion
633
+ //#region src/cache/delete.ts
634
+ /**
635
+ * Deletes a key from the cache.
636
+ * @param state - The cache state.
637
+ * @param key - The key.
638
+ * @returns A boolean indicating whether the key was successfully deleted.
639
+ */
640
+ const deleteKey = (state, key, reason = "manual") => {
641
+ const onDelete = state.onDelete;
642
+ const onExpire = state.onExpire;
643
+ if (!onDelete && !onExpire) return state.store.delete(key);
644
+ const entry = state.store.get(key);
645
+ if (!entry) return false;
646
+ state.store.delete(key);
647
+ state.onDelete?.(key, entry[1], reason);
648
+ if (reason !== "manual") state.onExpire?.(key, entry[1], reason);
649
+ return true;
650
+ };
651
+
652
+ //#endregion
653
+ //#region src/types.ts
654
+ /**
655
+ * Entry status: fresh, stale, or expired.
656
+ */
657
+ let ENTRY_STATUS = /* @__PURE__ */ function(ENTRY_STATUS) {
658
+ /** Valid and within TTL. */
659
+ ENTRY_STATUS["FRESH"] = "fresh";
660
+ /** Expired but within stale window; still served. */
661
+ ENTRY_STATUS["STALE"] = "stale";
662
+ /** Beyond stale window; not served. */
663
+ ENTRY_STATUS["EXPIRED"] = "expired";
664
+ return ENTRY_STATUS;
665
+ }({});
666
+
667
+ //#endregion
668
+ //#region src/utils/status-from-tags.ts
669
+ /**
670
+ * Computes the derived status of a cache entry based on its associated tags.
671
+ *
672
+ * Tags may impose stricter expiration or stale rules on the entry. Only tags
673
+ * created at or after the entry's creation timestamp are considered relevant.
674
+ *
675
+ * Resolution rules:
676
+ * - If any applicable tag marks the entry as expired, the status becomes `EXPIRED`.
677
+ * - Otherwise, if any applicable tag marks it as stale, the status becomes `STALE`.
678
+ * - If no tag imposes stricter rules, the entry remains `FRESH`.
679
+ *
680
+ * @param state - The cache state containing tag metadata.
681
+ * @param entry - The cache entry whose status is being evaluated.
682
+ * @returns A tuple containing:
683
+ * - The final {@link ENTRY_STATUS} imposed by tags.
684
+ * - The earliest timestamp at which a tag marked the entry as stale
685
+ * (or 0 if no tag imposed a stale rule).
686
+ */
687
+ function _statusFromTags(state, entry) {
688
+ const entryCreatedAt = entry[0][0];
689
+ let earliestTagStaleInvalidation = Infinity;
690
+ let status = "fresh";
691
+ const tags = entry[2];
692
+ if (tags) for (const tag of tags) {
693
+ const ts = state._tags.get(tag);
694
+ if (!ts) continue;
695
+ const [tagExpiredAt, tagStaleSinceAt] = ts;
696
+ if (tagExpiredAt >= entryCreatedAt) {
697
+ status = "expired";
698
+ break;
699
+ }
700
+ if (tagStaleSinceAt >= entryCreatedAt) {
701
+ if (tagStaleSinceAt < earliestTagStaleInvalidation) earliestTagStaleInvalidation = tagStaleSinceAt;
702
+ status = "stale";
703
+ }
704
+ }
705
+ return [status, status === "stale" ? earliestTagStaleInvalidation : 0];
706
+ }
707
+
708
+ //#endregion
709
+ //#region src/cache/validators.ts
710
+ /**
711
+ * Computes the final derived status of a cache entry by combining:
712
+ *
713
+ * - The entry's own expiration timestamps (TTL and stale TTL).
714
+ * - Any stricter expiration or stale rules imposed by its associated tags.
715
+ *
716
+ * Precedence rules:
717
+ * - `EXPIRED` overrides everything.
718
+ * - `STALE` overrides `FRESH`.
719
+ * - If neither the entry nor its tags impose stricter rules, the entry is `FRESH`.
720
+ *
721
+ * @param state - The cache state containing tag metadata.
722
+ * @param entry - The cache entry being evaluated.
723
+ * @returns The final {@link ENTRY_STATUS} for the entry.
724
+ */
725
+ function computeEntryStatus(state, entry, now) {
726
+ const [__createdAt, expiresAt, staleExpiresAt] = entry[0];
727
+ const [tagStatus, earliestTagStaleInvalidation] = _statusFromTags(state, entry);
728
+ if (tagStatus === "expired") return "expired";
729
+ const windowStale = staleExpiresAt - expiresAt;
730
+ if (tagStatus === "stale" && staleExpiresAt > 0 && now < earliestTagStaleInvalidation + windowStale && now <= staleExpiresAt) return "stale";
731
+ if (now < expiresAt) return "fresh";
732
+ if (staleExpiresAt > 0 && now < staleExpiresAt) return "stale";
733
+ return "expired";
734
+ }
735
+ /**
736
+ * Determines whether a cache entry is fresh.
737
+ *
738
+ * A fresh entry is one whose final derived status is `FRESH`, meaning:
739
+ * - It has not expired according to its own timestamps, and
740
+ * - No associated tag imposes a stricter stale or expired rule.
741
+ *
742
+ * `entry` can be either a {@link CacheEntry} or a pre-computed {@link ENTRY_STATUS}.
743
+ * Passing a pre-computed status avoids recalculating the entry status.
744
+ *
745
+ * @param state - The cache state containing tag metadata.
746
+ * @param entry - The cache entry or pre-computed status being evaluated.
747
+ * @param now - The current timestamp.
748
+ * @returns True if the entry is fresh.
749
+ */
750
+ const isFresh = (state, entry, now) => {
751
+ if (typeof entry === "string") return entry === "fresh";
752
+ return computeEntryStatus(state, entry, now) === "fresh";
753
+ };
754
+ /**
755
+ * Determines whether a cache entry is stale.
756
+ *
757
+ * A stale entry is one whose final derived status is `STALE`, meaning:
758
+ * - It has passed its TTL but is still within its stale window, or
759
+ * - A tag imposes a stale rule that applies to this entry.
760
+ *
761
+ * `entry` can be either a {@link CacheEntry} or a pre-computed {@link ENTRY_STATUS}.
762
+ * Passing a pre-computed status avoids recalculating the entry status.
763
+ *
764
+ * @param state - The cache state containing tag metadata.
765
+ * @param entry - The cache entry or pre-computed status being evaluated.
766
+ * @param now - The current timestamp.
767
+ * @returns True if the entry is stale.
768
+ */
769
+ const isStale = (state, entry, now) => {
770
+ if (typeof entry === "string") return entry === "stale";
771
+ return computeEntryStatus(state, entry, now) === "stale";
772
+ };
773
+ /**
774
+ * Determines whether a cache entry is expired.
775
+ *
776
+ * An expired entry is one whose final derived status is `EXPIRED`, meaning:
777
+ * - It has exceeded both its TTL and stale TTL, or
778
+ * - A tag imposes an expiration rule that applies to this entry.
779
+ *
780
+ * `entry` can be either a {@link CacheEntry} or a pre-computed {@link ENTRY_STATUS}.
781
+ * Passing a pre-computed status avoids recalculating the entry status.
782
+ *
783
+ * @param state - The cache state containing tag metadata.
784
+ * @param entry - The cache entry or pre-computed status being evaluated.
785
+ * @param now - The current timestamp.
786
+ * @returns True if the entry is expired.
787
+ */
788
+ const isExpired = (state, entry, now) => {
789
+ if (typeof entry === "string") return entry === "expired";
790
+ return computeEntryStatus(state, entry, now) === "expired";
791
+ };
792
+
793
+ //#endregion
794
+ //#region src/utils/purge-eval.ts
795
+ /**
796
+ * Computes memory utilization as a normalized 0–1 value.
797
+ *
798
+ * In backend environments where metrics are available, returns the actual
799
+ * memory utilization from the monitor. In browser environments or when
800
+ * metrics are unavailable, returns 0.
801
+ *
802
+ * @returns Memory utilization in range [0, 1]
803
+ *
804
+ * @internal
805
+ */
806
+ const getMemoryUtilization = () => {
807
+ if (!_metrics) return 0;
808
+ return _metrics.memory?.utilization ?? 0;
809
+ };
810
+ /**
811
+ * Computes size utilization as a normalized 0–1 value.
812
+ *
813
+ * If maxSize is finite, returns `currentSize / maxSize`. Otherwise returns 0.
814
+ *
815
+ * @param state - The cache state
816
+ * @returns Size utilization in range [0, 1]
817
+ *
818
+ * @internal
819
+ */
820
+ const getSizeUtilization = (state) => {
821
+ if (!Number.isFinite(state.maxSize) || state.maxSize <= 0 || state.size <= 0) return 0;
822
+ return Math.min(1, state.size / state.maxSize);
823
+ };
824
+ /**
825
+ * Computes a 0–1 resource usage metric based on the configured purge metric.
826
+ *
827
+ * - `"size"`: Returns size utilization only.
828
+ * - `"memory"`: Returns memory utilization (backend only; returns 0 in browser).
829
+ * - `"higher"`: Returns the maximum of memory and size utilization.
830
+ *
831
+ * The result is always clamped to [0, 1].
832
+ *
833
+ * @param state - The cache state
834
+ * @returns Resource usage in range [0, 1]
835
+ *
836
+ * @internal
837
+ */
838
+ const computeResourceUsage = (state) => {
839
+ const metric = state.purgeResourceMetric;
840
+ if (!metric || metric === "fixed") return null;
841
+ if (metric === "size") return getSizeUtilization(state);
842
+ if (metric === "memory") return getMemoryUtilization();
843
+ if (metric === "higher") return Math.min(1, Math.max(getMemoryUtilization(), getSizeUtilization(state)));
844
+ return null;
845
+ };
846
+ /**
847
+ * Determines whether stale entries should be purged based on the purge mode and current resource usage.
848
+ *
849
+ * @param mode - The purge mode setting
850
+ * - `false` → never purge
851
+ * - `true` → always purge
852
+ * - `number (0–1)` → purge when `resourceUsage >= threshold`
853
+ * @param state - The cache state
854
+ * @returns True if stale entries should be purged, false otherwise
855
+ *
856
+ * @internal
857
+ */
858
+ const shouldPurge = (mode, state, purgeContext) => {
859
+ if (mode === false) return false;
860
+ if (mode === true) return true;
861
+ const userThreshold = Number(mode);
862
+ const defaultPurge = purgeContext === "sweep" ? true : false;
863
+ if (Number.isNaN(userThreshold)) return defaultPurge;
864
+ const usage = computeResourceUsage(state);
865
+ if (!usage) return defaultPurge;
866
+ return usage >= Math.max(0, Math.min(1, userThreshold));
867
+ };
868
+
869
+ //#endregion
870
+ //#region src/sweep/sweep-once.ts
871
+ /**
872
+ * Performs a single sweep operation on the cache to remove expired and optionally stale entries.
873
+ * Uses a linear scan with a saved pointer to resume from the last processed key.
874
+ * @param state - The cache state.
875
+ * @param _maxKeysPerBatch - Maximum number of keys to process in this sweep.
876
+ * @returns An object containing statistics about the sweep operation.
877
+ */
878
+ function _sweepOnce(state, _maxKeysPerBatch = MAX_KEYS_PER_BATCH) {
879
+ if (!state._sweepIter) state._sweepIter = state.store.entries();
880
+ const now = Date.now();
881
+ let processed = 0;
882
+ let expiredCount = 0;
883
+ let staleCount = 0;
884
+ const shouldPurgeStale = shouldPurge(state.purgeStaleOnSweep, state, "sweep");
885
+ for (let i = 0; i < _maxKeysPerBatch; i++) {
886
+ const next = state._sweepIter.next();
887
+ if (next.done) {
888
+ state._sweepIter = state.store.entries();
889
+ break;
890
+ }
891
+ processed += 1;
892
+ const [key, entry] = next.value;
893
+ const status = computeEntryStatus(state, entry, now);
894
+ if (isExpired(state, status, now)) {
895
+ deleteKey(state, key, "expired");
896
+ expiredCount += 1;
897
+ } else if (isStale(state, status, now)) {
898
+ staleCount += 1;
899
+ if (shouldPurgeStale) deleteKey(state, key, "stale");
900
+ }
901
+ }
902
+ return {
903
+ processed,
904
+ expiredCount,
905
+ staleCount,
906
+ ratio: processed > 0 ? (expiredCount + (shouldPurgeStale ? staleCount : 0)) / processed : 0
907
+ };
908
+ }
909
+
910
+ //#endregion
911
+ //#region src/sweep/calculate-optimal-max-expired-ratio.ts
912
+ /**
913
+ * Calculates the optimal maximum expired ratio based on current memory utilization.
914
+ *
915
+ * This function interpolates between `maxAllowExpiredRatio` and `MINIMAL_EXPIRED_RATIO`
916
+ * depending on the memory usage reported by `_metrics`. At low memory usage (0%),
917
+ * the optimal ratio equals `maxAllowExpiredRatio`. At high memory usage
918
+ * the optimal ratio decreases toward `MINIMAL_EXPIRED_RATIO`.
919
+ *
920
+ * @param maxAllowExpiredRatio - The maximum allowed expired ratio at minimal memory usage.
921
+ * Defaults to `DEFAULT_MAX_EXPIRED_RATIO`.
922
+ * @returns A normalized value between 0 and 1 representing the optimal expired ratio.
923
+ */
924
+ function calculateOptimalMaxExpiredRatio(maxAllowExpiredRatio = DEFAULT_MAX_EXPIRED_RATIO) {
925
+ const optimalExpiredRatio = interpolate({
926
+ value: _metrics?.memory.utilization ?? 0,
927
+ fromStart: 0,
928
+ fromEnd: 1,
929
+ toStart: maxAllowExpiredRatio,
930
+ toEnd: MINIMAL_EXPIRED_RATIO
931
+ });
932
+ return Math.min(1, Math.max(0, optimalExpiredRatio));
933
+ }
934
+
935
+ //#endregion
936
+ //#region src/sweep/update-weight.ts
937
+ /**
938
+ * Updates the sweep weight (`_sweepWeight`) for each cache instance.
939
+ *
940
+ * The sweep weight determines the probability that an instance will be selected
941
+ * for a cleanup (sweep) process. It is calculated based on the store size and
942
+ * the ratio of expired keys.
943
+ *
944
+ * This function complements (`_selectInstanceToSweep`), which is responsible
945
+ * for selecting the correct instance based on the weights assigned here.
946
+ *
947
+ * ### Sweep systems:
948
+ * 1. **Normal sweep**
949
+ * - Runs whenever the percentage of expired keys exceeds the allowed threshold
950
+ * calculated by `calculateOptimalMaxExpiredRatio`.
951
+ * - It is the main cleanup mechanism and is applied proportionally to the
952
+ * store size and the expired‑key ratio.
953
+ *
954
+ * 2. **Round‑robin sweep (minimal control)**
955
+ * - Always runs, even if the expired ratio is low or memory usage does not
956
+ * require it.
957
+ * - Processes a very small number of keys per instance, much smaller than
958
+ * the normal sweep.
959
+ * - Its main purpose is to ensure that all instances receive at least a
960
+ * periodic weight update and minimal expired‑key control.
961
+ *
962
+ * @returns The total accumulated sweep weight across all cache instances.
963
+ */
964
+ function _updateWeightSweep() {
965
+ let totalSweepWeight = 0;
966
+ for (const instCache of _instancesCache) {
967
+ if (instCache.store.size <= 0) {
968
+ instCache._sweepWeight = 0;
969
+ continue;
970
+ }
971
+ const expiredRatio = instCache._expiredRatio;
972
+ if (expiredRatio <= calculateOptimalMaxExpiredRatio(instCache._maxAllowExpiredRatio)) {
973
+ instCache._sweepWeight = 0;
974
+ continue;
975
+ }
976
+ instCache._sweepWeight = instCache.store.size * expiredRatio;
977
+ totalSweepWeight += instCache._sweepWeight;
978
+ }
979
+ return totalSweepWeight;
980
+ }
981
+
982
+ //#endregion
983
+ //#region src/sweep/sweep.ts
984
+ let _isSweepActive = false;
985
+ let _pendingSweepTimeout = null;
986
+ function startSweep(state) {
987
+ if (_isSweepActive) return;
988
+ _isSweepActive = true;
989
+ startMonitor();
990
+ sweep(state);
991
+ }
992
+ /**
993
+ * Performs a sweep operation on the cache to remove expired and optionally stale entries.
994
+ * Uses a linear scan with a saved pointer to resume from the last processed key.
995
+ * @param state - The cache state.
996
+ */
997
+ const sweep = async (state, utilities = {}) => {
998
+ const { schedule = defaultSchedule, yieldFn = defaultYieldFn, now = Date.now(), runOnlyOne = false } = utilities;
999
+ const startTime = now;
1000
+ let sweepIntervalMs = OPTIMAL_SWEEP_INTERVAL;
1001
+ let sweepTimeBudgetMs = 15;
1002
+ if (_metrics) ({sweepIntervalMs, sweepTimeBudgetMs} = calculateOptimalSweepParams({ metrics: _metrics }));
1003
+ const totalSweepWeight = _updateWeightSweep();
1004
+ const currentExpiredRatios = [];
1005
+ const maxKeysPerBatch = totalSweepWeight <= 0 ? MAX_KEYS_PER_BATCH / _instancesCache.length : MAX_KEYS_PER_BATCH;
1006
+ let batchSweep = 0;
1007
+ while (true) {
1008
+ batchSweep += 1;
1009
+ const instanceToSweep = _selectInstanceToSweep({
1010
+ batchSweep,
1011
+ totalSweepWeight
1012
+ });
1013
+ if (!instanceToSweep) break;
1014
+ const { ratio } = _sweepOnce(instanceToSweep, maxKeysPerBatch);
1015
+ (currentExpiredRatios[instanceToSweep._instanceIndexState] ??= []).push(ratio);
1016
+ if (Date.now() - startTime > sweepTimeBudgetMs) break;
1017
+ await yieldFn();
1018
+ }
1019
+ _batchUpdateExpiredRatio(currentExpiredRatios);
1020
+ if (!runOnlyOne) schedule(() => void sweep(state, utilities), sweepIntervalMs);
1021
+ };
1022
+ const defaultSchedule = (fn, ms) => {
1023
+ _pendingSweepTimeout = setTimeout(fn, ms);
1024
+ if (typeof _pendingSweepTimeout.unref === "function") _pendingSweepTimeout.unref();
1025
+ };
1026
+ const defaultYieldFn = () => new Promise((resolve) => setImmediate(resolve));
1027
+
1028
+ //#endregion
1029
+ //#region src/cache/create-cache.ts
1030
+ let _instanceCount = 0;
1031
+ const INSTANCE_WARNING_THRESHOLD = 99;
1032
+ const _instancesCache = [];
1033
+ /**
1034
+ * Creates the initial state for the TTL cache.
1035
+ * @param options - Configuration options for the cache.
1036
+ * @returns The initial cache state.
1037
+ */
1038
+ const createCache = (options = {}) => {
1039
+ const { onExpire, onDelete, defaultTtl = DEFAULT_TTL, maxSize = DEFAULT_MAX_SIZE, maxMemorySize = DEFAULT_MAX_MEMORY_SIZE, _maxAllowExpiredRatio = DEFAULT_MAX_EXPIRED_RATIO, defaultStaleWindow = 0, purgeStaleOnGet, purgeStaleOnSweep, purgeResourceMetric, _autoStartSweep = true } = options;
1040
+ _instanceCount++;
1041
+ if (_instanceCount > INSTANCE_WARNING_THRESHOLD) console.warn(`Too many instances detected (${_instanceCount}). This may indicate a configuration issue; consider minimizing instance creation or grouping keys by expected expiration ranges. See the documentation: https://github.com/neezco/cache/docs/getting-started.md`);
1042
+ const resolvedPurgeResourceMetric = purgeResourceMetric ?? resolvePurgeResourceMetric({
1043
+ maxSize,
1044
+ maxMemorySize
1045
+ });
1046
+ const resolvedPurgeStaleOnGet = resolvePurgeStaleOnGet({
1047
+ limits: {
1048
+ maxSize,
1049
+ maxMemorySize
1050
+ },
1051
+ purgeResourceMetric: resolvedPurgeResourceMetric,
1052
+ userValue: purgeStaleOnGet
1053
+ });
1054
+ const resolvedPurgeStaleOnSweep = resolvePurgeStaleOnSweep({
1055
+ limits: {
1056
+ maxSize,
1057
+ maxMemorySize
1058
+ },
1059
+ purgeResourceMetric: resolvedPurgeResourceMetric,
1060
+ userValue: purgeStaleOnSweep
1061
+ });
1062
+ const state = {
1063
+ store: /* @__PURE__ */ new Map(),
1064
+ _sweepIter: null,
1065
+ get size() {
1066
+ return state.store.size;
1067
+ },
1068
+ onExpire,
1069
+ onDelete,
1070
+ maxSize,
1071
+ maxMemorySize,
1072
+ defaultTtl,
1073
+ defaultStaleWindow,
1074
+ purgeStaleOnGet: resolvedPurgeStaleOnGet,
1075
+ purgeStaleOnSweep: resolvedPurgeStaleOnSweep,
1076
+ purgeResourceMetric: resolvedPurgeResourceMetric,
1077
+ _maxAllowExpiredRatio,
1078
+ _autoStartSweep,
1079
+ _instanceIndexState: -1,
1080
+ _expiredRatio: 0,
1081
+ _sweepWeight: 0,
1082
+ _tags: /* @__PURE__ */ new Map()
1083
+ };
1084
+ state._instanceIndexState = _instancesCache.push(state) - 1;
1085
+ startSweep(state);
1086
+ return state;
1087
+ };
1088
+
1089
+ //#endregion
1090
+ //#region src/cache/get.ts
1091
+ /**
1092
+ * Internal function that retrieves a value from the cache with its status information.
1093
+ * Returns a tuple containing the entry status and the complete cache entry.
1094
+ *
1095
+ * @param state - The cache state.
1096
+ * @param key - The key to retrieve.
1097
+ * @param now - Optional timestamp override (defaults to Date.now()).
1098
+ * @returns A tuple of [status, entry] if the entry is valid, or [null, undefined] if not found or expired.
1099
+ *
1100
+ * @internal
1101
+ */
1102
+ const getWithStatus = (state, key, purgeMode, now = Date.now()) => {
1103
+ const entry = state.store.get(key);
1104
+ if (!entry) return [null, void 0];
1105
+ const status = computeEntryStatus(state, entry, now);
1106
+ if (isFresh(state, status, now)) return [status, entry];
1107
+ if (isStale(state, status, now)) {
1108
+ if (shouldPurge(purgeMode ?? state.purgeStaleOnGet, state, "get")) deleteKey(state, key, "stale");
1109
+ return [status, entry];
1110
+ }
1111
+ deleteKey(state, key, "expired");
1112
+ return [status, void 0];
1113
+ };
1114
+ /**
1115
+ * Retrieves a value from the cache if the entry is valid.
1116
+ * @param state - The cache state.
1117
+ * @param key - The key to retrieve.
1118
+ * @param now - Optional timestamp override (defaults to Date.now()).
1119
+ * @returns The cached value if valid, undefined otherwise.
1120
+ *
1121
+ * @internal
1122
+ */
1123
+ const get = (state, key, purgeMode, now = Date.now()) => {
1124
+ const [, entry] = getWithStatus(state, key, purgeMode, now);
1125
+ return entry ? entry[1] : void 0;
1126
+ };
1127
+
1128
+ //#endregion
1129
+ //#region src/cache/has.ts
1130
+ /**
1131
+ * Checks if a key exists in the cache and is not expired.
1132
+ * @param state - The cache state.
1133
+ * @param key - The key to check.
1134
+ * @param now - Optional timestamp override (defaults to Date.now()).
1135
+ * @returns True if the key exists and is valid, false otherwise.
1136
+ */
1137
+ const has = (state, key, now = Date.now()) => {
1138
+ return get(state, key, now) !== void 0;
1139
+ };
1140
+
1141
+ //#endregion
1142
+ //#region src/cache/invalidate-tag.ts
1143
+ /**
1144
+ * Invalidates one or more tags so that entries associated with them
1145
+ * become expired or stale from this moment onward.
1146
+ *
1147
+ * Semantics:
1148
+ * - Each tag maintains two timestamps in `state._tags`:
1149
+ * [expiredAt, staleSinceAt].
1150
+ * - Calling this function updates one of those timestamps to `_now`,
1151
+ * depending on whether the tag should force expiration or staleness.
1152
+ *
1153
+ * Rules:
1154
+ * - If `asStale` is false (default), the tag forces expiration:
1155
+ * entries created before `_now` will be considered expired.
1156
+ * - If `asStale` is true, the tag forces staleness:
1157
+ * entries created before `_now` will be considered stale,
1158
+ * but only if they support a stale window.
1159
+ *
1160
+ * Behavior:
1161
+ * - Each call replaces any previous invalidation timestamp for the tag.
1162
+ * - Entries created after `_now` are unaffected.
1163
+ *
1164
+ * @param state - The cache state containing tag metadata.
1165
+ * @param tags - A tag or list of tags to invalidate.
1166
+ * @param options.asStale - Whether the tag should mark entries as stale.
1167
+ */
1168
+ function invalidateTag(state, tags, options = {}, _now = Date.now()) {
1169
+ const tagList = Array.isArray(tags) ? tags : [tags];
1170
+ const asStale = options.asStale ?? false;
1171
+ for (const tag of tagList) {
1172
+ const currentTag = state._tags.get(tag);
1173
+ if (currentTag) if (asStale) currentTag[1] = _now;
1174
+ else currentTag[0] = _now;
1175
+ else state._tags.set(tag, [asStale ? 0 : _now, asStale ? _now : 0]);
1176
+ }
1177
+ }
1178
+
1179
+ //#endregion
1180
+ //#region src/cache/set.ts
1181
+ /**
1182
+ * Sets or updates a value in the cache with TTL and an optional stale window.
1183
+ *
1184
+ * @param state - The cache state.
1185
+ * @param input - Cache entry definition (key, value, ttl, staleWindow, tags).
1186
+ * @param now - Optional timestamp override used as the base time (defaults to Date.now()).
1187
+ * @returns True if the entry was created or updated, false if rejected due to limits or invalid input.
1188
+ *
1189
+ * @remarks
1190
+ * - `ttl` defines when the entry becomes expired.
1191
+ * - `staleWindow` defines how long the entry may still be served as stale
1192
+ * after the expiration moment (`now + ttl`).
1193
+ * - Returns false if value is `undefined` (entry ignored, existing value untouched).
1194
+ * - Returns false if new entry would exceed `maxSize` limit (existing keys always allowed).
1195
+ * - Returns false if new entry would exceed `maxMemorySize` limit (existing keys always allowed).
1196
+ * - Returns true if entry was set or updated (or if existing key was updated at limit).
1197
+ */
1198
+ const setOrUpdate = (state, input, now = Date.now()) => {
1199
+ const { key, value, ttl: ttlInput, staleWindow: staleWindowInput, tags } = input;
1200
+ if (value === void 0) return false;
1201
+ if (key == null) throw new Error("Missing key.");
1202
+ if (state.size >= state.maxSize && !state.store.has(key)) return false;
1203
+ if (_metrics?.memory.total.rss && _metrics?.memory.total.rss >= state.maxMemorySize * 1024 * 1024 && !state.store.has(key)) return false;
1204
+ const ttl = ttlInput ?? state.defaultTtl;
1205
+ const staleWindow = staleWindowInput ?? state.defaultStaleWindow;
1206
+ const expiresAt = ttl > 0 ? now + ttl : Infinity;
1207
+ const entry = [
1208
+ [
1209
+ now,
1210
+ expiresAt,
1211
+ staleWindow > 0 ? expiresAt + staleWindow : 0
1212
+ ],
1213
+ value,
1214
+ typeof tags === "string" ? [tags] : Array.isArray(tags) ? tags : null
1215
+ ];
1216
+ state.store.set(key, entry);
1217
+ return true;
1218
+ };
1219
+
1220
+ //#endregion
1221
+ //#region src/index.ts
1222
+ /**
1223
+ * A TTL (Time-To-Live) cache implementation with support for expiration,
1224
+ * stale windows, tag-based invalidation, and smart automatic sweeping.
1225
+ *
1226
+ * Provides O(1) constant-time operations for all core methods with support for:
1227
+ * - Expiration and stale windows
1228
+ * - Tag-based invalidation
1229
+ * - Automatic sweeping
1230
+ */
1231
+ var LocalTtlCache = class {
1232
+ state;
1233
+ /**
1234
+ * Creates a new cache instance.
1235
+ *
1236
+ * @param options - Configuration options for the cache (defaultTtl, defaultStaleWindow, maxSize, etc.)
1237
+ *
1238
+ */
1239
+ constructor(options) {
1240
+ this.state = createCache(options);
1241
+ }
1242
+ get size() {
1243
+ return this.state.size;
1244
+ }
1245
+ get(key, options) {
1246
+ if (options?.includeMetadata === true) {
1247
+ const [status, entry] = getWithStatus(this.state, key, options.purgeStale);
1248
+ if (!entry) return void 0;
1249
+ const [timestamps, value, tags] = entry;
1250
+ const [createdAt, expiresAt, staleExpiresAt] = timestamps;
1251
+ return {
1252
+ data: value,
1253
+ createdAt,
1254
+ expirationTime: expiresAt,
1255
+ staleWindowExpiration: staleExpiresAt,
1256
+ status,
1257
+ tags
1258
+ };
1259
+ }
1260
+ return get(this.state, key, options?.purgeStale);
1261
+ }
1262
+ set(key, value, options) {
1263
+ return setOrUpdate(this.state, {
1264
+ key,
1265
+ value,
1266
+ ttl: options?.ttl,
1267
+ staleWindow: options?.staleWindow,
1268
+ tags: options?.tags
1269
+ });
1270
+ }
1271
+ delete(key) {
1272
+ return deleteKey(this.state, key);
1273
+ }
1274
+ has(key) {
1275
+ return has(this.state, key);
1276
+ }
1277
+ clear() {
1278
+ clear(this.state);
1279
+ }
1280
+ invalidateTag(tags, options) {
1281
+ invalidateTag(this.state, tags, options ?? {});
1282
+ }
1283
+ };
1284
+
1285
+ //#endregion
1286
+ export { ENTRY_STATUS, LocalTtlCache };
1287
+ //# sourceMappingURL=index.mjs.map