tricache 0.5.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.
- package/README.md +14 -0
- package/dist/cache-service.d.ts +419 -0
- package/dist/cache-service.d.ts.map +1 -0
- package/dist/cache-service.js +1788 -0
- package/dist/cache-service.js.map +1 -0
- package/dist/disk-tier.d.ts +150 -0
- package/dist/disk-tier.d.ts.map +1 -0
- package/dist/disk-tier.js +808 -0
- package/dist/disk-tier.js.map +1 -0
- package/dist/encryption.d.ts +90 -0
- package/dist/encryption.d.ts.map +1 -0
- package/dist/encryption.js +393 -0
- package/dist/encryption.js.map +1 -0
- package/dist/index.cjs +463 -72
- package/dist/index.d.cts +123 -6
- package/dist/index.d.ts +18 -1310
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -3131
- package/dist/index.js.map +1 -0
- package/dist/smart-memory-cache.d.ts +208 -0
- package/dist/smart-memory-cache.d.ts.map +1 -0
- package/dist/smart-memory-cache.js +933 -0
- package/dist/smart-memory-cache.js.map +1 -0
- package/dist/types.d.ts +537 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +20 -0
- package/dist/types.js.map +1 -0
- package/dist/wasm/bloom-filter-wasm.d.ts +29 -0
- package/dist/wasm/bloom-filter-wasm.d.ts.map +1 -0
- package/dist/wasm/bloom-filter-wasm.js +83 -0
- package/dist/wasm/bloom-filter-wasm.js.map +1 -0
- package/package.json +2 -2
- package/src/cache-service.ts +201 -17
- package/src/disk-tier.ts +244 -28
- package/src/encryption.ts +47 -22
- package/src/smart-memory-cache.ts +63 -30
- package/src/types.ts +80 -0
package/dist/index.d.cts
CHANGED
|
@@ -199,6 +199,22 @@ interface CacheMetrics {
|
|
|
199
199
|
sizeKB: number;
|
|
200
200
|
maxKB: number;
|
|
201
201
|
};
|
|
202
|
+
/** Adaptive TTL statistics — present only when `adaptiveTtl` is enabled */
|
|
203
|
+
adaptiveTtl?: {
|
|
204
|
+
enabled: true;
|
|
205
|
+
/** Number of unique keys currently tracked in the latency ring buffers */
|
|
206
|
+
trackedKeys: number;
|
|
207
|
+
/**
|
|
208
|
+
* Top-20 slowest keys by p95 fetch latency, sorted descending.
|
|
209
|
+
* Keys are shown without the instance namespace prefix.
|
|
210
|
+
* Only keys with ≥ 5 recorded samples appear here.
|
|
211
|
+
*/
|
|
212
|
+
slowestKeys: Array<{
|
|
213
|
+
key: string;
|
|
214
|
+
p95Ms: number;
|
|
215
|
+
adaptedTtlSec: number;
|
|
216
|
+
}>;
|
|
217
|
+
};
|
|
202
218
|
}
|
|
203
219
|
/** Per-category memory limits for L1 */
|
|
204
220
|
interface CategoryLimit {
|
|
@@ -220,6 +236,12 @@ interface CacheOptions {
|
|
|
220
236
|
l1MaxBytes?: number;
|
|
221
237
|
/** Max total entries in L1. Default: 2000 */
|
|
222
238
|
l1MaxEntries?: number;
|
|
239
|
+
/**
|
|
240
|
+
* Fraction of `l1MaxEntries` / `l1MaxBytes` at which proactive eviction fires
|
|
241
|
+
* before the hard limit is reached. Range: 0–1. Default: 0.9.
|
|
242
|
+
* Lower (e.g. 0.8) reduces GC pressure at the cost of slightly more eviction work.
|
|
243
|
+
*/
|
|
244
|
+
l1EvictionWatermark?: number;
|
|
223
245
|
/**
|
|
224
246
|
* Per-category limits keyed by the key prefix (e.g. `'org:'`).
|
|
225
247
|
* Merged over the built-in defaults.
|
|
@@ -458,6 +480,59 @@ interface CacheOptions {
|
|
|
458
480
|
* frozen: process.env.NODE_ENV !== 'production'
|
|
459
481
|
*/
|
|
460
482
|
frozen?: boolean;
|
|
483
|
+
/**
|
|
484
|
+
* When `true`, tricache tracks per-key fetch latency in a rolling ring buffer
|
|
485
|
+
* and automatically derives an optimal TTL from the p95 fetch duration:
|
|
486
|
+
*
|
|
487
|
+
* ```
|
|
488
|
+
* adaptedTtl = clamp(p95LatencyMs × adaptiveTtlMultiplier, adaptiveTtlMin, adaptiveTtlMax)
|
|
489
|
+
* ```
|
|
490
|
+
*
|
|
491
|
+
* The caller-supplied `ttlSeconds` is used until at least 5 samples are collected
|
|
492
|
+
* for a key, then the library takes over TTL management autonomously.
|
|
493
|
+
*
|
|
494
|
+
* Expensive keys (slow DB queries) get cached longer; cheap keys stay close to
|
|
495
|
+
* their base TTL. Callers no longer need to reason about individual TTL values.
|
|
496
|
+
*
|
|
497
|
+
* Default: `false`.
|
|
498
|
+
*/
|
|
499
|
+
adaptiveTtl?: boolean;
|
|
500
|
+
/**
|
|
501
|
+
* Minimum TTL (seconds) the adaptive tuner will ever assign.
|
|
502
|
+
* Guards against extremely fast fetches producing near-zero TTLs.
|
|
503
|
+
* Default: `10`.
|
|
504
|
+
*/
|
|
505
|
+
adaptiveTtlMin?: number;
|
|
506
|
+
/**
|
|
507
|
+
* Maximum TTL (seconds) the adaptive tuner will ever assign.
|
|
508
|
+
* Guards against pathologically slow fetches producing indefinite caching.
|
|
509
|
+
* Default: `86400` (24 hours).
|
|
510
|
+
*/
|
|
511
|
+
adaptiveTtlMax?: number;
|
|
512
|
+
/**
|
|
513
|
+
* `p95FetchLatencyMs × adaptiveTtlMultiplier = adaptedTtlSeconds`.
|
|
514
|
+
*
|
|
515
|
+
* Intuition: a 200 ms p95 fetch with the default multiplier of 20 yields a
|
|
516
|
+
* 4-second TTL. A 2 000 ms fetch yields 40 seconds. Increase the multiplier
|
|
517
|
+
* if you want the cache to absorb more upstream pressure.
|
|
518
|
+
*
|
|
519
|
+
* Default: `20`.
|
|
520
|
+
*/
|
|
521
|
+
adaptiveTtlMultiplier?: number;
|
|
522
|
+
/**
|
|
523
|
+
* Number of fetch duration samples kept per key in the ring buffer.
|
|
524
|
+
* Larger values smooth out latency spikes but use slightly more memory
|
|
525
|
+
* (~8 bytes × samples per tracked key).
|
|
526
|
+
* Default: `32`.
|
|
527
|
+
*/
|
|
528
|
+
adaptiveTtlSamples?: number;
|
|
529
|
+
/**
|
|
530
|
+
* Maximum number of distinct keys tracked by the adaptive TTL system.
|
|
531
|
+
* When the cap is reached, the oldest-tracked key is evicted from the
|
|
532
|
+
* ring-buffer store to make room for the new key.
|
|
533
|
+
* Default: `5000`.
|
|
534
|
+
*/
|
|
535
|
+
adaptiveTtlMaxKeys?: number;
|
|
461
536
|
}
|
|
462
537
|
|
|
463
538
|
/**
|
|
@@ -508,6 +583,7 @@ declare class CacheService {
|
|
|
508
583
|
private oomInterval;
|
|
509
584
|
private metricsInterval;
|
|
510
585
|
private _shutdownHandler;
|
|
586
|
+
private latencyTracker;
|
|
511
587
|
private readonly instanceId;
|
|
512
588
|
private readonly backplaneChannel;
|
|
513
589
|
private subClient;
|
|
@@ -917,8 +993,10 @@ declare class CacheService {
|
|
|
917
993
|
type EncryptionMode = 'aes-256-gcm' | 'aes-128-gcm' | 'aes-128-ctr' | 'xor';
|
|
918
994
|
declare class CacheEncryption {
|
|
919
995
|
private _key;
|
|
996
|
+
private _keyObj;
|
|
920
997
|
private readonly _mode;
|
|
921
998
|
private _prevKey;
|
|
999
|
+
private _prevKeyObj;
|
|
922
1000
|
private _prevMode;
|
|
923
1001
|
private readonly _ivPool;
|
|
924
1002
|
private _ivOffset;
|
|
@@ -987,6 +1065,8 @@ interface SmartMemoryCacheOptions {
|
|
|
987
1065
|
diskSpill?: (key: string, entry: SmartCacheEntry) => void | Promise<void>;
|
|
988
1066
|
onEviction?: (key: string, reason: EvictionReason) => void;
|
|
989
1067
|
logger: ILogger;
|
|
1068
|
+
/** Fraction of maxEntries / maxBytes at which proactive eviction fires (default 0.9). */
|
|
1069
|
+
evictionWatermark?: number;
|
|
990
1070
|
}
|
|
991
1071
|
declare class SmartMemoryCache {
|
|
992
1072
|
private readonly cache;
|
|
@@ -1007,6 +1087,15 @@ declare class SmartMemoryCache {
|
|
|
1007
1087
|
private categoryHits;
|
|
1008
1088
|
/** Counts every _delete() call — used to trigger bloom rebuild from ALL deletion paths. */
|
|
1009
1089
|
private _bloomDirtyCount;
|
|
1090
|
+
/**
|
|
1091
|
+
* Pre-allocated eviction candidate pools — reused across every smartEvict() call to
|
|
1092
|
+
* eliminate per-call heap pressure. Each slot holds a mutable {key, score} record
|
|
1093
|
+
* that is overwritten in-place rather than replaced with a new object literal.
|
|
1094
|
+
* Safe because JS is single-threaded: smartEvict() completes synchronously before
|
|
1095
|
+
* any other code can observe the pool state.
|
|
1096
|
+
*/
|
|
1097
|
+
private readonly _evictPool;
|
|
1098
|
+
private readonly _evictGPool;
|
|
1010
1099
|
constructor(opts: SmartMemoryCacheOptions, existing?: Map<string, SmartCacheEntry>);
|
|
1011
1100
|
private recalculateStats;
|
|
1012
1101
|
private getCategory;
|
|
@@ -1195,8 +1284,28 @@ declare class DiskTier {
|
|
|
1195
1284
|
private fileCount;
|
|
1196
1285
|
/** Next bucket index (0–255) for the staggered janitor wheel. */
|
|
1197
1286
|
private _nextJanitorBucket;
|
|
1287
|
+
private _db;
|
|
1288
|
+
private _stmtInsert;
|
|
1289
|
+
private _stmtSelect;
|
|
1290
|
+
private _stmtDelete;
|
|
1291
|
+
private _stmtExpire;
|
|
1292
|
+
private _stmtDelExp;
|
|
1293
|
+
private _stmtStats;
|
|
1198
1294
|
constructor(opts: DiskTierOptions);
|
|
1199
1295
|
private ensureDir;
|
|
1296
|
+
/**
|
|
1297
|
+
* Initialise the SQLite metadata index. Called once after the cache directory
|
|
1298
|
+
* is confirmed to exist. On any failure the instance silently falls back to
|
|
1299
|
+
* file-only mode — all public methods handle `this._db === null`.
|
|
1300
|
+
*
|
|
1301
|
+
* Schema:
|
|
1302
|
+
* meta(key_hash TEXT PK, file_path TEXT, expires_at INT, size INT)
|
|
1303
|
+
* idx_expires on (expires_at) — makes WHERE expires_at <= ? O(log n).
|
|
1304
|
+
*
|
|
1305
|
+
* WAL + synchronous=NORMAL: durable enough for a cache (a crash loses at
|
|
1306
|
+
* most one WAL frame — acceptable data loss for an L1.5 spill store).
|
|
1307
|
+
*/
|
|
1308
|
+
private _initSqlite;
|
|
1200
1309
|
private ensureUsageCounted;
|
|
1201
1310
|
/**
|
|
1202
1311
|
* SHA-256 hex digest of a key (pure CPU, no I/O).
|
|
@@ -1265,13 +1374,17 @@ declare class DiskTier {
|
|
|
1265
1374
|
/** Purge all expired entries. Returns number deleted. */
|
|
1266
1375
|
purgeExpired(): number;
|
|
1267
1376
|
/**
|
|
1268
|
-
* Purge expired entries
|
|
1269
|
-
*
|
|
1270
|
-
*
|
|
1271
|
-
*
|
|
1377
|
+
* Purge expired entries. Two modes depending on availability of SQLite:
|
|
1378
|
+
*
|
|
1379
|
+
* SQLite mode (Node 24 / Node 22 + --experimental-sqlite):
|
|
1380
|
+
* One indexed query (`WHERE expires_at <= now`) replaces the entire bucket
|
|
1381
|
+
* wheel. All expired entries are found and deleted in a single pass — the
|
|
1382
|
+
* 128-minute stagger is no longer necessary. `_nextJanitorBucket` is still
|
|
1383
|
+
* incremented so tests that inspect the counter continue to work.
|
|
1272
1384
|
*
|
|
1273
|
-
*
|
|
1274
|
-
*
|
|
1385
|
+
* File-only fallback:
|
|
1386
|
+
* Original staggered bucket wheel — purges exactly one of 256 subdirectory
|
|
1387
|
+
* buckets per call, spreading the O(fileCount) work across 256 ticks.
|
|
1275
1388
|
*
|
|
1276
1389
|
* Returns the number of entries deleted in this tick.
|
|
1277
1390
|
*/
|
|
@@ -1283,6 +1396,10 @@ declare class DiskTier {
|
|
|
1283
1396
|
sizeKB: number;
|
|
1284
1397
|
maxKB: number;
|
|
1285
1398
|
};
|
|
1399
|
+
/** Whether a SQLite metadata index is active ('sqlite') or the disk is scanned directly ('file-only'). */
|
|
1400
|
+
get indexMode(): 'sqlite' | 'file-only';
|
|
1401
|
+
/** Release the SQLite connection (if open). Call during shutdown before deleting the cache directory. */
|
|
1402
|
+
close(): void;
|
|
1286
1403
|
}
|
|
1287
1404
|
|
|
1288
1405
|
declare class WasmBloomFilter {
|