@neezco/cache 0.5.0 → 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,1030 +0,0 @@
1
- //#region src/cache/clear.ts
2
- /**
3
- * Clears all entries from the cache without invoking callbacks.
4
- *
5
- * @note The `onDelete` callback is NOT invoked during a clear operation.
6
- * This is intentional to avoid unnecessary overhead when bulk-removing entries.
7
- *
8
- * @param state - The cache state.
9
- * @returns void
10
- */
11
- const clear = (state) => {
12
- state.store.clear();
13
- };
14
-
15
- //#endregion
16
- //#region src/defaults.ts
17
- const ONE_SECOND = 1e3;
18
- const ONE_MINUTE = 60 * ONE_SECOND;
19
- /**
20
- * ===================================================================
21
- * Cache Entry Lifecycle
22
- * Default TTL and stale window settings for short-lived cache entries.
23
- * ===================================================================
24
- */
25
- /**
26
- * Default Time-To-Live in milliseconds for cache entries.
27
- * @default 1_800_000 (30 minutes)
28
- */
29
- const DEFAULT_TTL = 30 * ONE_MINUTE;
30
- /**
31
- * Default stale window in milliseconds after expiration.
32
- * Allows serving slightly outdated data while fetching fresh data.
33
- */
34
- const DEFAULT_STALE_WINDOW = 0;
35
- /**
36
- * Maximum number of entries the cache can hold.
37
- * Beyond this limit, new entries are ignored.
38
- */
39
- const DEFAULT_MAX_SIZE = Infinity;
40
- /**
41
- * Default maximum memory size in MB the cache can use.
42
- * Beyond this limit, new entries are ignored.
43
- * @default Infinite (unlimited)
44
- */
45
- const DEFAULT_MAX_MEMORY_SIZE = Infinity;
46
- /**
47
- * ===================================================================
48
- * Sweep & Cleanup Operations
49
- * Parameters controlling how and when expired entries are removed.
50
- * ===================================================================
51
- */
52
- /**
53
- * Maximum number of keys to process in a single sweep batch.
54
- * Higher values = more aggressive cleanup, lower latency overhead.
55
- */
56
- const MAX_KEYS_PER_BATCH = 1e3;
57
- /**
58
- * Minimal expired ratio enforced during sweeps.
59
- * Ensures control sweeps run above {@link EXPIRED_RATIO_MEMORY_THRESHOLD}.
60
- */
61
- const MINIMAL_EXPIRED_RATIO = .05;
62
- /**
63
- * Maximum allowed expired ratio when memory usage is low.
64
- * Upper bound for interpolation with MINIMAL_EXPIRED_RATIO.
65
- * Recommended range: `0.3 – 0.5` .
66
- */
67
- const DEFAULT_MAX_EXPIRED_RATIO = .4;
68
- /**
69
- * ===================================================================
70
- * Sweep Intervals & Timing
71
- * Frequency and time budgets for cleanup operations.
72
- * ===================================================================
73
- */
74
- /**
75
- * Optimal interval in milliseconds between sweeps.
76
- * Used when system load is minimal and metrics are available.
77
- */
78
- const OPTIMAL_SWEEP_INTERVAL = 2 * ONE_SECOND;
79
- /**
80
- * Optimal time budget in milliseconds for each sweep cycle.
81
- * Used when performance metrics are not available or unreliable.
82
- */
83
- const OPTIMAL_SWEEP_TIME_BUDGET_IF_NOTE_METRICS_AVAILABLE = 15;
84
- /**
85
- * Fallback behavior for stale purging on GET
86
- * when no resource limits are defined.
87
- *
88
- * In this scenario, threshold-based purging is disabled,
89
- * so GET operations do NOT purge stale entries.
90
- */
91
- const DEFAULT_PURGE_STALE_ON_GET_NO_LIMITS = false;
92
- /**
93
- * Fallback behavior for stale purging on SWEEP
94
- * when no resource limits are defined.
95
- *
96
- * In this scenario, threshold-based purging is disabled,
97
- * so SWEEP operations DO purge stale entries to prevent buildup.
98
- */
99
- const DEFAULT_PURGE_STALE_ON_SWEEP_NO_LIMITS = true;
100
- /**
101
- * Default threshold for purging stale entries on get operations (backend with limits).
102
- * Stale entries are purged when resource usage exceeds 80%.
103
- *
104
- * Note: This is used when limits are configured.
105
- * When no limits are defined, purgeStaleOnGet defaults to false.
106
- */
107
- const DEFAULT_PURGE_STALE_ON_GET_THRESHOLD = .8;
108
- /**
109
- * Default threshold for purging stale entries during sweep operations (backend with limits).
110
- * Stale entries are purged when resource usage exceeds 50%.
111
- *
112
- * Note: This is used when limits are configured.
113
- * When no limits are defined, purgeStaleOnSweep defaults to true.
114
- */
115
- const DEFAULT_PURGE_STALE_ON_SWEEP_THRESHOLD = .5;
116
-
117
- //#endregion
118
- //#region src/resolve-purge-config/validators.ts
119
- /**
120
- * Validates if a numeric value is a valid positive limit.
121
- * @internal
122
- */
123
- const isValidLimit = (value) => Number.isFinite(value) && value > 0;
124
- /**
125
- * Checks if the required limits are configured for the given metric.
126
- * @internal
127
- */
128
- const checkRequiredLimits = (metric, limitStatus) => {
129
- if (metric === "fixed") return false;
130
- if (metric === "size") return limitStatus.hasSizeLimit;
131
- if (metric === "memory") return limitStatus.hasMemoryLimit;
132
- if (metric === "higher") return limitStatus.hasSizeLimit && limitStatus.hasMemoryLimit;
133
- return false;
134
- };
135
-
136
- //#endregion
137
- //#region src/resolve-purge-config/formatters.ts
138
- /**
139
- * Gets the requirement text for a metric when limits are missing.
140
- * @internal
141
- */
142
- const getLimitRequirementText = (metric) => {
143
- if (metric === "fixed") return "Numeric thresholds are not supported (metric is 'fixed')";
144
- if (metric === "size") return "'maxSize' must be a valid positive number";
145
- if (metric === "memory") return "'maxMemorySize' must be a valid positive number";
146
- if (metric === "higher") return "both 'maxSize' and 'maxMemorySize' must be valid positive numbers";
147
- return "required configuration";
148
- };
149
- /**
150
- * Formats a purge mode value for display.
151
- * @internal
152
- */
153
- const formatPurgeValue = (mode) => {
154
- if (typeof mode === "number") return `threshold ${(mode * 100).toFixed(0)}%`;
155
- return `${mode}`;
156
- };
157
-
158
- //#endregion
159
- //#region src/resolve-purge-config/warnings.ts
160
- /**
161
- * Warns user about invalid purge configuration.
162
- * Only called when user-provided threshold value is invalid.
163
- *
164
- * @internal
165
- */
166
- const warnInvalidPurgeMode = (config, invalidConditions) => {
167
- if (invalidConditions.isOutOfRange) {
168
- 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}'`);
169
- return;
170
- }
171
- if (invalidConditions.isIncompatibleWithMetric) {
172
- 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}'`);
173
- return;
174
- }
175
- if (invalidConditions.isMissingLimits) {
176
- const requirement = getLimitRequirementText(config.metric);
177
- 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}'`);
178
- }
179
- };
180
-
181
- //#endregion
182
- //#region src/resolve-purge-config/core.ts
183
- /**
184
- * Generic purge mode resolver that handles both get and sweep operations.
185
- *
186
- * Resolves valid user values or returns appropriate defaults based on:
187
- * - Available configuration limits (maxSize, maxMemorySize)
188
- * - Purge resource metric support (size, memory, higher, fixed)
189
- * - User-provided threshold validity (0 < value ≤ 1)
190
- *
191
- * Behavior:
192
- * - Boolean values (true/false): always valid, returns as-is
193
- * - Numeric thresholds (0-1): validated against 3 conditions:
194
- * 1. Range validation: must be 0 < value ≤ 1
195
- * 2. Metric compatibility: metric must support thresholds (not 'fixed')
196
- * 3. Configuration requirement: metric's required limits must be set
197
- * - Invalid numerics: logs warning and returns configuration default
198
- *
199
- * Defaults:
200
- * - With required limits: threshold-based (0.80 for get, 0.5 for sweep)
201
- * - Without required limits: boolean (false for get, true for sweep)
202
- *
203
- * @internal
204
- */
205
- const resolvePurgeMode = (limits, config, defaults, userValue) => {
206
- const hasSizeLimit = isValidLimit(limits.maxSize);
207
- const hasMemoryLimit = isValidLimit(limits.maxMemorySize);
208
- const hasRequiredLimits = checkRequiredLimits(config.purgeResourceMetric, {
209
- hasSizeLimit,
210
- hasMemoryLimit
211
- });
212
- const fallback = hasRequiredLimits ? defaults.withLimits : defaults.withoutLimits;
213
- if (userValue !== void 0) {
214
- const isNumeric = typeof userValue === "number";
215
- const isOutOfRange = isNumeric && (userValue <= 0 || userValue > 1);
216
- const isIncompatibleWithMetric = isNumeric && config.purgeResourceMetric === "fixed";
217
- const isMissingLimits = isNumeric && !hasRequiredLimits;
218
- if (isOutOfRange || isIncompatibleWithMetric || isMissingLimits) {
219
- warnInvalidPurgeMode({
220
- mode: userValue,
221
- metric: config.purgeResourceMetric,
222
- operation: config.operation,
223
- fallback
224
- }, {
225
- isOutOfRange,
226
- isIncompatibleWithMetric,
227
- isMissingLimits
228
- });
229
- return fallback;
230
- }
231
- return userValue;
232
- }
233
- return fallback;
234
- };
235
-
236
- //#endregion
237
- //#region src/resolve-purge-config/get.ts
238
- /**
239
- * Resolves the purgeStaleOnGet mode based on available configuration.
240
- *
241
- * Returns:
242
- * - User value if valid (boolean always valid; numeric must satisfy all conditions)
243
- * - Configuration default if user value is invalid
244
- *
245
- * Validation for numeric user values (0-1 thresholds):
246
- * - Must be in range: 0 < value ≤ 1
247
- * - Metric must support thresholds: not 'fixed'
248
- * - Metric must have required limits: 'size' needs maxSize, 'memory' needs maxMemorySize, 'higher' needs both
249
- *
250
- * Configuration defaults:
251
- * - With limits matching metric: 0.80 (80% purge threshold)
252
- * - Without matching limits: false (preserve stale entries)
253
- *
254
- * @param config - Configuration with limits, purgeResourceMetric, and optional userValue
255
- * @returns Valid purgeStaleOnGet value (boolean or threshold 0-1)
256
- *
257
- * @internal
258
- */
259
- const resolvePurgeStaleOnGet = (config) => resolvePurgeMode(config.limits, {
260
- purgeResourceMetric: config.purgeResourceMetric,
261
- operation: "purgeStaleOnGet"
262
- }, {
263
- withLimits: DEFAULT_PURGE_STALE_ON_GET_THRESHOLD,
264
- withoutLimits: DEFAULT_PURGE_STALE_ON_GET_NO_LIMITS
265
- }, config.userValue);
266
-
267
- //#endregion
268
- //#region src/resolve-purge-config/metric.ts
269
- /**
270
- * Resolves the purge resource metric based on available limits and environment.
271
- *
272
- * - Browser: returns "size" if maxSize is valid, otherwise "fixed"
273
- * - Backend with both maxSize and maxMemorySize: returns "higher"
274
- * - Backend with only maxMemorySize: returns "memory"
275
- * - Backend with only maxSize: returns "size"
276
- * - Backend with no limits: returns "fixed"
277
- *
278
- * @param config - Configuration object with maxSize and maxMemorySize limits
279
- * @returns The appropriate purge resource metric for this configuration
280
- *
281
- * @internal
282
- */
283
- const resolvePurgeResourceMetric = (config) => {
284
- return {
285
- hasSizeLimit: isValidLimit(config.maxSize),
286
- hasMemoryLimit: isValidLimit(config.maxMemorySize)
287
- }.hasSizeLimit ? "size" : "fixed";
288
- };
289
-
290
- //#endregion
291
- //#region src/resolve-purge-config/sweep.ts
292
- /**
293
- * Resolves the purgeStaleOnSweep mode based on available configuration.
294
- *
295
- * Returns:
296
- * - User value if valid (boolean always valid; numeric must satisfy all conditions)
297
- * - Configuration default if user value is invalid
298
- *
299
- * Validation for numeric user values (0-1 thresholds):
300
- * - Must be in range: 0 < value ≤ 1
301
- * - Metric must support thresholds: not 'fixed'
302
- * - Metric must have required limits: 'size' needs maxSize, 'memory' needs maxMemorySize, 'higher' needs both
303
- *
304
- * Configuration defaults:
305
- * - With limits matching metric: 0.5 (50% purge threshold)
306
- * - Without matching limits: true (always purge to prevent unbounded growth)
307
- *
308
- * @param config - Configuration with limits, purgeResourceMetric, and optional userValue
309
- * @returns Valid purgeStaleOnSweep value (boolean or threshold 0-1)
310
- *
311
- * @internal
312
- */
313
- const resolvePurgeStaleOnSweep = (config) => resolvePurgeMode(config.limits, {
314
- purgeResourceMetric: config.purgeResourceMetric,
315
- operation: "purgeStaleOnSweep"
316
- }, {
317
- withLimits: DEFAULT_PURGE_STALE_ON_SWEEP_THRESHOLD,
318
- withoutLimits: true
319
- }, config.userValue);
320
-
321
- //#endregion
322
- //#region src/utils/start-monitor.ts
323
- function startMonitor() {}
324
-
325
- //#endregion
326
- //#region src/sweep/batchUpdateExpiredRatio.ts
327
- /**
328
- * Updates the expired ratio for each cache instance based on the collected ratios.
329
- * @param currentExpiredRatios - An array of arrays containing expired ratios for each cache instance.
330
- * @internal
331
- */
332
- function _batchUpdateExpiredRatio(currentExpiredRatios) {
333
- for (const inst of _instancesCache) {
334
- const ratios = currentExpiredRatios[inst._instanceIndexState];
335
- if (ratios && ratios.length > 0) {
336
- const avgRatio = ratios.reduce((sum, val) => sum + val, 0) / ratios.length;
337
- const alpha = .6;
338
- inst._expiredRatio = inst._expiredRatio * (1 - alpha) + avgRatio * alpha;
339
- }
340
- }
341
- }
342
-
343
- //#endregion
344
- //#region src/sweep/select-instance-to-sweep.ts
345
- /**
346
- * Selects a cache instance to sweep based on sweep weights or round‑robin order.
347
- *
348
- * Two selection modes are supported:
349
- * - **Round‑robin mode**: If `totalSweepWeight` ≤ 0, instances are selected
350
- * deterministically in sequence using `batchSweep`. Once all instances
351
- * have been processed, returns `null`.
352
- * - **Weighted mode**: If sweep weights are available, performs a probabilistic
353
- * selection. Each instance’s `_sweepWeight` contributes proportionally to its
354
- * chance of being chosen.
355
- *
356
- * This function depends on `_updateWeightSweep` to maintain accurate sweep weights.
357
- *
358
- * @param totalSweepWeight - Sum of all sweep weights across instances.
359
- * @param batchSweep - Current batch index used for round‑robin selection.
360
- * @returns The selected `CacheState` instance, `null` if no instance remains,
361
- * or `undefined` if the cache is empty.
362
- */
363
- function _selectInstanceToSweep({ totalSweepWeight, batchSweep }) {
364
- let instanceToSweep = _instancesCache[0];
365
- if (totalSweepWeight <= 0) {
366
- if (batchSweep > _instancesCache.length) instanceToSweep = null;
367
- instanceToSweep = _instancesCache[batchSweep - 1];
368
- } else {
369
- let threshold = Math.random() * totalSweepWeight;
370
- for (const inst of _instancesCache) {
371
- threshold -= inst._sweepWeight;
372
- if (threshold <= 0) {
373
- instanceToSweep = inst;
374
- break;
375
- }
376
- }
377
- }
378
- return instanceToSweep;
379
- }
380
-
381
- //#endregion
382
- //#region src/cache/delete.ts
383
- let DELETE_REASON = /* @__PURE__ */ function(DELETE_REASON$1) {
384
- DELETE_REASON$1["MANUAL"] = "manual";
385
- DELETE_REASON$1["EXPIRED"] = "expired";
386
- DELETE_REASON$1["STALE"] = "stale";
387
- return DELETE_REASON$1;
388
- }({});
389
- /**
390
- * Deletes a key from the cache.
391
- * @param state - The cache state.
392
- * @param key - The key.
393
- * @returns A boolean indicating whether the key was successfully deleted.
394
- */
395
- const deleteKey = (state, key, reason = DELETE_REASON.MANUAL) => {
396
- const onDelete = state.onDelete;
397
- const onExpire = state.onExpire;
398
- if (!onDelete && !onExpire) return state.store.delete(key);
399
- const entry = state.store.get(key);
400
- if (!entry) return false;
401
- state.store.delete(key);
402
- state.onDelete?.(key, entry[1], reason);
403
- if (reason !== DELETE_REASON.MANUAL) state.onExpire?.(key, entry[1], reason);
404
- return true;
405
- };
406
-
407
- //#endregion
408
- //#region src/types.ts
409
- /**
410
- * Entry status: fresh, stale, or expired.
411
- */
412
- let ENTRY_STATUS = /* @__PURE__ */ function(ENTRY_STATUS$1) {
413
- /** Valid and within TTL. */
414
- ENTRY_STATUS$1["FRESH"] = "fresh";
415
- /** Expired but within stale window; still served. */
416
- ENTRY_STATUS$1["STALE"] = "stale";
417
- /** Beyond stale window; not served. */
418
- ENTRY_STATUS$1["EXPIRED"] = "expired";
419
- return ENTRY_STATUS$1;
420
- }({});
421
-
422
- //#endregion
423
- //#region src/utils/status-from-tags.ts
424
- /**
425
- * Computes the derived status of a cache entry based on its associated tags.
426
- *
427
- * Tags may impose stricter expiration or stale rules on the entry. Only tags
428
- * created at or after the entry's creation timestamp are considered relevant.
429
- *
430
- * Resolution rules:
431
- * - If any applicable tag marks the entry as expired, the status becomes `EXPIRED`.
432
- * - Otherwise, if any applicable tag marks it as stale, the status becomes `STALE`.
433
- * - If no tag imposes stricter rules, the entry remains `FRESH`.
434
- *
435
- * @param state - The cache state containing tag metadata.
436
- * @param entry - The cache entry whose status is being evaluated.
437
- * @returns A tuple containing:
438
- * - The final {@link ENTRY_STATUS} imposed by tags.
439
- * - The earliest timestamp at which a tag marked the entry as stale
440
- * (or 0 if no tag imposed a stale rule).
441
- */
442
- function _statusFromTags(state, entry) {
443
- const entryCreatedAt = entry[0][0];
444
- let earliestTagStaleInvalidation = Infinity;
445
- let status = ENTRY_STATUS.FRESH;
446
- const tags = entry[2];
447
- if (tags) for (const tag of tags) {
448
- const ts = state._tags.get(tag);
449
- if (!ts) continue;
450
- const [tagExpiredAt, tagStaleSinceAt] = ts;
451
- if (tagExpiredAt >= entryCreatedAt) {
452
- status = ENTRY_STATUS.EXPIRED;
453
- break;
454
- }
455
- if (tagStaleSinceAt >= entryCreatedAt) {
456
- if (tagStaleSinceAt < earliestTagStaleInvalidation) earliestTagStaleInvalidation = tagStaleSinceAt;
457
- status = ENTRY_STATUS.STALE;
458
- }
459
- }
460
- return [status, status === ENTRY_STATUS.STALE ? earliestTagStaleInvalidation : 0];
461
- }
462
-
463
- //#endregion
464
- //#region src/cache/validators.ts
465
- /**
466
- * Computes the final derived status of a cache entry by combining:
467
- *
468
- * - The entry's own expiration timestamps (TTL and stale TTL).
469
- * - Any stricter expiration or stale rules imposed by its associated tags.
470
- *
471
- * Precedence rules:
472
- * - `EXPIRED` overrides everything.
473
- * - `STALE` overrides `FRESH`.
474
- * - If neither the entry nor its tags impose stricter rules, the entry is `FRESH`.
475
- *
476
- * @param state - The cache state containing tag metadata.
477
- * @param entry - The cache entry being evaluated.
478
- * @returns The final {@link ENTRY_STATUS} for the entry.
479
- */
480
- function computeEntryStatus(state, entry, now) {
481
- const [__createdAt, expiresAt, staleExpiresAt] = entry[0];
482
- const [tagStatus, earliestTagStaleInvalidation] = _statusFromTags(state, entry);
483
- if (tagStatus === ENTRY_STATUS.EXPIRED) return ENTRY_STATUS.EXPIRED;
484
- const windowStale = staleExpiresAt - expiresAt;
485
- if (tagStatus === ENTRY_STATUS.STALE && staleExpiresAt > 0 && now < earliestTagStaleInvalidation + windowStale && now <= staleExpiresAt) return ENTRY_STATUS.STALE;
486
- if (now < expiresAt) return ENTRY_STATUS.FRESH;
487
- if (staleExpiresAt > 0 && now < staleExpiresAt) return ENTRY_STATUS.STALE;
488
- return ENTRY_STATUS.EXPIRED;
489
- }
490
- /**
491
- * Determines whether a cache entry is fresh.
492
- *
493
- * A fresh entry is one whose final derived status is `FRESH`, meaning:
494
- * - It has not expired according to its own timestamps, and
495
- * - No associated tag imposes a stricter stale or expired rule.
496
- *
497
- * `entry` can be either a {@link CacheEntry} or a pre-computed {@link ENTRY_STATUS}.
498
- * Passing a pre-computed status avoids recalculating the entry status.
499
- *
500
- * @param state - The cache state containing tag metadata.
501
- * @param entry - The cache entry or pre-computed status being evaluated.
502
- * @param now - The current timestamp.
503
- * @returns True if the entry is fresh.
504
- */
505
- const isFresh = (state, entry, now) => {
506
- if (typeof entry === "string") return entry === ENTRY_STATUS.FRESH;
507
- return computeEntryStatus(state, entry, now) === ENTRY_STATUS.FRESH;
508
- };
509
- /**
510
- * Determines whether a cache entry is stale.
511
- *
512
- * A stale entry is one whose final derived status is `STALE`, meaning:
513
- * - It has passed its TTL but is still within its stale window, or
514
- * - A tag imposes a stale rule that applies to this entry.
515
- *
516
- * `entry` can be either a {@link CacheEntry} or a pre-computed {@link ENTRY_STATUS}.
517
- * Passing a pre-computed status avoids recalculating the entry status.
518
- *
519
- * @param state - The cache state containing tag metadata.
520
- * @param entry - The cache entry or pre-computed status being evaluated.
521
- * @param now - The current timestamp.
522
- * @returns True if the entry is stale.
523
- */
524
- const isStale = (state, entry, now) => {
525
- if (typeof entry === "string") return entry === ENTRY_STATUS.STALE;
526
- return computeEntryStatus(state, entry, now) === ENTRY_STATUS.STALE;
527
- };
528
- /**
529
- * Determines whether a cache entry is expired.
530
- *
531
- * An expired entry is one whose final derived status is `EXPIRED`, meaning:
532
- * - It has exceeded both its TTL and stale TTL, or
533
- * - A tag imposes an expiration rule that applies to this entry.
534
- *
535
- * `entry` can be either a {@link CacheEntry} or a pre-computed {@link ENTRY_STATUS}.
536
- * Passing a pre-computed status avoids recalculating the entry status.
537
- *
538
- * @param state - The cache state containing tag metadata.
539
- * @param entry - The cache entry or pre-computed status being evaluated.
540
- * @param now - The current timestamp.
541
- * @returns True if the entry is expired.
542
- */
543
- const isExpired = (state, entry, now) => {
544
- if (typeof entry === "string") return entry === ENTRY_STATUS.EXPIRED;
545
- return computeEntryStatus(state, entry, now) === ENTRY_STATUS.EXPIRED;
546
- };
547
-
548
- //#endregion
549
- //#region src/utils/purge-eval.ts
550
- /**
551
- * Computes memory utilization as a normalized 0–1 value.
552
- *
553
- * In backend environments where metrics are available, returns the actual
554
- * memory utilization from the monitor. In browser environments or when
555
- * metrics are unavailable, returns 0.
556
- *
557
- * @returns Memory utilization in range [0, 1]
558
- *
559
- * @internal
560
- */
561
- const getMemoryUtilization = () => {
562
- return 0;
563
- };
564
- /**
565
- * Computes size utilization as a normalized 0–1 value.
566
- *
567
- * If maxSize is finite, returns `currentSize / maxSize`. Otherwise returns 0.
568
- *
569
- * @param state - The cache state
570
- * @returns Size utilization in range [0, 1]
571
- *
572
- * @internal
573
- */
574
- const getSizeUtilization = (state) => {
575
- if (!Number.isFinite(state.maxSize) || state.maxSize <= 0 || state.size <= 0) return 0;
576
- return Math.min(1, state.size / state.maxSize);
577
- };
578
- /**
579
- * Computes a 0–1 resource usage metric based on the configured purge metric.
580
- *
581
- * - `"size"`: Returns size utilization only.
582
- * - `"memory"`: Returns memory utilization (backend only; returns 0 in browser).
583
- * - `"higher"`: Returns the maximum of memory and size utilization.
584
- *
585
- * The result is always clamped to [0, 1].
586
- *
587
- * @param state - The cache state
588
- * @returns Resource usage in range [0, 1]
589
- *
590
- * @internal
591
- */
592
- const computeResourceUsage = (state) => {
593
- const metric = state.purgeResourceMetric;
594
- if (!metric || metric === "fixed") return null;
595
- if (metric === "size") return getSizeUtilization(state);
596
- if (metric === "memory") return getMemoryUtilization();
597
- if (metric === "higher") return Math.min(1, Math.max(getMemoryUtilization(), getSizeUtilization(state)));
598
- return null;
599
- };
600
- /**
601
- * Determines whether stale entries should be purged based on the purge mode and current resource usage.
602
- *
603
- * @param mode - The purge mode setting
604
- * - `false` → never purge
605
- * - `true` → always purge
606
- * - `number (0–1)` → purge when `resourceUsage >= threshold`
607
- * @param state - The cache state
608
- * @returns True if stale entries should be purged, false otherwise
609
- *
610
- * @internal
611
- */
612
- const shouldPurge = (mode, state, purgeContext) => {
613
- if (mode === false) return false;
614
- if (mode === true) return true;
615
- const userThreshold = Number(mode);
616
- const defaultPurge = purgeContext === "sweep" ? DEFAULT_PURGE_STALE_ON_SWEEP_NO_LIMITS : DEFAULT_PURGE_STALE_ON_GET_NO_LIMITS;
617
- if (Number.isNaN(userThreshold)) return defaultPurge;
618
- const usage = computeResourceUsage(state);
619
- if (!usage) return defaultPurge;
620
- return usage >= Math.max(0, Math.min(1, userThreshold));
621
- };
622
-
623
- //#endregion
624
- //#region src/sweep/sweep-once.ts
625
- /**
626
- * Performs a single sweep operation on the cache to remove expired and optionally stale entries.
627
- * Uses a linear scan with a saved pointer to resume from the last processed key.
628
- * @param state - The cache state.
629
- * @param _maxKeysPerBatch - Maximum number of keys to process in this sweep.
630
- * @returns An object containing statistics about the sweep operation.
631
- */
632
- function _sweepOnce(state, _maxKeysPerBatch = MAX_KEYS_PER_BATCH) {
633
- if (!state._sweepIter) state._sweepIter = state.store.entries();
634
- let processed = 0;
635
- let expiredCount = 0;
636
- let staleCount = 0;
637
- for (let i = 0; i < _maxKeysPerBatch; i++) {
638
- const next = state._sweepIter.next();
639
- if (next.done) {
640
- state._sweepIter = state.store.entries();
641
- break;
642
- }
643
- processed += 1;
644
- const [key, entry] = next.value;
645
- const now = Date.now();
646
- const status = computeEntryStatus(state, entry, now);
647
- if (isExpired(state, status, now)) {
648
- deleteKey(state, key, DELETE_REASON.EXPIRED);
649
- expiredCount += 1;
650
- } else if (isStale(state, status, now)) {
651
- staleCount += 1;
652
- if (shouldPurge(state.purgeStaleOnSweep, state, "sweep")) deleteKey(state, key, DELETE_REASON.STALE);
653
- }
654
- }
655
- const expiredStaleCount = shouldPurge(state.purgeStaleOnSweep, state, "sweep") ? staleCount : 0;
656
- return {
657
- processed,
658
- expiredCount,
659
- staleCount,
660
- ratio: processed > 0 ? (expiredCount + expiredStaleCount) / processed : 0
661
- };
662
- }
663
-
664
- //#endregion
665
- //#region src/sweep/update-weight.ts
666
- /**
667
- * Updates the sweep weight (`_sweepWeight`) for each cache instance.
668
- *
669
- * The sweep weight determines the probability that an instance will be selected
670
- * for a cleanup (sweep) process. It is calculated based on the store size and
671
- * the ratio of expired keys.
672
- *
673
- * This function complements (`_selectInstanceToSweep`), which is responsible
674
- * for selecting the correct instance based on the weights assigned here.
675
- *
676
- * ---
677
- *
678
- * ### Sweep systems:
679
- * 1. **Normal sweep**
680
- * - Runs whenever the percentage of expired keys exceeds the allowed threshold
681
- * calculated by `calculateOptimalMaxExpiredRatio`.
682
- * - It is the main cleanup mechanism and is applied proportionally to the
683
- * store size and the expired‑key ratio.
684
- *
685
- * 2. **Memory‑conditioned sweep (control)**
686
- * - Works exactly like the normal sweep, except it may run even when it
687
- * normally wouldn’t.
688
- * - Only activates under **high memory pressure**.
689
- * - Serves as an additional control mechanism to adjust weights, keep the
690
- * system updated, and help prevent memory overflows.
691
- *
692
- * 3. **Round‑robin sweep (minimal control)**
693
- * - Always runs, even if the expired ratio is low or memory usage does not
694
- * require it.
695
- * - Processes a very small number of keys per instance, much smaller than
696
- * the normal sweep.
697
- * - Its main purpose is to ensure that all instances receive at least a
698
- * periodic weight update and minimal expired‑key control.
699
- *
700
- * ---
701
- * #### Important notes:
702
- * - A minimum `MINIMAL_EXPIRED_RATIO` (e.g., 5%) is assumed to ensure that
703
- * control sweeps can always run under high‑memory scenarios.
704
- * - Even with a minimum ratio, the normal sweep and the memory‑conditioned sweep
705
- * may **skip execution** if memory usage allows it and the expired ratio is
706
- * below the optimal maximum.
707
- * - The round‑robin sweep is never skipped: it always runs with a very small,
708
- * almost imperceptible cost.
709
- *
710
- * @returns The total accumulated sweep weight across all cache instances.
711
- */
712
- function _updateWeightSweep() {
713
- let totalSweepWeight = 0;
714
- for (const instCache of _instancesCache) {
715
- if (instCache.store.size <= 0) {
716
- instCache._sweepWeight = 0;
717
- continue;
718
- }
719
- let expiredRatio = MINIMAL_EXPIRED_RATIO;
720
- if (instCache._expiredRatio > MINIMAL_EXPIRED_RATIO) expiredRatio = instCache._expiredRatio;
721
- instCache._sweepWeight = instCache.store.size * expiredRatio;
722
- totalSweepWeight += instCache._sweepWeight;
723
- }
724
- return totalSweepWeight;
725
- }
726
-
727
- //#endregion
728
- //#region src/sweep/sweep.ts
729
- let _isSweepActive = false;
730
- let _pendingSweepTimeout = null;
731
- function startSweep(state) {
732
- if (_isSweepActive) return;
733
- _isSweepActive = true;
734
- /* @__PURE__ */ startMonitor();
735
- sweep(state);
736
- }
737
- /**
738
- * Performs a sweep operation on the cache to remove expired and optionally stale entries.
739
- * Uses a linear scan with a saved pointer to resume from the last processed key.
740
- * @param state - The cache state.
741
- */
742
- const sweep = async (state, utilities = {}) => {
743
- const { schedule = defaultSchedule, yieldFn = defaultYieldFn, now = Date.now(), runOnlyOne = false } = utilities;
744
- const startTime = now;
745
- let sweepIntervalMs = OPTIMAL_SWEEP_INTERVAL;
746
- let sweepTimeBudgetMs = OPTIMAL_SWEEP_TIME_BUDGET_IF_NOTE_METRICS_AVAILABLE;
747
- const totalSweepWeight = _updateWeightSweep();
748
- const currentExpiredRatios = [];
749
- const maxKeysPerBatch = totalSweepWeight <= 0 ? MAX_KEYS_PER_BATCH / _instancesCache.length : MAX_KEYS_PER_BATCH;
750
- let batchSweep = 0;
751
- while (true) {
752
- batchSweep += 1;
753
- const instanceToSweep = _selectInstanceToSweep({
754
- batchSweep,
755
- totalSweepWeight
756
- });
757
- if (!instanceToSweep) break;
758
- const { ratio } = _sweepOnce(instanceToSweep, maxKeysPerBatch);
759
- (currentExpiredRatios[instanceToSweep._instanceIndexState] ??= []).push(ratio);
760
- if (Date.now() - startTime > sweepTimeBudgetMs) break;
761
- await yieldFn();
762
- }
763
- _batchUpdateExpiredRatio(currentExpiredRatios);
764
- if (!runOnlyOne) schedule(() => void sweep(state, utilities), sweepIntervalMs);
765
- };
766
- const defaultSchedule = (fn, ms) => {
767
- _pendingSweepTimeout = setTimeout(fn, ms);
768
- if (typeof _pendingSweepTimeout.unref === "function") _pendingSweepTimeout.unref();
769
- };
770
- const defaultYieldFn = () => new Promise((resolve) => setImmediate(resolve));
771
-
772
- //#endregion
773
- //#region src/cache/create-cache.ts
774
- let _instanceCount = 0;
775
- const INSTANCE_WARNING_THRESHOLD = 99;
776
- const _instancesCache = [];
777
- /**
778
- * Creates the initial state for the TTL cache.
779
- * @param options - Configuration options for the cache.
780
- * @returns The initial cache state.
781
- */
782
- const createCache = (options = {}) => {
783
- 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;
784
- _instanceCount++;
785
- 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`);
786
- const resolvedPurgeResourceMetric = purgeResourceMetric ?? resolvePurgeResourceMetric({
787
- maxSize,
788
- maxMemorySize
789
- });
790
- const resolvedPurgeStaleOnGet = resolvePurgeStaleOnGet({
791
- limits: {
792
- maxSize,
793
- maxMemorySize
794
- },
795
- purgeResourceMetric: resolvedPurgeResourceMetric,
796
- userValue: purgeStaleOnGet
797
- });
798
- const resolvedPurgeStaleOnSweep = resolvePurgeStaleOnSweep({
799
- limits: {
800
- maxSize,
801
- maxMemorySize
802
- },
803
- purgeResourceMetric: resolvedPurgeResourceMetric,
804
- userValue: purgeStaleOnSweep
805
- });
806
- const state = {
807
- store: /* @__PURE__ */ new Map(),
808
- _sweepIter: null,
809
- get size() {
810
- return state.store.size;
811
- },
812
- onExpire,
813
- onDelete,
814
- maxSize,
815
- maxMemorySize,
816
- defaultTtl,
817
- defaultStaleWindow,
818
- purgeStaleOnGet: resolvedPurgeStaleOnGet,
819
- purgeStaleOnSweep: resolvedPurgeStaleOnSweep,
820
- purgeResourceMetric: resolvedPurgeResourceMetric,
821
- _maxAllowExpiredRatio,
822
- _autoStartSweep,
823
- _instanceIndexState: -1,
824
- _expiredRatio: 0,
825
- _sweepWeight: 0,
826
- _tags: /* @__PURE__ */ new Map()
827
- };
828
- state._instanceIndexState = _instancesCache.push(state) - 1;
829
- startSweep(state);
830
- return state;
831
- };
832
-
833
- //#endregion
834
- //#region src/cache/get.ts
835
- /**
836
- * Internal function that retrieves a value from the cache with its status information.
837
- * Returns a tuple containing the entry status and the complete cache entry.
838
- *
839
- * @param state - The cache state.
840
- * @param key - The key to retrieve.
841
- * @param now - Optional timestamp override (defaults to Date.now()).
842
- * @returns A tuple of [status, entry] if the entry is valid, or [null, undefined] if not found or expired.
843
- *
844
- * @internal
845
- */
846
- const getWithStatus = (state, key, purgeMode, now = Date.now()) => {
847
- const entry = state.store.get(key);
848
- if (!entry) return [null, void 0];
849
- const status = computeEntryStatus(state, entry, now);
850
- if (isFresh(state, status, now)) return [status, entry];
851
- if (isStale(state, status, now)) {
852
- if (shouldPurge(purgeMode ?? state.purgeStaleOnGet, state, "get")) deleteKey(state, key, DELETE_REASON.STALE);
853
- return [status, entry];
854
- }
855
- deleteKey(state, key, DELETE_REASON.EXPIRED);
856
- return [status, void 0];
857
- };
858
- /**
859
- * Retrieves a value from the cache if the entry is valid.
860
- * @param state - The cache state.
861
- * @param key - The key to retrieve.
862
- * @param now - Optional timestamp override (defaults to Date.now()).
863
- * @returns The cached value if valid, undefined otherwise.
864
- *
865
- * @internal
866
- */
867
- const get = (state, key, purgeMode, now = Date.now()) => {
868
- const [, entry] = getWithStatus(state, key, purgeMode, now);
869
- return entry ? entry[1] : void 0;
870
- };
871
-
872
- //#endregion
873
- //#region src/cache/has.ts
874
- /**
875
- * Checks if a key exists in the cache and is not expired.
876
- * @param state - The cache state.
877
- * @param key - The key to check.
878
- * @param now - Optional timestamp override (defaults to Date.now()).
879
- * @returns True if the key exists and is valid, false otherwise.
880
- */
881
- const has = (state, key, now = Date.now()) => {
882
- return get(state, key, now) !== void 0;
883
- };
884
-
885
- //#endregion
886
- //#region src/cache/invalidate-tag.ts
887
- /**
888
- * Invalidates one or more tags so that entries associated with them
889
- * become expired or stale from this moment onward.
890
- *
891
- * Semantics:
892
- * - Each tag maintains two timestamps in `state._tags`:
893
- * [expiredAt, staleSinceAt].
894
- * - Calling this function updates one of those timestamps to `_now`,
895
- * depending on whether the tag should force expiration or staleness.
896
- *
897
- * Rules:
898
- * - If `asStale` is false (default), the tag forces expiration:
899
- * entries created before `_now` will be considered expired.
900
- * - If `asStale` is true, the tag forces staleness:
901
- * entries created before `_now` will be considered stale,
902
- * but only if they support a stale window.
903
- *
904
- * Behavior:
905
- * - Each call replaces any previous invalidation timestamp for the tag.
906
- * - Entries created after `_now` are unaffected.
907
- *
908
- * @param state - The cache state containing tag metadata.
909
- * @param tags - A tag or list of tags to invalidate.
910
- * @param options.asStale - Whether the tag should mark entries as stale.
911
- */
912
- function invalidateTag(state, tags, options = {}, _now = Date.now()) {
913
- const tagList = Array.isArray(tags) ? tags : [tags];
914
- const asStale = options.asStale ?? false;
915
- for (const tag of tagList) {
916
- const currentTag = state._tags.get(tag);
917
- if (currentTag) if (asStale) currentTag[1] = _now;
918
- else currentTag[0] = _now;
919
- else state._tags.set(tag, [asStale ? 0 : _now, asStale ? _now : 0]);
920
- }
921
- }
922
-
923
- //#endregion
924
- //#region src/cache/set.ts
925
- /**
926
- * Sets or updates a value in the cache with TTL and an optional stale window.
927
- *
928
- * @param state - The cache state.
929
- * @param input - Cache entry definition (key, value, ttl, staleWindow, tags).
930
- * @param now - Optional timestamp override used as the base time (defaults to Date.now()).
931
- * @returns True if the entry was created or updated, false if rejected due to limits or invalid input.
932
- *
933
- * @remarks
934
- * - `ttl` defines when the entry becomes expired.
935
- * - `staleWindow` defines how long the entry may still be served as stale
936
- * after the expiration moment (`now + ttl`).
937
- * - Returns false if value is `undefined` (entry ignored, existing value untouched).
938
- * - Returns false if new entry would exceed `maxSize` limit (existing keys always allowed).
939
- * - Returns false if new entry would exceed `maxMemorySize` limit (existing keys always allowed).
940
- * - Returns true if entry was set or updated (or if existing key was updated at limit).
941
- */
942
- const setOrUpdate = (state, input, now = Date.now()) => {
943
- const { key, value, ttl: ttlInput, staleWindow: staleWindowInput, tags } = input;
944
- if (value === void 0) return false;
945
- if (key == null) throw new Error("Missing key.");
946
- if (state.size >= state.maxSize && !state.store.has(key)) return false;
947
- const ttl = ttlInput ?? state.defaultTtl;
948
- const staleWindow = staleWindowInput ?? state.defaultStaleWindow;
949
- const expiresAt = ttl > 0 ? now + ttl : Infinity;
950
- const entry = [
951
- [
952
- now,
953
- expiresAt,
954
- staleWindow > 0 ? expiresAt + staleWindow : 0
955
- ],
956
- value,
957
- typeof tags === "string" ? [tags] : Array.isArray(tags) ? tags : null
958
- ];
959
- state.store.set(key, entry);
960
- return true;
961
- };
962
-
963
- //#endregion
964
- //#region src/index.ts
965
- /**
966
- * A TTL (Time-To-Live) cache implementation with support for expiration,
967
- * stale windows, tag-based invalidation, and smart automatic sweeping.
968
- *
969
- * Provides O(1) constant-time operations for all core methods with support for:
970
- * - Expiration and stale windows
971
- * - Tag-based invalidation
972
- * - Automatic sweeping
973
- */
974
- var LocalTtlCache = class {
975
- state;
976
- /**
977
- * Creates a new cache instance.
978
- *
979
- * @param options - Configuration options for the cache (defaultTtl, defaultStaleWindow, maxSize, etc.)
980
- *
981
- */
982
- constructor(options) {
983
- this.state = createCache(options);
984
- }
985
- get size() {
986
- return this.state.size;
987
- }
988
- get(key, options) {
989
- if (options?.includeMetadata === true) {
990
- const [status, entry] = getWithStatus(this.state, key, options.purgeStale);
991
- if (!entry) return void 0;
992
- const [timestamps, value, tags] = entry;
993
- const [createdAt, expiresAt, staleExpiresAt] = timestamps;
994
- return {
995
- data: value,
996
- createdAt,
997
- expirationTime: expiresAt,
998
- staleWindowExpiration: staleExpiresAt,
999
- status,
1000
- tags
1001
- };
1002
- }
1003
- return get(this.state, key, options?.purgeStale);
1004
- }
1005
- set(key, value, options) {
1006
- return setOrUpdate(this.state, {
1007
- key,
1008
- value,
1009
- ttl: options?.ttl,
1010
- staleWindow: options?.staleWindow,
1011
- tags: options?.tags
1012
- });
1013
- }
1014
- delete(key) {
1015
- return deleteKey(this.state, key);
1016
- }
1017
- has(key) {
1018
- return has(this.state, key);
1019
- }
1020
- clear() {
1021
- clear(this.state);
1022
- }
1023
- invalidateTag(tags, options) {
1024
- invalidateTag(this.state, tags, options ?? {});
1025
- }
1026
- };
1027
-
1028
- //#endregion
1029
- export { ENTRY_STATUS, LocalTtlCache };
1030
- //# sourceMappingURL=index.js.map