tricache 0.6.5 → 0.6.6

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.
@@ -363,8 +363,8 @@ export class SmartMemoryCache {
363
363
  const lim = this.getCategoryLimit(category);
364
364
  const catCnt = this.categoryCount.get(category) ?? 0;
365
365
  const catSz = this.categorySize.get(category) ?? 0;
366
- const catOvf = catCnt >= lim.maxEntries || catSz + neededSize > lim.maxSizeBytes;
367
- const glbOvf = this.cache.size >= this.opts.maxEntries || this.totalSize + neededSize > this.opts.maxBytes;
366
+ let catOvf = catCnt >= lim.maxEntries || catSz + neededSize > lim.maxSizeBytes;
367
+ let glbOvf = this.cache.size >= this.opts.maxEntries || this.totalSize + neededSize > this.opts.maxBytes;
368
368
  if (!catOvf && !glbOvf) {
369
369
  // Proactive watermark: when either the global entry count OR total byte
370
370
  // usage crosses 90 % of its configured ceiling while there is still
@@ -381,7 +381,23 @@ export class SmartMemoryCache {
381
381
  }
382
382
  return;
383
383
  }
384
- this.smartEvict(category, catOvf);
384
+ // Hard overflow: evict until back under BOTH the category and global limits.
385
+ // A single smartEvict() pass removes at most EVICT_COUNT entries, which is not
386
+ // enough when one set() pushes the cache far past its ceiling (e.g. a large
387
+ // entry that itself dominates the byte budget). Loop until the overflow clears,
388
+ // bounded by MAX_EVICT_PASSES so a pathological key (larger than the limit
389
+ // cannot be evicted to fit itself) cannot spin forever.
390
+ const MAX_EVICT_PASSES = 64;
391
+ let passes = 0;
392
+ do {
393
+ this.smartEvict(category, catOvf);
394
+ const lim2 = this.getCategoryLimit(category);
395
+ const catCnt2 = this.categoryCount.get(category) ?? 0;
396
+ const catSz2 = this.categorySize.get(category) ?? 0;
397
+ catOvf = catCnt2 >= lim2.maxEntries || catSz2 + neededSize > lim2.maxSizeBytes;
398
+ glbOvf = this.cache.size >= this.opts.maxEntries || this.totalSize + neededSize > this.opts.maxBytes;
399
+ passes++;
400
+ } while ((catOvf || glbOvf) && passes < MAX_EVICT_PASSES);
385
401
  }
386
402
 
387
403
  private score(entry: SmartCacheEntry, catBonus: number, now: number, key: string): number {
package/src/types.ts CHANGED
@@ -182,6 +182,14 @@ export interface CacheMetrics {
182
182
  deletes: { total: number };
183
183
  revalidations: { total: number };
184
184
 
185
+ /** Rate-limit / singleton diagnostics */
186
+ counters: {
187
+ /** Times `increment()` hit a Redis error (fail-open by default, or fail-closed re-throw). */
188
+ errors: number;
189
+ /** Times a later `create(ns)` call passed options diverging from the live singleton. */
190
+ singletonDivergences: number;
191
+ };
192
+
185
193
  bloom: {
186
194
  /** Total bloom-filter checks (get() calls that passed prefix filter) */
187
195
  checksTotal: number;
@@ -700,4 +708,25 @@ export interface CacheOptions {
700
708
  name: string;
701
709
  sentinels: Array<{ host: string; port: number }>;
702
710
  };
711
+
712
+ /**
713
+ * When `true`, a second `CacheService.create()` call for an already-existing
714
+ * singleton (same namespace) whose options differ from the first call throws
715
+ * instead of silently returning the existing instance. Use in codebases where
716
+ * multiple init points could pass conflicting configuration (e.g. different
717
+ * Redis credentials or TTLs) and you want that class of bug to fail loudly.
718
+ *
719
+ * Default: `false` (warn once, return existing instance).
720
+ */
721
+ strictSingleton?: boolean;
722
+
723
+ /**
724
+ * `cache.increment()` distributed-rate-limit behaviour on a Redis error.
725
+ *
726
+ * Default: `false` (fail-open — on a Redis error the method returns `0`, so a
727
+ * caller's `if (count > LIMIT) reject()` guard never rejects during an outage).
728
+ * Set to `true` to instead re-throw the Redis error, letting the caller's limit
729
+ * guard enforce the limit even when Redis is unavailable.
730
+ */
731
+ failClosed?: boolean;
703
732
  }
@@ -94,8 +94,8 @@ export class WasmBloomFilter {
94
94
 
95
95
  /**
96
96
  * Maximum safe insertions before false-positive rate exceeds ~1 %.
97
- * Exact formula for k=7 hash rounds, m=100 000 bits:
98
- * n_max = -m * ln(1 - p^(1/k)) / k ≈ 18 169
97
+ * Exact formula for k=7 hash rounds, m=100 000 bits, p=1%:
98
+ * n_max = -m * ln(1 - p^(1/k)) / k ≈ 10 434
99
99
  */
100
100
  get maxCapacity(): number { return this._maxCapacity; }
101
101
  }