tricache 0.1.0 → 0.3.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 +248 -57
- package/dist/index.cjs +709 -87
- package/dist/index.d.cts +253 -8
- package/dist/index.d.ts +253 -8
- package/dist/index.js +709 -87
- package/package.json +15 -14
- package/src/cache-service.ts +436 -54
- package/src/disk-tier.ts +18 -2
- package/src/encryption.ts +69 -13
- package/src/index.ts +2 -0
- package/src/smart-memory-cache.ts +283 -23
- package/src/types.ts +75 -1
package/src/types.ts
CHANGED
|
@@ -20,6 +20,31 @@ export const consoleLogger: ILogger = {
|
|
|
20
20
|
error: (msg, meta, err) => console.error('[tricache]', msg, meta ?? '', err ?? ''),
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
+
// ─── Eviction reason ────────────────────────────────────────────────────────
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Reason passed to the `onEviction` callback.
|
|
27
|
+
* - `'capacity'` — L1 over-capacity; evicted during a `set()` call
|
|
28
|
+
* - `'category'` — Per-category limit exceeded
|
|
29
|
+
* - `'rebalance'` — `cache.rebalance()` was called
|
|
30
|
+
* - `'oom'` — OOM guard emergency eviction
|
|
31
|
+
* - `'ttl'` — Entry expired during periodic cleanup
|
|
32
|
+
* - `'manual'` — Explicit `cache.delete()` call
|
|
33
|
+
*/
|
|
34
|
+
export type EvictionReason = 'capacity' | 'category' | 'rebalance' | 'oom' | 'ttl' | 'manual';
|
|
35
|
+
|
|
36
|
+
// ─── Ping result ─────────────────────────────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
/** Per-tier latency returned by `cache.ping()`. */
|
|
39
|
+
export interface CachePingResult {
|
|
40
|
+
/** L1 RAM check latency in milliseconds */
|
|
41
|
+
l1: number;
|
|
42
|
+
/** L1.5 disk stats access in milliseconds */
|
|
43
|
+
disk: number;
|
|
44
|
+
/** L2 Redis PING round-trip in milliseconds, or `null` when Redis is disabled */
|
|
45
|
+
l2: number | null;
|
|
46
|
+
}
|
|
47
|
+
|
|
23
48
|
// ─── Cache priorities ────────────────────────────────────────────────────────
|
|
24
49
|
|
|
25
50
|
/** Priority levels for L1 eviction — higher = less likely to be evicted */
|
|
@@ -34,8 +59,14 @@ export enum CachePriority {
|
|
|
34
59
|
|
|
35
60
|
/** Internal representation of a single cache entry in the L1 Map */
|
|
36
61
|
export interface SmartCacheEntry {
|
|
37
|
-
/** msgpackr-packed binary payload */
|
|
62
|
+
/** msgpackr-packed binary payload — used for disk spill and snapshot serialization */
|
|
38
63
|
data: Buffer;
|
|
64
|
+
/**
|
|
65
|
+
* Deserialized JS value, cached at write time so hot reads skip the unpack step entirely.
|
|
66
|
+
* Undefined for entries restored from disk/snapshot (populated lazily on first access).
|
|
67
|
+
* NOTE: returned by reference — callers should treat the value as immutable.
|
|
68
|
+
*/
|
|
69
|
+
value?: unknown;
|
|
39
70
|
/** always true for entries written by the current code;
|
|
40
71
|
* may be false in legacy snapshots/disk files — handled transparently on import */
|
|
41
72
|
isCompressed: boolean;
|
|
@@ -236,6 +267,16 @@ export interface CacheOptions {
|
|
|
236
267
|
* Default: `'aes-256-gcm'`
|
|
237
268
|
*/
|
|
238
269
|
encryptionMode?: 'aes-256-gcm' | 'aes-128-gcm' | 'aes-128-ctr' | 'xor';
|
|
270
|
+
/**
|
|
271
|
+
* Fallback encryption key for zero-downtime key rotation.
|
|
272
|
+
* On every read, tricache tries the current `encryptionKey` first.
|
|
273
|
+
* If decryption fails (wrong key), it retries with `previousEncryptionKey`.
|
|
274
|
+
* All writes always use the current `encryptionKey`.
|
|
275
|
+
* Remove this field once you are confident all data has been re-encrypted.
|
|
276
|
+
*/
|
|
277
|
+
previousEncryptionKey?: string;
|
|
278
|
+
/** Encryption mode that was used with `previousEncryptionKey`. Defaults to `encryptionMode`. */
|
|
279
|
+
previousEncryptionMode?: 'aes-256-gcm' | 'aes-128-gcm' | 'aes-128-ctr' | 'xor';
|
|
239
280
|
|
|
240
281
|
// ── Snapshot ────────────────────────────────────────────────────────────
|
|
241
282
|
/**
|
|
@@ -270,6 +311,39 @@ export interface CacheOptions {
|
|
|
270
311
|
*/
|
|
271
312
|
namespace?: string;
|
|
272
313
|
|
|
314
|
+
/**
|
|
315
|
+
* Seconds to extend a stale SWR entry's life when background revalidation fails.
|
|
316
|
+
* Without this, a failed revalidation leaves the entry stale until its hard TTL
|
|
317
|
+
* expires, after which the next caller blocks. With `staleIfError`, tricache bumps
|
|
318
|
+
* the expiry by this many seconds on every failed revalidation, keeping the stale
|
|
319
|
+
* value available while the underlying service is degraded.
|
|
320
|
+
* Default: `0` (disabled — entry expires normally on revalidation failure).
|
|
321
|
+
*/
|
|
322
|
+
staleIfError?: number;
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Called after every L1 eviction. Receives the raw (namespaced) key and the reason.
|
|
326
|
+
* Useful for diagnosing cache pressure and tuning `categoryLimits`.
|
|
327
|
+
*/
|
|
328
|
+
onEviction?: (key: string, reason: EvictionReason) => void;
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Human-readable name for this CacheService instance. Added as an `instance` label
|
|
332
|
+
* to all Prometheus metrics emitted by `CacheService.toPrometheusText()`.
|
|
333
|
+
* Required when multiple CacheService instances share a process — without it,
|
|
334
|
+
* metric names from different instances collide.
|
|
335
|
+
* Default: `''` (no label).
|
|
336
|
+
*/
|
|
337
|
+
instanceName?: string;
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Controls whether writes are propagated to L2 (Redis).
|
|
341
|
+
* - `'read-write'` (default): read from and write to L2.
|
|
342
|
+
* - `'read-only'`: read from L2 but never write, delete, or expire keys in Redis.
|
|
343
|
+
* Use for shared read replicas or external Redis clusters you do not own.
|
|
344
|
+
*/
|
|
345
|
+
l2WriteMode?: 'read-write' | 'read-only';
|
|
346
|
+
|
|
273
347
|
// ── Invalidation backplane (Redis Pub/Sub) ──────────────────────────────
|
|
274
348
|
/**
|
|
275
349
|
* Enable Redis Pub/Sub invalidation backplane so every running instance
|