@neezco/cache 0.3.0 → 0.4.1

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.
@@ -134,6 +134,246 @@ const DEFAULT_CPU_WEIGHT = 8.5;
134
134
  * Complements CPU weight to assess overall processing capacity.
135
135
  */
136
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);
137
377
 
138
378
  //#endregion
139
379
  //#region src/utils/get-process-memory-limit.ts
@@ -479,14 +719,14 @@ const deleteKey = (state, key, reason = DELETE_REASON.MANUAL) => {
479
719
  //#endregion
480
720
  //#region src/types.ts
481
721
  /**
482
- * Status of a cache entry.
722
+ * Entry status: fresh, stale, or expired.
483
723
  */
484
724
  let ENTRY_STATUS = /* @__PURE__ */ function(ENTRY_STATUS$1) {
485
- /** The entry is fresh and valid. */
725
+ /** Valid and within TTL. */
486
726
  ENTRY_STATUS$1["FRESH"] = "fresh";
487
- /** The entry is stale but can still be served. */
727
+ /** Expired but within stale window; still served. */
488
728
  ENTRY_STATUS$1["STALE"] = "stale";
489
- /** The entry has expired and is no longer valid. */
729
+ /** Beyond stale window; not served. */
490
730
  ENTRY_STATUS$1["EXPIRED"] = "expired";
491
731
  return ENTRY_STATUS$1;
492
732
  }({});
@@ -554,7 +794,7 @@ function computeEntryStatus(state, entry, now) {
554
794
  const [tagStatus, earliestTagStaleInvalidation] = _statusFromTags(state, entry);
555
795
  if (tagStatus === ENTRY_STATUS.EXPIRED) return ENTRY_STATUS.EXPIRED;
556
796
  const windowStale = staleExpiresAt - expiresAt;
557
- if (tagStatus === ENTRY_STATUS.STALE && staleExpiresAt > 0 && now < earliestTagStaleInvalidation + windowStale) return ENTRY_STATUS.STALE;
797
+ if (tagStatus === ENTRY_STATUS.STALE && staleExpiresAt > 0 && now < earliestTagStaleInvalidation + windowStale && now <= staleExpiresAt) return ENTRY_STATUS.STALE;
558
798
  if (now < expiresAt) return ENTRY_STATUS.FRESH;
559
799
  if (staleExpiresAt > 0 && now < staleExpiresAt) return ENTRY_STATUS.STALE;
560
800
  return ENTRY_STATUS.EXPIRED;
@@ -617,6 +857,82 @@ const isExpired = (state, entry, now) => {
617
857
  return computeEntryStatus(state, entry, now) === ENTRY_STATUS.EXPIRED;
618
858
  };
619
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
+
620
936
  //#endregion
621
937
  //#region src/sweep/sweep-once.ts
622
938
  /**
@@ -646,10 +962,10 @@ function _sweepOnce(state, _maxKeysPerBatch = MAX_KEYS_PER_BATCH) {
646
962
  expiredCount += 1;
647
963
  } else if (isStale(state, status, now)) {
648
964
  staleCount += 1;
649
- if (state.purgeStaleOnSweep) deleteKey(state, key, DELETE_REASON.STALE);
965
+ if (shouldPurge(state.purgeStaleOnSweep, state, "sweep")) deleteKey(state, key, DELETE_REASON.STALE);
650
966
  }
651
967
  }
652
- const expiredStaleCount = state.purgeStaleOnSweep ? staleCount : 0;
968
+ const expiredStaleCount = shouldPurge(state.purgeStaleOnSweep, state, "sweep") ? staleCount : 0;
653
969
  return {
654
970
  processed,
655
971
  expiredCount,
@@ -756,6 +1072,14 @@ function _updateWeightSweep() {
756
1072
 
757
1073
  //#endregion
758
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
+ }
759
1083
  /**
760
1084
  * Performs a sweep operation on the cache to remove expired and optionally stale entries.
761
1085
  * Uses a linear scan with a saved pointer to resume from the last processed key.
@@ -787,8 +1111,8 @@ const sweep = async (state, utilities = {}) => {
787
1111
  if (!runOnlyOne) schedule(() => void sweep(state, utilities), sweepIntervalMs);
788
1112
  };
789
1113
  const defaultSchedule = (fn, ms) => {
790
- const t = setTimeout(fn, ms);
791
- if (typeof t.unref === "function") t.unref();
1114
+ _pendingSweepTimeout = setTimeout(fn, ms);
1115
+ if (typeof _pendingSweepTimeout.unref === "function") _pendingSweepTimeout.unref();
792
1116
  };
793
1117
  const defaultYieldFn = () => new Promise((resolve) => setImmediate(resolve));
794
1118
 
@@ -797,16 +1121,35 @@ const defaultYieldFn = () => new Promise((resolve) => setImmediate(resolve));
797
1121
  let _instanceCount = 0;
798
1122
  const INSTANCE_WARNING_THRESHOLD = 99;
799
1123
  const _instancesCache = [];
800
- let _initSweepScheduled = false;
801
1124
  /**
802
1125
  * Creates the initial state for the TTL cache.
803
1126
  * @param options - Configuration options for the cache.
804
1127
  * @returns The initial cache state.
805
1128
  */
806
1129
  const createCache = (options = {}) => {
807
- 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 = false, purgeStaleOnSweep = false, _autoStartSweep = true } = 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;
808
1131
  _instanceCount++;
809
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
+ });
810
1153
  const state = {
811
1154
  store: /* @__PURE__ */ new Map(),
812
1155
  _sweepIter: null,
@@ -819,8 +1162,9 @@ const createCache = (options = {}) => {
819
1162
  maxMemorySize,
820
1163
  defaultTtl,
821
1164
  defaultStaleWindow,
822
- purgeStaleOnGet,
823
- purgeStaleOnSweep,
1165
+ purgeStaleOnGet: resolvedPurgeStaleOnGet,
1166
+ purgeStaleOnSweep: resolvedPurgeStaleOnSweep,
1167
+ purgeResourceMetric: resolvedPurgeResourceMetric,
824
1168
  _maxAllowExpiredRatio,
825
1169
  _autoStartSweep,
826
1170
  _instanceIndexState: -1,
@@ -829,12 +1173,7 @@ const createCache = (options = {}) => {
829
1173
  _tags: /* @__PURE__ */ new Map()
830
1174
  };
831
1175
  state._instanceIndexState = _instancesCache.push(state) - 1;
832
- if (_autoStartSweep) {
833
- if (_initSweepScheduled) return state;
834
- _initSweepScheduled = true;
835
- sweep(state);
836
- }
837
- startMonitor();
1176
+ startSweep(state);
838
1177
  return state;
839
1178
  };
840
1179
 
@@ -851,13 +1190,13 @@ const createCache = (options = {}) => {
851
1190
  *
852
1191
  * @internal
853
1192
  */
854
- const getWithStatus = (state, key, now = Date.now()) => {
1193
+ const getWithStatus = (state, key, purgeMode, now = Date.now()) => {
855
1194
  const entry = state.store.get(key);
856
1195
  if (!entry) return [null, void 0];
857
1196
  const status = computeEntryStatus(state, entry, now);
858
1197
  if (isFresh(state, status, now)) return [status, entry];
859
1198
  if (isStale(state, status, now)) {
860
- if (state.purgeStaleOnGet) deleteKey(state, key, DELETE_REASON.STALE);
1199
+ if (shouldPurge(purgeMode ?? state.purgeStaleOnGet, state, "get")) deleteKey(state, key, DELETE_REASON.STALE);
861
1200
  return [status, entry];
862
1201
  }
863
1202
  deleteKey(state, key, DELETE_REASON.EXPIRED);
@@ -872,8 +1211,8 @@ const getWithStatus = (state, key, now = Date.now()) => {
872
1211
  *
873
1212
  * @internal
874
1213
  */
875
- const get = (state, key, now = Date.now()) => {
876
- const [, entry] = getWithStatus(state, key, now);
1214
+ const get = (state, key, purgeMode, now = Date.now()) => {
1215
+ const [, entry] = getWithStatus(state, key, purgeMode, now);
877
1216
  return entry ? entry[1] : void 0;
878
1217
  };
879
1218
 
@@ -973,16 +1312,12 @@ const setOrUpdate = (state, input, now = Date.now()) => {
973
1312
  //#region src/index.ts
974
1313
  /**
975
1314
  * A TTL (Time-To-Live) cache implementation with support for expiration,
976
- * stale windows, tag-based invalidation, and automatic sweeping.
977
- *
978
- * Provides O(1) constant-time operations for all core methods.
1315
+ * stale windows, tag-based invalidation, and smart automatic sweeping.
979
1316
  *
980
- * @example
981
- * ```typescript
982
- * const cache = new LocalTtlCache();
983
- * cache.set("user:123", { name: "Alice" }, { ttl: 5 * 60 * 1000 });
984
- * const user = cache.get("user:123"); // { name: "Alice" }
985
- * ```
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
986
1321
  */
987
1322
  var LocalTtlCache = class {
988
1323
  state;
@@ -991,41 +1326,16 @@ var LocalTtlCache = class {
991
1326
  *
992
1327
  * @param options - Configuration options for the cache (defaultTtl, defaultStaleWindow, maxSize, etc.)
993
1328
  *
994
- * @example
995
- * ```typescript
996
- * const cache = new LocalTtlCache({
997
- * defaultTtl: 30 * 60 * 1000, // 30 minutes
998
- * defaultStaleWindow: 5 * 60 * 1000, // 5 minutes
999
- * maxSize: 500_000, // Maximum 500_000 entries
1000
- * onExpire: (key, value) => console.log(`Expired: ${key}`),
1001
- * onDelete: (key, value, reason) => console.log(`Deleted: ${key}, reason: ${reason}`),
1002
- * });
1003
- * ```
1004
1329
  */
1005
1330
  constructor(options) {
1006
1331
  this.state = createCache(options);
1007
1332
  }
1008
- /**
1009
- * Gets the current number of entries tracked by the cache.
1010
- *
1011
- * This value may include entries that are already expired but have not yet been
1012
- * removed by the lazy cleanup system. Expired keys are cleaned only when it is
1013
- * efficient to do so, so the count can temporarily be higher than the number of
1014
- * actually valid (non‑expired) entries.
1015
- *
1016
- * @returns The number of entries currently stored (including entries pending cleanup)
1017
- *
1018
- * @example
1019
- * ```typescript
1020
- * console.log(cache.size); // e.g., 42
1021
- * ```
1022
- */
1023
1333
  get size() {
1024
1334
  return this.state.size;
1025
1335
  }
1026
1336
  get(key, options) {
1027
- if (options?.includeMetadata) {
1028
- const [status, entry] = getWithStatus(this.state, key);
1337
+ if (options?.includeMetadata === true) {
1338
+ const [status, entry] = getWithStatus(this.state, key, options.purgeStale);
1029
1339
  if (!entry) return void 0;
1030
1340
  const [timestamps, value, tags] = entry;
1031
1341
  const [, expiresAt, staleExpiresAt] = timestamps;
@@ -1037,44 +1347,8 @@ var LocalTtlCache = class {
1037
1347
  tags
1038
1348
  };
1039
1349
  }
1040
- return get(this.state, key);
1350
+ return get(this.state, key, options?.purgeStale);
1041
1351
  }
1042
- /**
1043
- * Sets or updates a value in the cache.
1044
- *
1045
- * If the key already exists, it will be completely replaced.
1046
- *
1047
- * @param key - The key under which to store the value
1048
- * @param value - The value to cache (any type)
1049
- * @param options - Optional configuration for this specific entry
1050
- * @param options.ttl - Time-To-Live in milliseconds. Defaults to `defaultTtl`
1051
- * @param options.staleWindow - How long to serve stale data after expiration (milliseconds)
1052
- * @param options.tags - One or more tags for group invalidation
1053
- * @returns True if the entry was set or updated, false if rejected due to limits or invalid input
1054
- *
1055
- * @example
1056
- * ```typescript
1057
- * const success = cache.set("user:123", { name: "Alice" }, {
1058
- * ttl: 5 * 60 * 1000,
1059
- * staleWindow: 1 * 60 * 1000,
1060
- * tags: "user:123",
1061
- * });
1062
- *
1063
- * if (!success) {
1064
- * console.log("Entry was rejected due to size or memory limits");
1065
- * }
1066
- * ```
1067
- *
1068
- * @edge-cases
1069
- * - Overwriting an existing key replaces it completely
1070
- * - If `ttl` is 0 or Infinite, the entry never expires
1071
- * - If `staleWindow` is larger than `ttl`, the entry can be served as stale longer than it was fresh
1072
- * - Tags are optional; only necessary for group invalidation via `invalidateTag()`
1073
- * - Returns `false` if value is `undefined` (existing value remains untouched)
1074
- * - Returns `false` if new key would exceed [`maxSize`](./docs/configuration.md#maxsize-number) limit
1075
- * - Returns `false` if new key would exceed [`maxMemorySize`](./docs/configuration.md#maxmemorysize-number) limit
1076
- * - Updating existing keys always succeeds, even at limit
1077
- */
1078
1352
  set(key, value, options) {
1079
1353
  return setOrUpdate(this.state, {
1080
1354
  key,
@@ -1084,97 +1358,20 @@ var LocalTtlCache = class {
1084
1358
  tags: options?.tags
1085
1359
  });
1086
1360
  }
1087
- /**
1088
- * Deletes a specific key from the cache.
1089
- *
1090
- * @param key - The key to delete
1091
- * @returns True if the key was deleted, false if it didn't exist
1092
- *
1093
- * @example
1094
- * ```typescript
1095
- * const wasDeleted = cache.delete("user:123");
1096
- * ```
1097
- *
1098
- * @edge-cases
1099
- * - Triggers the `onDelete` callback with reason `'manual'`
1100
- * - Does not trigger the `onExpire` callback
1101
- * - Returns `false` if the key was already expired
1102
- * - Deleting a non-existent key returns `false` without error
1103
- */
1104
1361
  delete(key) {
1105
1362
  return deleteKey(this.state, key);
1106
1363
  }
1107
- /**
1108
- * Checks if a key exists in the cache and is not fully expired.
1109
- *
1110
- * Returns true if the key exists and is either fresh or within the stale window.
1111
- * Use this when you only need to check existence without retrieving the value.
1112
- *
1113
- * @param key - The key to check
1114
- * @returns True if the key exists and is valid, false otherwise
1115
- *
1116
- * @example
1117
- * ```typescript
1118
- * if (cache.has("user:123")) {
1119
- * // Key exists (either fresh or stale)
1120
- * }
1121
- * ```
1122
- *
1123
- * @edge-cases
1124
- * - Returns `false` if the key doesn't exist
1125
- * - Returns `false` if the key has expired beyond the stale window
1126
- * - Returns `true` if the key is in the stale window (still being served)
1127
- * - Both `has()` and `get()` have O(1) complexity; prefer `get()` if you need the value
1128
- */
1129
1364
  has(key) {
1130
1365
  return has(this.state, key);
1131
1366
  }
1132
- /**
1133
- * Removes all entries from the cache at once.
1134
- *
1135
- * This is useful for resetting the cache or freeing memory when needed.
1136
- * The `onDelete` callback is NOT invoked during clear (intentional optimization).
1137
- *
1138
- * @example
1139
- * ```typescript
1140
- * cache.clear(); // cache.size is now 0
1141
- * ```
1142
- *
1143
- * @edge-cases
1144
- * - The `onDelete` callback is NOT triggered during clear
1145
- * - Clears both expired and fresh entries
1146
- * - Resets `cache.size` to 0
1147
- */
1148
1367
  clear() {
1149
1368
  clear(this.state);
1150
1369
  }
1151
- /**
1152
- * Marks all entries with one or more tags as expired (or stale, if requested).
1153
- *
1154
- * If an entry has multiple tags, invalidating ANY of those tags will invalidate the entry.
1155
- *
1156
- * @param tags - A single tag (string) or array of tags to invalidate
1157
- * @param asStale - If true, marks entries as stale instead of fully expired (still served from stale window)
1158
- *
1159
- * @example
1160
- * ```typescript
1161
- * // Invalidate a single tag
1162
- * cache.invalidateTag("user:123");
1163
- *
1164
- * // Invalidate multiple tags
1165
- * cache.invalidateTag(["user:123", "posts:456"]);
1166
- * ```
1167
- *
1168
- * @edge-cases
1169
- * - Does not throw errors if a tag has no associated entries
1170
- * - Invalidating a tag doesn't prevent new entries from being tagged with it later
1171
- * - The `onDelete` callback is triggered with reason `'expired'` (even if `asStale` is true)
1172
- */
1173
1370
  invalidateTag(tags, options) {
1174
1371
  invalidateTag(this.state, tags, options ?? {});
1175
1372
  }
1176
1373
  };
1177
1374
 
1178
1375
  //#endregion
1179
- export { LocalTtlCache };
1376
+ export { ENTRY_STATUS, LocalTtlCache };
1180
1377
  //# sourceMappingURL=index.mjs.map