tricache 0.6.2 → 0.6.4
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/dist/index.cjs +18 -10
- package/dist/index.d.cts +16 -5
- package/dist/index.d.ts +16 -5
- package/dist/index.js +18 -10
- package/package.json +1 -1
- package/src/cache-service.ts +1 -1
- package/src/serialize-worker.ts +3 -3
- package/src/smart-memory-cache.ts +35 -14
- package/src/types.ts +7 -0
package/dist/index.cjs
CHANGED
|
@@ -583,6 +583,7 @@ var WasmBloomFilter = class {
|
|
|
583
583
|
|
|
584
584
|
// src/smart-memory-cache.ts
|
|
585
585
|
var _hit = { value: void 0, isStale: false, expiresAt: 0, ttlMs: void 0, delta: void 0, fetchedAt: 0 };
|
|
586
|
+
var LARGE_VALUE_BYTES = 16384;
|
|
586
587
|
var EVICTION_BATCH_PERCENT = 0.2;
|
|
587
588
|
var EVICTION_SAMPLE = 16;
|
|
588
589
|
var EVICT_COUNT = Math.max(1, Math.ceil(EVICTION_SAMPLE * EVICTION_BATCH_PERCENT));
|
|
@@ -1002,7 +1003,8 @@ var SmartMemoryCache = class {
|
|
|
1002
1003
|
}
|
|
1003
1004
|
this.ensureCapacity(cat, size);
|
|
1004
1005
|
const now = Date.now();
|
|
1005
|
-
|
|
1006
|
+
const liveValue = size <= LARGE_VALUE_BYTES ? data : void 0;
|
|
1007
|
+
this.cache.set(key, { data: packed, value: liveValue, isCompressed: true, expiresAt: now + ttlMs, staleAt, size, hits: 1, lastAccess: now, priority, ttlMs, delta, setAt: now });
|
|
1006
1008
|
if (!existingEntry) this.bloom.add(key);
|
|
1007
1009
|
this.maybeRebuildBloom();
|
|
1008
1010
|
this.totalSize += size;
|
|
@@ -1167,9 +1169,9 @@ var SmartMemoryCache = class {
|
|
|
1167
1169
|
* peers while the backplane was down; evicting them forces a controlled
|
|
1168
1170
|
* cold-start re-fetch from L2 rather than serving silently stale data.
|
|
1169
1171
|
*
|
|
1170
|
-
* Write-time is
|
|
1171
|
-
*
|
|
1172
|
-
* conservatively.
|
|
1172
|
+
* Write-time is read from `entry.setAt` (set by `SmartMemoryCache.set()` since v0.7.0).
|
|
1173
|
+
* Older snapshot entries without `setAt` fall back to approximating via `expiresAt - ttlMs`;
|
|
1174
|
+
* entries with neither field are treated as potentially stale and evicted conservatively.
|
|
1173
1175
|
*
|
|
1174
1176
|
* @param cutoffMs - Unix timestamp in milliseconds; entries written before
|
|
1175
1177
|
* this time are evicted.
|
|
@@ -1179,7 +1181,7 @@ var SmartMemoryCache = class {
|
|
|
1179
1181
|
let evicted = 0;
|
|
1180
1182
|
for (const [key, entry] of this.cache) {
|
|
1181
1183
|
if (entry.priority === 4 && entry.expiresAt > Date.now()) continue;
|
|
1182
|
-
const setAt = entry.ttlMs != null ? entry.expiresAt - entry.ttlMs : 0;
|
|
1184
|
+
const setAt = entry.setAt ?? (entry.ttlMs != null ? entry.expiresAt - entry.ttlMs : 0);
|
|
1183
1185
|
if (setAt < cutoffMs) {
|
|
1184
1186
|
this._delete(key, "manual");
|
|
1185
1187
|
evicted++;
|
|
@@ -1211,7 +1213,7 @@ var SmartMemoryCache = class {
|
|
|
1211
1213
|
if (this.cache.has(key)) continue;
|
|
1212
1214
|
const rawData = entry.data;
|
|
1213
1215
|
const data = !entry.isCompressed ? (0, import_msgpackr.pack)(JSON.parse(rawData)) : rawData instanceof Uint8Array ? Buffer.from(rawData) : rawData;
|
|
1214
|
-
const value = (0, import_msgpackr.unpack)(data);
|
|
1216
|
+
const value = entry.size <= LARGE_VALUE_BYTES ? (0, import_msgpackr.unpack)(data) : void 0;
|
|
1215
1217
|
this.cache.set(key, { ...entry, data, value, isCompressed: true });
|
|
1216
1218
|
this.bloom.add(key);
|
|
1217
1219
|
loaded++;
|
|
@@ -1268,16 +1270,22 @@ var SmartMemoryCache = class {
|
|
|
1268
1270
|
/**
|
|
1269
1271
|
* Yields only the resolved values of live entries.
|
|
1270
1272
|
* Iterates Map.values() so the key is never loaded into the yielded path.
|
|
1271
|
-
* Returns the
|
|
1272
|
-
* the raw msgpackr Buffer — identical semantics to get() but without bloom/hit tracking.
|
|
1273
|
+
* Returns the deserialized value — identical semantics to get() but without bloom/hit tracking.
|
|
1273
1274
|
*/
|
|
1274
1275
|
*liveValues() {
|
|
1275
1276
|
const now = Date.now();
|
|
1276
1277
|
for (const entry of this.cache.values()) {
|
|
1277
1278
|
if (entry.expiresAt > now)
|
|
1278
|
-
yield
|
|
1279
|
+
yield this.resolveValue(entry);
|
|
1279
1280
|
}
|
|
1280
1281
|
}
|
|
1282
|
+
/**
|
|
1283
|
+
* Returns the deserialized value for an entry, unpacking from the msgpackr buffer
|
|
1284
|
+
* if the live object was not cached (large-entry optimisation or disk-restored entry).
|
|
1285
|
+
*/
|
|
1286
|
+
resolveValue(entry) {
|
|
1287
|
+
return entry.value !== void 0 ? entry.value : (0, import_msgpackr.unpack)(entry.data);
|
|
1288
|
+
}
|
|
1281
1289
|
// ── Stats ─────────────────────────────────────────────────────────────────
|
|
1282
1290
|
get size() {
|
|
1283
1291
|
return this.cache.size;
|
|
@@ -3289,7 +3297,7 @@ var CacheService = class _CacheService {
|
|
|
3289
3297
|
const prefixLen = this._namespace ? this._namespace.length + 1 : 0;
|
|
3290
3298
|
for (const [key, entry] of this.l1.liveEntries()) {
|
|
3291
3299
|
const k = prefixLen > 0 ? key.slice(prefixLen) : key;
|
|
3292
|
-
yield [k,
|
|
3300
|
+
yield [k, this.l1.resolveValue(entry)];
|
|
3293
3301
|
}
|
|
3294
3302
|
}
|
|
3295
3303
|
/**
|
package/dist/index.d.cts
CHANGED
|
@@ -83,6 +83,13 @@ interface SmartCacheEntry {
|
|
|
83
83
|
ttlMs?: number;
|
|
84
84
|
/** Milliseconds taken to fetch/compute this value — used by XFetch probabilistic early expiration. */
|
|
85
85
|
delta?: number;
|
|
86
|
+
/**
|
|
87
|
+
* Unix timestamp (ms) when this entry was written.
|
|
88
|
+
* Stored explicitly so staleness-fence eviction (`evictSetBefore`) is precise
|
|
89
|
+
* instead of approximating via `expiresAt - ttlMs`.
|
|
90
|
+
* Optional for backward compatibility with snapshots written by older versions.
|
|
91
|
+
*/
|
|
92
|
+
setAt?: number;
|
|
86
93
|
}
|
|
87
94
|
/** Returned by SmartMemoryCache.get() — distinguishes "cached null" from a real miss */
|
|
88
95
|
interface CacheHit {
|
|
@@ -1261,9 +1268,9 @@ declare class SmartMemoryCache {
|
|
|
1261
1268
|
* peers while the backplane was down; evicting them forces a controlled
|
|
1262
1269
|
* cold-start re-fetch from L2 rather than serving silently stale data.
|
|
1263
1270
|
*
|
|
1264
|
-
* Write-time is
|
|
1265
|
-
*
|
|
1266
|
-
* conservatively.
|
|
1271
|
+
* Write-time is read from `entry.setAt` (set by `SmartMemoryCache.set()` since v0.7.0).
|
|
1272
|
+
* Older snapshot entries without `setAt` fall back to approximating via `expiresAt - ttlMs`;
|
|
1273
|
+
* entries with neither field are treated as potentially stale and evicted conservatively.
|
|
1267
1274
|
*
|
|
1268
1275
|
* @param cutoffMs - Unix timestamp in milliseconds; entries written before
|
|
1269
1276
|
* this time are evicted.
|
|
@@ -1311,10 +1318,14 @@ declare class SmartMemoryCache {
|
|
|
1311
1318
|
/**
|
|
1312
1319
|
* Yields only the resolved values of live entries.
|
|
1313
1320
|
* Iterates Map.values() so the key is never loaded into the yielded path.
|
|
1314
|
-
* Returns the
|
|
1315
|
-
* the raw msgpackr Buffer — identical semantics to get() but without bloom/hit tracking.
|
|
1321
|
+
* Returns the deserialized value — identical semantics to get() but without bloom/hit tracking.
|
|
1316
1322
|
*/
|
|
1317
1323
|
liveValues(): Generator<unknown>;
|
|
1324
|
+
/**
|
|
1325
|
+
* Returns the deserialized value for an entry, unpacking from the msgpackr buffer
|
|
1326
|
+
* if the live object was not cached (large-entry optimisation or disk-restored entry).
|
|
1327
|
+
*/
|
|
1328
|
+
resolveValue(entry: SmartCacheEntry): unknown;
|
|
1318
1329
|
get size(): number;
|
|
1319
1330
|
get memoryUsage(): number;
|
|
1320
1331
|
getStats(): {
|
package/dist/index.d.ts
CHANGED
|
@@ -83,6 +83,13 @@ interface SmartCacheEntry {
|
|
|
83
83
|
ttlMs?: number;
|
|
84
84
|
/** Milliseconds taken to fetch/compute this value — used by XFetch probabilistic early expiration. */
|
|
85
85
|
delta?: number;
|
|
86
|
+
/**
|
|
87
|
+
* Unix timestamp (ms) when this entry was written.
|
|
88
|
+
* Stored explicitly so staleness-fence eviction (`evictSetBefore`) is precise
|
|
89
|
+
* instead of approximating via `expiresAt - ttlMs`.
|
|
90
|
+
* Optional for backward compatibility with snapshots written by older versions.
|
|
91
|
+
*/
|
|
92
|
+
setAt?: number;
|
|
86
93
|
}
|
|
87
94
|
/** Returned by SmartMemoryCache.get() — distinguishes "cached null" from a real miss */
|
|
88
95
|
interface CacheHit {
|
|
@@ -1261,9 +1268,9 @@ declare class SmartMemoryCache {
|
|
|
1261
1268
|
* peers while the backplane was down; evicting them forces a controlled
|
|
1262
1269
|
* cold-start re-fetch from L2 rather than serving silently stale data.
|
|
1263
1270
|
*
|
|
1264
|
-
* Write-time is
|
|
1265
|
-
*
|
|
1266
|
-
* conservatively.
|
|
1271
|
+
* Write-time is read from `entry.setAt` (set by `SmartMemoryCache.set()` since v0.7.0).
|
|
1272
|
+
* Older snapshot entries without `setAt` fall back to approximating via `expiresAt - ttlMs`;
|
|
1273
|
+
* entries with neither field are treated as potentially stale and evicted conservatively.
|
|
1267
1274
|
*
|
|
1268
1275
|
* @param cutoffMs - Unix timestamp in milliseconds; entries written before
|
|
1269
1276
|
* this time are evicted.
|
|
@@ -1311,10 +1318,14 @@ declare class SmartMemoryCache {
|
|
|
1311
1318
|
/**
|
|
1312
1319
|
* Yields only the resolved values of live entries.
|
|
1313
1320
|
* Iterates Map.values() so the key is never loaded into the yielded path.
|
|
1314
|
-
* Returns the
|
|
1315
|
-
* the raw msgpackr Buffer — identical semantics to get() but without bloom/hit tracking.
|
|
1321
|
+
* Returns the deserialized value — identical semantics to get() but without bloom/hit tracking.
|
|
1316
1322
|
*/
|
|
1317
1323
|
liveValues(): Generator<unknown>;
|
|
1324
|
+
/**
|
|
1325
|
+
* Returns the deserialized value for an entry, unpacking from the msgpackr buffer
|
|
1326
|
+
* if the live object was not cached (large-entry optimisation or disk-restored entry).
|
|
1327
|
+
*/
|
|
1328
|
+
resolveValue(entry: SmartCacheEntry): unknown;
|
|
1318
1329
|
get size(): number;
|
|
1319
1330
|
get memoryUsage(): number;
|
|
1320
1331
|
getStats(): {
|
package/dist/index.js
CHANGED
|
@@ -214,6 +214,7 @@ var WasmBloomFilter = class {
|
|
|
214
214
|
|
|
215
215
|
// src/smart-memory-cache.ts
|
|
216
216
|
var _hit = { value: void 0, isStale: false, expiresAt: 0, ttlMs: void 0, delta: void 0, fetchedAt: 0 };
|
|
217
|
+
var LARGE_VALUE_BYTES = 16384;
|
|
217
218
|
var EVICTION_BATCH_PERCENT = 0.2;
|
|
218
219
|
var EVICTION_SAMPLE = 16;
|
|
219
220
|
var EVICT_COUNT = Math.max(1, Math.ceil(EVICTION_SAMPLE * EVICTION_BATCH_PERCENT));
|
|
@@ -633,7 +634,8 @@ var SmartMemoryCache = class {
|
|
|
633
634
|
}
|
|
634
635
|
this.ensureCapacity(cat, size);
|
|
635
636
|
const now = Date.now();
|
|
636
|
-
|
|
637
|
+
const liveValue = size <= LARGE_VALUE_BYTES ? data : void 0;
|
|
638
|
+
this.cache.set(key, { data: packed, value: liveValue, isCompressed: true, expiresAt: now + ttlMs, staleAt, size, hits: 1, lastAccess: now, priority, ttlMs, delta, setAt: now });
|
|
637
639
|
if (!existingEntry) this.bloom.add(key);
|
|
638
640
|
this.maybeRebuildBloom();
|
|
639
641
|
this.totalSize += size;
|
|
@@ -798,9 +800,9 @@ var SmartMemoryCache = class {
|
|
|
798
800
|
* peers while the backplane was down; evicting them forces a controlled
|
|
799
801
|
* cold-start re-fetch from L2 rather than serving silently stale data.
|
|
800
802
|
*
|
|
801
|
-
* Write-time is
|
|
802
|
-
*
|
|
803
|
-
* conservatively.
|
|
803
|
+
* Write-time is read from `entry.setAt` (set by `SmartMemoryCache.set()` since v0.7.0).
|
|
804
|
+
* Older snapshot entries without `setAt` fall back to approximating via `expiresAt - ttlMs`;
|
|
805
|
+
* entries with neither field are treated as potentially stale and evicted conservatively.
|
|
804
806
|
*
|
|
805
807
|
* @param cutoffMs - Unix timestamp in milliseconds; entries written before
|
|
806
808
|
* this time are evicted.
|
|
@@ -810,7 +812,7 @@ var SmartMemoryCache = class {
|
|
|
810
812
|
let evicted = 0;
|
|
811
813
|
for (const [key, entry] of this.cache) {
|
|
812
814
|
if (entry.priority === 4 && entry.expiresAt > Date.now()) continue;
|
|
813
|
-
const setAt = entry.ttlMs != null ? entry.expiresAt - entry.ttlMs : 0;
|
|
815
|
+
const setAt = entry.setAt ?? (entry.ttlMs != null ? entry.expiresAt - entry.ttlMs : 0);
|
|
814
816
|
if (setAt < cutoffMs) {
|
|
815
817
|
this._delete(key, "manual");
|
|
816
818
|
evicted++;
|
|
@@ -842,7 +844,7 @@ var SmartMemoryCache = class {
|
|
|
842
844
|
if (this.cache.has(key)) continue;
|
|
843
845
|
const rawData = entry.data;
|
|
844
846
|
const data = !entry.isCompressed ? pack(JSON.parse(rawData)) : rawData instanceof Uint8Array ? Buffer.from(rawData) : rawData;
|
|
845
|
-
const value = unpack(data);
|
|
847
|
+
const value = entry.size <= LARGE_VALUE_BYTES ? unpack(data) : void 0;
|
|
846
848
|
this.cache.set(key, { ...entry, data, value, isCompressed: true });
|
|
847
849
|
this.bloom.add(key);
|
|
848
850
|
loaded++;
|
|
@@ -899,16 +901,22 @@ var SmartMemoryCache = class {
|
|
|
899
901
|
/**
|
|
900
902
|
* Yields only the resolved values of live entries.
|
|
901
903
|
* Iterates Map.values() so the key is never loaded into the yielded path.
|
|
902
|
-
* Returns the
|
|
903
|
-
* the raw msgpackr Buffer — identical semantics to get() but without bloom/hit tracking.
|
|
904
|
+
* Returns the deserialized value — identical semantics to get() but without bloom/hit tracking.
|
|
904
905
|
*/
|
|
905
906
|
*liveValues() {
|
|
906
907
|
const now = Date.now();
|
|
907
908
|
for (const entry of this.cache.values()) {
|
|
908
909
|
if (entry.expiresAt > now)
|
|
909
|
-
yield
|
|
910
|
+
yield this.resolveValue(entry);
|
|
910
911
|
}
|
|
911
912
|
}
|
|
913
|
+
/**
|
|
914
|
+
* Returns the deserialized value for an entry, unpacking from the msgpackr buffer
|
|
915
|
+
* if the live object was not cached (large-entry optimisation or disk-restored entry).
|
|
916
|
+
*/
|
|
917
|
+
resolveValue(entry) {
|
|
918
|
+
return entry.value !== void 0 ? entry.value : unpack(entry.data);
|
|
919
|
+
}
|
|
912
920
|
// ── Stats ─────────────────────────────────────────────────────────────────
|
|
913
921
|
get size() {
|
|
914
922
|
return this.cache.size;
|
|
@@ -2919,7 +2927,7 @@ var CacheService = class _CacheService {
|
|
|
2919
2927
|
const prefixLen = this._namespace ? this._namespace.length + 1 : 0;
|
|
2920
2928
|
for (const [key, entry] of this.l1.liveEntries()) {
|
|
2921
2929
|
const k = prefixLen > 0 ? key.slice(prefixLen) : key;
|
|
2922
|
-
yield [k,
|
|
2930
|
+
yield [k, this.l1.resolveValue(entry)];
|
|
2923
2931
|
}
|
|
2924
2932
|
}
|
|
2925
2933
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tricache",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.4",
|
|
4
4
|
"description": "Three-tier Node.js cache: L1 smart RAM (adaptive LFU/LRU + Count-Min Sketch) → L1.5 NVMe disk spill → L2 Redis/Valkey. Includes AES-256-GCM at-rest encryption, WASM Bloom filter, Stale-While-Revalidate, and thundering-herd prevention.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cache",
|
package/src/cache-service.ts
CHANGED
|
@@ -1555,7 +1555,7 @@ export class CacheService {
|
|
|
1555
1555
|
const prefixLen = this._namespace ? this._namespace.length + 1 : 0;
|
|
1556
1556
|
for (const [key, entry] of this.l1.liveEntries()) {
|
|
1557
1557
|
const k = prefixLen > 0 ? key.slice(prefixLen) : key;
|
|
1558
|
-
yield [k,
|
|
1558
|
+
yield [k, this.l1.resolveValue(entry) as T];
|
|
1559
1559
|
}
|
|
1560
1560
|
}
|
|
1561
1561
|
|
package/src/serialize-worker.ts
CHANGED
|
@@ -14,9 +14,9 @@
|
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
16
|
import { parentPort, workerData } from 'worker_threads';
|
|
17
|
-
import { CacheEncryption } from './encryption';
|
|
18
|
-
import type { EncryptionMode } from './encryption';
|
|
19
|
-
import type { ILogger } from './types';
|
|
17
|
+
import { CacheEncryption } from './encryption.ts';
|
|
18
|
+
import type { EncryptionMode } from './encryption.ts';
|
|
19
|
+
import type { ILogger } from './types.ts';
|
|
20
20
|
|
|
21
21
|
interface WorkerInit {
|
|
22
22
|
keyBase64: string;
|
|
@@ -18,6 +18,19 @@ import type { CacheHit, CachePriority, CategoryLimit, SmartCacheEntry, ILogger,
|
|
|
18
18
|
// Reusable return object for get() — eliminates one heap allocation per hot read.
|
|
19
19
|
// Safe because JS is single-threaded: callers consume all fields before the next get().
|
|
20
20
|
const _hit: CacheHit = { value: undefined, isStale: false, expiresAt: 0, ttlMs: undefined, delta: undefined, fetchedAt: 0 };
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Packed-byte threshold above which the live JS object is NOT stored alongside the
|
|
24
|
+
* serialised buffer. For entries above this size, the double-heap overhead (both the
|
|
25
|
+
* msgpackr Buffer and the deserialized object resident simultaneously) outweighs the
|
|
26
|
+
* latency saved by skipping unpack on read. Below the threshold the live object is
|
|
27
|
+
* stored so hot reads skip deserialization entirely.
|
|
28
|
+
*
|
|
29
|
+
* 16 KiB is a conservative default: msgpackr unpacks a 16 KiB buffer in ~0.03 ms,
|
|
30
|
+
* while storing the deserialized form of a 16 KiB payload typically costs 32–64 KiB
|
|
31
|
+
* of additional heap depending on object shape.
|
|
32
|
+
*/
|
|
33
|
+
const LARGE_VALUE_BYTES = 16_384;
|
|
21
34
|
import { WasmBloomFilter } from './wasm/bloom-filter-wasm';
|
|
22
35
|
|
|
23
36
|
|
|
@@ -551,7 +564,11 @@ export class SmartMemoryCache {
|
|
|
551
564
|
this.ensureCapacity(cat, size);
|
|
552
565
|
|
|
553
566
|
const now = Date.now();
|
|
554
|
-
|
|
567
|
+
// Only cache the live object for small entries — large entries would store the packed
|
|
568
|
+
// buffer AND a potentially much-larger deserialized object simultaneously (double-heap).
|
|
569
|
+
// For large entries, get() falls back to the unpack() path transparently.
|
|
570
|
+
const liveValue = size <= LARGE_VALUE_BYTES ? data : undefined;
|
|
571
|
+
this.cache.set(key, { data: packed, value: liveValue, isCompressed: true, expiresAt: now + ttlMs, staleAt, size, hits: 1, lastAccess: now, priority, ttlMs, delta, setAt: now });
|
|
555
572
|
// Only add new keys to the bloom — overwrites already have their bits set.
|
|
556
573
|
// Re-adding inflates the insertions counter, delaying trigger-2 phantom detection.
|
|
557
574
|
if (!existingEntry) this.bloom.add(key);
|
|
@@ -738,9 +755,9 @@ export class SmartMemoryCache {
|
|
|
738
755
|
* peers while the backplane was down; evicting them forces a controlled
|
|
739
756
|
* cold-start re-fetch from L2 rather than serving silently stale data.
|
|
740
757
|
*
|
|
741
|
-
* Write-time is
|
|
742
|
-
*
|
|
743
|
-
* conservatively.
|
|
758
|
+
* Write-time is read from `entry.setAt` (set by `SmartMemoryCache.set()` since v0.7.0).
|
|
759
|
+
* Older snapshot entries without `setAt` fall back to approximating via `expiresAt - ttlMs`;
|
|
760
|
+
* entries with neither field are treated as potentially stale and evicted conservatively.
|
|
744
761
|
*
|
|
745
762
|
* @param cutoffMs - Unix timestamp in milliseconds; entries written before
|
|
746
763
|
* this time are evicted.
|
|
@@ -752,11 +769,8 @@ export class SmartMemoryCache {
|
|
|
752
769
|
// Never evict live CRITICAL entries — auth tokens, active sessions, etc.
|
|
753
770
|
if (entry.priority === 4 /* CRITICAL */ && entry.expiresAt > Date.now()) continue;
|
|
754
771
|
|
|
755
|
-
//
|
|
756
|
-
|
|
757
|
-
const setAt = (entry.ttlMs != null)
|
|
758
|
-
? entry.expiresAt - entry.ttlMs
|
|
759
|
-
: 0; // unknown → assume stale
|
|
772
|
+
// Prefer the explicit setAt timestamp; fall back to approximation for older entries.
|
|
773
|
+
const setAt = entry.setAt ?? (entry.ttlMs != null ? entry.expiresAt - entry.ttlMs : 0);
|
|
760
774
|
|
|
761
775
|
if (setAt < cutoffMs) {
|
|
762
776
|
this._delete(key, 'manual');
|
|
@@ -798,8 +812,8 @@ export class SmartMemoryCache {
|
|
|
798
812
|
: rawData instanceof Uint8Array
|
|
799
813
|
? Buffer.from(rawData) // msgpackr Uint8Array → Buffer
|
|
800
814
|
: rawData as Buffer; // already a Buffer
|
|
801
|
-
//
|
|
802
|
-
const value = unpack(data);
|
|
815
|
+
// Cache the live object for small entries only — large entries skip double-heap storage.
|
|
816
|
+
const value = entry.size <= LARGE_VALUE_BYTES ? unpack(data) : undefined;
|
|
803
817
|
this.cache.set(key, { ...entry, data, value, isCompressed: true });
|
|
804
818
|
this.bloom.add(key);
|
|
805
819
|
loaded++;
|
|
@@ -861,17 +875,24 @@ export class SmartMemoryCache {
|
|
|
861
875
|
/**
|
|
862
876
|
* Yields only the resolved values of live entries.
|
|
863
877
|
* Iterates Map.values() so the key is never loaded into the yielded path.
|
|
864
|
-
* Returns the
|
|
865
|
-
* the raw msgpackr Buffer — identical semantics to get() but without bloom/hit tracking.
|
|
878
|
+
* Returns the deserialized value — identical semantics to get() but without bloom/hit tracking.
|
|
866
879
|
*/
|
|
867
880
|
*liveValues(): Generator<unknown> {
|
|
868
881
|
const now = Date.now();
|
|
869
882
|
for (const entry of this.cache.values()) {
|
|
870
883
|
if (entry.expiresAt > now)
|
|
871
|
-
yield
|
|
884
|
+
yield this.resolveValue(entry);
|
|
872
885
|
}
|
|
873
886
|
}
|
|
874
887
|
|
|
888
|
+
/**
|
|
889
|
+
* Returns the deserialized value for an entry, unpacking from the msgpackr buffer
|
|
890
|
+
* if the live object was not cached (large-entry optimisation or disk-restored entry).
|
|
891
|
+
*/
|
|
892
|
+
resolveValue(entry: SmartCacheEntry): unknown {
|
|
893
|
+
return entry.value !== undefined ? entry.value : unpack(entry.data as Buffer);
|
|
894
|
+
}
|
|
895
|
+
|
|
875
896
|
// ── Stats ─────────────────────────────────────────────────────────────────
|
|
876
897
|
|
|
877
898
|
get size(): number { return this.cache.size; }
|
package/src/types.ts
CHANGED
|
@@ -105,6 +105,13 @@ export interface SmartCacheEntry {
|
|
|
105
105
|
ttlMs?: number;
|
|
106
106
|
/** Milliseconds taken to fetch/compute this value — used by XFetch probabilistic early expiration. */
|
|
107
107
|
delta?: number;
|
|
108
|
+
/**
|
|
109
|
+
* Unix timestamp (ms) when this entry was written.
|
|
110
|
+
* Stored explicitly so staleness-fence eviction (`evictSetBefore`) is precise
|
|
111
|
+
* instead of approximating via `expiresAt - ttlMs`.
|
|
112
|
+
* Optional for backward compatibility with snapshots written by older versions.
|
|
113
|
+
*/
|
|
114
|
+
setAt?: number;
|
|
108
115
|
}
|
|
109
116
|
|
|
110
117
|
/** Returned by SmartMemoryCache.get() — distinguishes "cached null" from a real miss */
|