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