tricache 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.
- package/README.md +32 -0
- package/dist/cache-service.d.ts +54 -1
- package/dist/cache-service.d.ts.map +1 -1
- package/dist/cache-service.js +353 -59
- package/dist/cache-service.js.map +1 -1
- package/dist/disk-tier.d.ts +74 -7
- package/dist/disk-tier.d.ts.map +1 -1
- package/dist/disk-tier.js +512 -28
- package/dist/disk-tier.js.map +1 -1
- package/dist/encryption.d.ts +2 -0
- package/dist/encryption.d.ts.map +1 -1
- package/dist/encryption.js +55 -33
- package/dist/encryption.js.map +1 -1
- package/dist/index.cjs +952 -155
- package/dist/index.d.cts +269 -8
- package/dist/smart-memory-cache.d.ts +30 -0
- package/dist/smart-memory-cache.d.ts.map +1 -1
- package/dist/smart-memory-cache.js +216 -36
- package/dist/smart-memory-cache.js.map +1 -1
- package/dist/types.d.ts +109 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/wasm/bloom-filter-wasm.d.ts.map +1 -1
- package/dist/wasm/bloom-filter-wasm.js +5 -21
- package/dist/wasm/bloom-filter-wasm.js.map +1 -1
- package/package.json +11 -10
- package/src/cache-service.ts +394 -57
- package/src/disk-tier.ts +481 -27
- package/src/encryption.ts +61 -34
- package/src/smart-memory-cache.ts +213 -41
- package/src/types.ts +117 -0
- package/src/wasm/bloom-filter-wasm.ts +5 -21
package/src/types.ts
CHANGED
|
@@ -119,6 +119,9 @@ export interface CacheHit {
|
|
|
119
119
|
ttlMs?: number;
|
|
120
120
|
/** Fetch duration in ms stored at write time — for XFetch probabilistic threshold */
|
|
121
121
|
delta?: number;
|
|
122
|
+
/** Timestamp (Date.now()) recorded inside SmartMemoryCache.get() — lets CacheService
|
|
123
|
+
* reuse this value for refresh-ahead/XFetch math instead of making a second syscall. */
|
|
124
|
+
fetchedAt?: number;
|
|
122
125
|
}
|
|
123
126
|
|
|
124
127
|
// ─── Disk tier ───────────────────────────────────────────────────────────────
|
|
@@ -219,6 +222,19 @@ export interface CacheMetrics {
|
|
|
219
222
|
|
|
220
223
|
l1: { entries: number; sizeBytes: number; maxBytes: number };
|
|
221
224
|
disk: { files: number; sizeKB: number; maxKB: number };
|
|
225
|
+
|
|
226
|
+
/** Adaptive TTL statistics — present only when `adaptiveTtl` is enabled */
|
|
227
|
+
adaptiveTtl?: {
|
|
228
|
+
enabled: true;
|
|
229
|
+
/** Number of unique keys currently tracked in the latency ring buffers */
|
|
230
|
+
trackedKeys: number;
|
|
231
|
+
/**
|
|
232
|
+
* Top-20 slowest keys by p95 fetch latency, sorted descending.
|
|
233
|
+
* Keys are shown without the instance namespace prefix.
|
|
234
|
+
* Only keys with ≥ 5 recorded samples appear here.
|
|
235
|
+
*/
|
|
236
|
+
slowestKeys: Array<{ key: string; p95Ms: number; adaptedTtlSec: number }>;
|
|
237
|
+
};
|
|
222
238
|
}
|
|
223
239
|
|
|
224
240
|
// ─── Configuration ───────────────────────────────────────────────────────────
|
|
@@ -246,6 +262,12 @@ export interface CacheOptions {
|
|
|
246
262
|
l1MaxBytes?: number;
|
|
247
263
|
/** Max total entries in L1. Default: 2000 */
|
|
248
264
|
l1MaxEntries?: number;
|
|
265
|
+
/**
|
|
266
|
+
* Fraction of `l1MaxEntries` / `l1MaxBytes` at which proactive eviction fires
|
|
267
|
+
* before the hard limit is reached. Range: 0–1. Default: 0.9.
|
|
268
|
+
* Lower (e.g. 0.8) reduces GC pressure at the cost of slightly more eviction work.
|
|
269
|
+
*/
|
|
270
|
+
l1EvictionWatermark?: number;
|
|
249
271
|
/**
|
|
250
272
|
* Per-category limits keyed by the key prefix (e.g. `'org:'`).
|
|
251
273
|
* Merged over the built-in defaults.
|
|
@@ -478,4 +500,99 @@ export interface CacheOptions {
|
|
|
478
500
|
* No-op when Redis is disabled or unreachable — safe to set unconditionally.
|
|
479
501
|
*/
|
|
480
502
|
warmKeys?: string;
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Called on every L1, disk, or L2 cache hit. Receives the caller-facing key (no
|
|
506
|
+
* namespace prefix) and the tier that served it. Useful for per-route hit-rate
|
|
507
|
+
* metrics without waiting for the `onMetrics` interval.
|
|
508
|
+
*
|
|
509
|
+
* @example
|
|
510
|
+
* onHit: (key, tier) => cloudwatch.putMetricData({ key, tier })
|
|
511
|
+
*/
|
|
512
|
+
onHit?: (key: string, tier: 'l1' | 'disk' | 'l2') => void;
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Called on every cache miss (after L1, disk, and L2 are all exhausted).
|
|
516
|
+
* Receives the caller-facing key (no namespace prefix).
|
|
517
|
+
*
|
|
518
|
+
* @example
|
|
519
|
+
* onMiss: (key) => cloudwatch.putMetricData({ key })
|
|
520
|
+
*/
|
|
521
|
+
onMiss?: (key: string) => void;
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* When `true`, every value returned from an L1 cache hit is recursively
|
|
525
|
+
* frozen with `Object.freeze()` before being handed to the caller.
|
|
526
|
+
* Any attempt to mutate the returned object throws a `TypeError` immediately,
|
|
527
|
+
* catching reference-semantic corruption bugs that would otherwise silently
|
|
528
|
+
* corrupt the cached entry.
|
|
529
|
+
*
|
|
530
|
+
* **Do not enable in production** — use `process.env.NODE_ENV !== 'production'`.
|
|
531
|
+
* Deep-freezing large objects has measurable overhead per hit.
|
|
532
|
+
*
|
|
533
|
+
* @example
|
|
534
|
+
* frozen: process.env.NODE_ENV !== 'production'
|
|
535
|
+
*/
|
|
536
|
+
frozen?: boolean;
|
|
537
|
+
|
|
538
|
+
// ── Adaptive TTL ──────────────────────────────────────────────────────────
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* When `true`, tricache tracks per-key fetch latency in a rolling ring buffer
|
|
542
|
+
* and automatically derives an optimal TTL from the p95 fetch duration:
|
|
543
|
+
*
|
|
544
|
+
* ```
|
|
545
|
+
* adaptedTtl = clamp(p95LatencyMs × adaptiveTtlMultiplier, adaptiveTtlMin, adaptiveTtlMax)
|
|
546
|
+
* ```
|
|
547
|
+
*
|
|
548
|
+
* The caller-supplied `ttlSeconds` is used until at least 5 samples are collected
|
|
549
|
+
* for a key, then the library takes over TTL management autonomously.
|
|
550
|
+
*
|
|
551
|
+
* Expensive keys (slow DB queries) get cached longer; cheap keys stay close to
|
|
552
|
+
* their base TTL. Callers no longer need to reason about individual TTL values.
|
|
553
|
+
*
|
|
554
|
+
* Default: `false`.
|
|
555
|
+
*/
|
|
556
|
+
adaptiveTtl?: boolean;
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* Minimum TTL (seconds) the adaptive tuner will ever assign.
|
|
560
|
+
* Guards against extremely fast fetches producing near-zero TTLs.
|
|
561
|
+
* Default: `10`.
|
|
562
|
+
*/
|
|
563
|
+
adaptiveTtlMin?: number;
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* Maximum TTL (seconds) the adaptive tuner will ever assign.
|
|
567
|
+
* Guards against pathologically slow fetches producing indefinite caching.
|
|
568
|
+
* Default: `86400` (24 hours).
|
|
569
|
+
*/
|
|
570
|
+
adaptiveTtlMax?: number;
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* `p95FetchLatencyMs × adaptiveTtlMultiplier = adaptedTtlSeconds`.
|
|
574
|
+
*
|
|
575
|
+
* Intuition: a 200 ms p95 fetch with the default multiplier of 20 yields a
|
|
576
|
+
* 4-second TTL. A 2 000 ms fetch yields 40 seconds. Increase the multiplier
|
|
577
|
+
* if you want the cache to absorb more upstream pressure.
|
|
578
|
+
*
|
|
579
|
+
* Default: `20`.
|
|
580
|
+
*/
|
|
581
|
+
adaptiveTtlMultiplier?: number;
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* Number of fetch duration samples kept per key in the ring buffer.
|
|
585
|
+
* Larger values smooth out latency spikes but use slightly more memory
|
|
586
|
+
* (~8 bytes × samples per tracked key).
|
|
587
|
+
* Default: `32`.
|
|
588
|
+
*/
|
|
589
|
+
adaptiveTtlSamples?: number;
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* Maximum number of distinct keys tracked by the adaptive TTL system.
|
|
593
|
+
* When the cap is reached, the oldest-tracked key is evicted from the
|
|
594
|
+
* ring-buffer store to make room for the new key.
|
|
595
|
+
* Default: `5000`.
|
|
596
|
+
*/
|
|
597
|
+
adaptiveTtlMaxKeys?: number;
|
|
481
598
|
}
|
|
@@ -38,11 +38,7 @@ interface BloomWasmExports {
|
|
|
38
38
|
countBits(): number;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
const wasmBytes = Buffer.from(BLOOM_WASM_BASE64, 'base64');
|
|
43
|
-
const compiled = new WebAssembly.Module(wasmBytes);
|
|
44
|
-
return new WebAssembly.Instance(compiled, {});
|
|
45
|
-
}
|
|
41
|
+
const BLOOM_WASM_MODULE = new WebAssembly.Module(Buffer.from(BLOOM_WASM_BASE64, 'base64'));
|
|
46
42
|
|
|
47
43
|
export class WasmBloomFilter {
|
|
48
44
|
private readonly exports: BloomWasmExports;
|
|
@@ -55,7 +51,7 @@ export class WasmBloomFilter {
|
|
|
55
51
|
private _insertionCount = 0;
|
|
56
52
|
|
|
57
53
|
constructor() {
|
|
58
|
-
const instance =
|
|
54
|
+
const instance = new WebAssembly.Instance(BLOOM_WASM_MODULE, {});
|
|
59
55
|
this.exports = instance.exports as unknown as BloomWasmExports;
|
|
60
56
|
this.mem = new Uint8Array(this.exports.memory.buffer);
|
|
61
57
|
const k = 7, p = 0.01;
|
|
@@ -63,21 +59,9 @@ export class WasmBloomFilter {
|
|
|
63
59
|
}
|
|
64
60
|
|
|
65
61
|
private writeKey(key: string): number {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
// On the first non-ASCII char, fall back to TextEncoder for correct multi-byte encoding.
|
|
70
|
-
for (let i = 0; i < max; i++) {
|
|
71
|
-
const c = key.charCodeAt(i);
|
|
72
|
-
if (c > 127) {
|
|
73
|
-
const encoded = this.encoder.encode(key);
|
|
74
|
-
const len = Math.min(encoded.length, MAX_KEY_BYTES);
|
|
75
|
-
this.mem.set(encoded.subarray(0, len), KEY_STAGING_OFFSET);
|
|
76
|
-
return len;
|
|
77
|
-
}
|
|
78
|
-
this.mem[KEY_STAGING_OFFSET + i] = c;
|
|
79
|
-
}
|
|
80
|
-
return max;
|
|
62
|
+
if (key.length === 0) return 0;
|
|
63
|
+
const target = this.mem.subarray(KEY_STAGING_OFFSET, KEY_STAGING_OFFSET + MAX_KEY_BYTES);
|
|
64
|
+
return this.encoder.encodeInto(key, target).written;
|
|
81
65
|
}
|
|
82
66
|
|
|
83
67
|
add(key: string): void {
|