tricache 0.9.0 → 1.0.1
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 +7 -3
- package/dist/{cache-service-DVvroyqA.d.ts → cache-service-CREOp7MM.d.ts} +57 -12
- package/dist/{cache-service-KI5ACF3F.js → cache-service-MG7PN6MX.js} +3 -3
- package/dist/{chunk-XZL45ABR.js → chunk-2AJKEQMZ.js} +54 -1
- package/dist/{chunk-QNU7B43J.js → chunk-3I5L7K6I.js} +17 -0
- package/dist/{chunk-PL5J3ZNT.js → chunk-JYQFQJNJ.js} +682 -277
- package/dist/{chunk-LJ5QJB5O.js → chunk-UBWLPKTI.js} +127 -11
- package/dist/cli.cjs +857 -293
- package/dist/cli.js +4 -4
- package/dist/dashboard/index.d.ts +2 -2
- package/dist/drizzle/index.cjs +865 -294
- package/dist/drizzle/index.d.ts +2 -2
- package/dist/drizzle/index.js +1 -1
- package/dist/edge/index.cjs +115 -36
- package/dist/edge/index.d.ts +3 -3
- package/dist/edge/index.js +88 -37
- package/dist/hono/index.cjs +865 -294
- package/dist/hono/index.d.ts +2 -2
- package/dist/hono/index.js +1 -1
- package/dist/{hono-VA9A1m0I.d.ts → hono-DSV2ve66.d.ts} +11 -4
- package/dist/http/index.cjs +917 -315
- package/dist/http/index.d.ts +3 -3
- package/dist/http/index.js +60 -29
- package/dist/index.cjs +885 -294
- package/dist/index.d.ts +37 -7
- package/dist/index.js +16 -6
- package/dist/nestjs/index.cjs +856 -292
- package/dist/nestjs/index.d.ts +2 -2
- package/dist/nestjs/index.js +4 -4
- package/dist/next/index.cjs +858 -300
- package/dist/next/index.d.ts +2 -2
- package/dist/next/index.js +5 -11
- package/dist/prisma/index.d.ts +2 -2
- package/dist/serialize-worker.cjs +127 -9
- package/dist/serialize-worker.js +8 -4
- package/dist/{types-B_QkVwJ7.d.ts → types-Dyip5wAl.d.ts} +53 -7
- package/package.json +7 -4
- package/src/adapters/node-redis.ts +19 -0
- package/src/cache-service.ts +781 -297
- package/src/cli.ts +1 -1
- package/src/compression.ts +193 -15
- package/src/cross-region.ts +1 -1
- package/src/edge/cache.ts +94 -42
- package/src/env.d.ts +13 -0
- package/src/http/express.ts +55 -16
- package/src/http/fastify.ts +3 -7
- package/src/index.ts +9 -1
- package/src/next/cache-handler.ts +1 -4
- package/src/next/isr-handler.ts +1 -4
- package/src/serialize-worker.ts +6 -2
- package/src/smart-memory-cache.ts +3 -2
- package/src/types.ts +45 -5
- package/src/utils/tag-matcher.ts +90 -0
package/src/index.ts
CHANGED
|
@@ -36,7 +36,7 @@ export {
|
|
|
36
36
|
export { DiskTier, resolveDefaultDiskDir } from './disk-tier';
|
|
37
37
|
export { TierLatencyWatchdog, type LatencyWatchdogOptions, type LatencyWatchdogTelemetry } from './latency-watchdog';
|
|
38
38
|
export { WasmBloomFilter } from './wasm/bloom-filter-wasm';
|
|
39
|
-
export { type CompressionAlgorithm } from './compression';
|
|
39
|
+
export { type CompressionAlgorithm, type CustomCompressor, initZstd } from './compression';
|
|
40
40
|
export { CacheCodec, defaultCodec, type CacheCodecOptions } from './codec';
|
|
41
41
|
export {
|
|
42
42
|
createHttpSnapshotAdapter,
|
|
@@ -114,6 +114,7 @@ export {
|
|
|
114
114
|
type ICacheBatchObservableCallback,
|
|
115
115
|
type ICacheMeter,
|
|
116
116
|
type CacheHealthStatus,
|
|
117
|
+
type InvalidateTagOptions,
|
|
117
118
|
} from './types';
|
|
118
119
|
|
|
119
120
|
export {
|
|
@@ -122,6 +123,13 @@ export {
|
|
|
122
123
|
type ParsedTraceParent,
|
|
123
124
|
} from './utils/tracing';
|
|
124
125
|
|
|
126
|
+
export {
|
|
127
|
+
isWildcardTag,
|
|
128
|
+
matchesTagPattern,
|
|
129
|
+
isChildTag,
|
|
130
|
+
expandHierarchicalTags,
|
|
131
|
+
} from './utils/tag-matcher';
|
|
132
|
+
|
|
125
133
|
export {
|
|
126
134
|
tricacheDashboard,
|
|
127
135
|
createNextDashboardHandlers,
|
|
@@ -111,10 +111,7 @@ export class TriCacheHandler {
|
|
|
111
111
|
* and returns a fresh unlocked ReadableStream if the stored value was a stream.
|
|
112
112
|
*/
|
|
113
113
|
async get(cacheKey: string, ctx?: CacheHandlerContext): Promise<CacheHandlerValue | undefined> {
|
|
114
|
-
const stored = await this.cache.
|
|
115
|
-
cacheKey,
|
|
116
|
-
async () => null,
|
|
117
|
-
);
|
|
114
|
+
const stored = await this.cache.peek<StoredNextCacheEntry>(cacheKey);
|
|
118
115
|
|
|
119
116
|
if (!stored || stored.data == null) {
|
|
120
117
|
return undefined;
|
package/src/next/isr-handler.ts
CHANGED
|
@@ -38,10 +38,7 @@ export class TriCacheISRHandler {
|
|
|
38
38
|
return this.requestCache.get(key) ?? null;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
const stored = await this.cache.
|
|
42
|
-
key,
|
|
43
|
-
async () => null,
|
|
44
|
-
);
|
|
41
|
+
const stored = await this.cache.peek<IncrementalCacheEntry>(key);
|
|
45
42
|
|
|
46
43
|
if (stored) {
|
|
47
44
|
this.requestCache.set(key, stored);
|
package/src/serialize-worker.ts
CHANGED
|
@@ -20,6 +20,7 @@ import type { ILogger } from './types.ts';
|
|
|
20
20
|
import {
|
|
21
21
|
compressBuffer,
|
|
22
22
|
decompressBuffer,
|
|
23
|
+
initZstd,
|
|
23
24
|
PREFIX_COMPRESSED,
|
|
24
25
|
PREFIX_ENC_COMPRESSED,
|
|
25
26
|
type CompressionAlgorithm,
|
|
@@ -53,13 +54,16 @@ const enc = new CacheEncryption(
|
|
|
53
54
|
prevMode,
|
|
54
55
|
);
|
|
55
56
|
|
|
56
|
-
const cmpAlgo = compression ?? '
|
|
57
|
+
const cmpAlgo = compression ?? 'zstd';
|
|
57
58
|
const cmpThreshold = compressionThresholdBytes ?? 1024;
|
|
58
59
|
|
|
59
60
|
parentPort!.on(
|
|
60
61
|
'message',
|
|
61
|
-
({ id, type, payload }: { id: number; type: 'encrypt' | 'decrypt'; payload: string | Uint8Array }) => {
|
|
62
|
+
async ({ id, type, payload }: { id: number; type: 'encrypt' | 'decrypt'; payload: string | Uint8Array }) => {
|
|
62
63
|
try {
|
|
64
|
+
if (cmpAlgo === 'zstd') {
|
|
65
|
+
await initZstd();
|
|
66
|
+
}
|
|
63
67
|
const textPayload =
|
|
64
68
|
typeof payload === 'string'
|
|
65
69
|
? payload
|
|
@@ -576,6 +576,7 @@ export class SmartMemoryCache {
|
|
|
576
576
|
staleAt?: number,
|
|
577
577
|
delta?: number,
|
|
578
578
|
tagVersions?: Record<string, number>,
|
|
579
|
+
setAt?: number,
|
|
579
580
|
): void {
|
|
580
581
|
// Single serialization pass — always msgpackr. Eliminates the prior JSON.stringify
|
|
581
582
|
// "size probe" that was discarded for large payloads (the double-pass).
|
|
@@ -621,7 +622,7 @@ export class SmartMemoryCache {
|
|
|
621
622
|
priority,
|
|
622
623
|
ttlMs,
|
|
623
624
|
delta,
|
|
624
|
-
setAt: now,
|
|
625
|
+
setAt: setAt ?? now,
|
|
625
626
|
tagVersions,
|
|
626
627
|
});
|
|
627
628
|
}
|
|
@@ -657,7 +658,7 @@ export class SmartMemoryCache {
|
|
|
657
658
|
// buffer AND a potentially much-larger deserialized object simultaneously (double-heap).
|
|
658
659
|
// For large entries, get() falls back to the unpack() path transparently.
|
|
659
660
|
const liveValue = size <= LARGE_VALUE_BYTES ? data : undefined;
|
|
660
|
-
this.cache.set(key, { data: packed, value: liveValue, isCompressed: true, expiresAt: now + ttlMs, staleAt, size, hits: 1, lastAccess: now, priority, ttlMs, delta, setAt: now, tagVersions });
|
|
661
|
+
this.cache.set(key, { data: packed, value: liveValue, isCompressed: true, expiresAt: now + ttlMs, staleAt, size, hits: 1, lastAccess: now, priority, ttlMs, delta, setAt: setAt ?? now, tagVersions });
|
|
661
662
|
// Only add new keys to the bloom — overwrites already have their bits set.
|
|
662
663
|
// Re-adding inflates the insertions counter, delaying trigger-2 phantom detection.
|
|
663
664
|
if (!existingEntry) this.bloom.add(key);
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { RemoteSnapshotOptions } from './remote-snapshot';
|
|
2
2
|
import type { CrossRegionRelayOptions } from './cross-region';
|
|
3
|
+
import type { CompressionAlgorithm, CustomCompressor } from './compression';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* tricache — shared types and interfaces
|
|
@@ -568,13 +569,22 @@ export interface CacheOptions {
|
|
|
568
569
|
|
|
569
570
|
// ── Compression ──────────────────────────────────────────────────────────
|
|
570
571
|
/**
|
|
571
|
-
*
|
|
572
|
-
* - `'
|
|
573
|
-
* - `'
|
|
572
|
+
* Compression algorithm for L2 (Redis) strings and disk-tier binary blobs.
|
|
573
|
+
* - `'zstd'` (default) — Facebook Zstandard (via WebAssembly) for ultra-high throughput and ~96% ratio.
|
|
574
|
+
* - `'lz4'` — Ultra-fast compression/decompression for high-throughput, latency-critical workloads.
|
|
575
|
+
* - `'brotli'` — High compression ratio for JSON / text payloads.
|
|
576
|
+
* - `'gzip'` — Standard Deflate/Gzip compression.
|
|
574
577
|
* - `'none'` — disable compression.
|
|
575
|
-
* Default: `'
|
|
578
|
+
* Default: `'zstd'`.
|
|
576
579
|
*/
|
|
577
|
-
compression?:
|
|
580
|
+
compression?: CompressionAlgorithm;
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Custom compression engine plugin. When provided, overrides the built-in algorithms
|
|
584
|
+
* and delegates compression/decompression to this provider.
|
|
585
|
+
*/
|
|
586
|
+
customCompressor?: CustomCompressor;
|
|
587
|
+
|
|
578
588
|
/**
|
|
579
589
|
* Minimum payload size in bytes before compression is applied.
|
|
580
590
|
* Payloads smaller than this threshold remain uncompressed to avoid compression overhead.
|
|
@@ -1033,6 +1043,22 @@ export interface CacheOptions {
|
|
|
1033
1043
|
*/
|
|
1034
1044
|
tagVersionTtlMs?: number;
|
|
1035
1045
|
|
|
1046
|
+
/**
|
|
1047
|
+
* Automatically expand hierarchical tags into their ancestor namespaces.
|
|
1048
|
+
* For example, tagging an entry with `'org:123:dept:456'` will also associate
|
|
1049
|
+
* it with `'org:123'` and `'org'`, allowing `invalidateTag('org:123')` or
|
|
1050
|
+
* `invalidateTag('org')` to automatically cascade down to this entry.
|
|
1051
|
+
*
|
|
1052
|
+
* Default: `false`.
|
|
1053
|
+
*/
|
|
1054
|
+
hierarchicalTags?: boolean;
|
|
1055
|
+
|
|
1056
|
+
/**
|
|
1057
|
+
* The delimiter used for splitting hierarchical tags when `hierarchicalTags` is enabled.
|
|
1058
|
+
* Default: `':'`.
|
|
1059
|
+
*/
|
|
1060
|
+
tagHierarchyDelimiter?: string;
|
|
1061
|
+
|
|
1036
1062
|
/**
|
|
1037
1063
|
* Read safety strategy for returned cached objects:
|
|
1038
1064
|
* - `'none'` (default): Returns raw in-memory references for sub-microsecond performance (~350 ns).
|
|
@@ -1075,6 +1101,18 @@ export interface CacheOptions {
|
|
|
1075
1101
|
meter?: ICacheMeter;
|
|
1076
1102
|
}
|
|
1077
1103
|
|
|
1104
|
+
/**
|
|
1105
|
+
* Options for tag invalidation operations (`cache.invalidateTag()` and `cache.invalidateTags()`).
|
|
1106
|
+
*/
|
|
1107
|
+
export interface InvalidateTagOptions {
|
|
1108
|
+
/**
|
|
1109
|
+
* When true, invalidates both the specified tag and any descendant child tags
|
|
1110
|
+
* in the hierarchy (e.g. invalidating 'org:123' also invalidates 'org:123:*').
|
|
1111
|
+
* Default: `false`.
|
|
1112
|
+
*/
|
|
1113
|
+
cascade?: boolean;
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1078
1116
|
/**
|
|
1079
1117
|
* Options for the ergonomic `cache.wrap(key, fetchFn, options)` method.
|
|
1080
1118
|
*/
|
|
@@ -1160,6 +1198,7 @@ export interface IRedisPipeline {
|
|
|
1160
1198
|
sadd(key: string, ...members: string[]): this;
|
|
1161
1199
|
smembers(key: string): this;
|
|
1162
1200
|
hset(key: string, value: Record<string, string>): this;
|
|
1201
|
+
hgetall?(key: string): this;
|
|
1163
1202
|
exec(): Promise<Array<[Error | null, any]> | null>;
|
|
1164
1203
|
}
|
|
1165
1204
|
|
|
@@ -1183,6 +1222,7 @@ export interface IRedisDriver {
|
|
|
1183
1222
|
scan?(cursor: string, ...args: (string | number)[]): Promise<[string, string[]]>;
|
|
1184
1223
|
scanStream?(options?: { match?: string; count?: number }): any;
|
|
1185
1224
|
hset?(key: string, value: Record<string, string>): Promise<unknown>;
|
|
1225
|
+
hgetall?(key: string): Promise<Record<string, string>>;
|
|
1186
1226
|
publish?(channel: string, message: string): Promise<number>;
|
|
1187
1227
|
subscribe?(...channels: string[]): Promise<unknown>;
|
|
1188
1228
|
disconnect(): Promise<void> | void;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utilities for wildcard matching and hierarchical cascading of cache tags.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const globRegexCache = new Map<string, RegExp>();
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Returns true if the given tag contains glob wildcard characters ('*' or '?').
|
|
9
|
+
*/
|
|
10
|
+
export function isWildcardTag(tag: string): boolean {
|
|
11
|
+
return tag.includes('*') || tag.includes('?');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Matches a tag name against a pattern supporting '*' (0 or more chars) and '?' (single char).
|
|
16
|
+
* Optimized with fast-paths for exact match, universal wildcard, and single prefix/suffix wildcards.
|
|
17
|
+
*/
|
|
18
|
+
export function matchesTagPattern(pattern: string, tag: string): boolean {
|
|
19
|
+
if (pattern === '*' || pattern === tag) return true;
|
|
20
|
+
if (!pattern.includes('*') && !pattern.includes('?')) return pattern === tag;
|
|
21
|
+
|
|
22
|
+
// Fast path: trailing single '*' (e.g. "org:123:*")
|
|
23
|
+
if (pattern.endsWith('*') && !pattern.slice(0, -1).includes('*') && !pattern.includes('?')) {
|
|
24
|
+
const prefix = pattern.slice(0, -1);
|
|
25
|
+
return tag.startsWith(prefix);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Fast path: leading single '*' (e.g. "*:settings")
|
|
29
|
+
if (pattern.startsWith('*') && !pattern.slice(1).includes('*') && !pattern.includes('?')) {
|
|
30
|
+
const suffix = pattern.slice(1);
|
|
31
|
+
return tag.endsWith(suffix);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// General glob with bounded RegExp cache
|
|
35
|
+
let re = globRegexCache.get(pattern);
|
|
36
|
+
if (!re) {
|
|
37
|
+
if (globRegexCache.size >= 1000) globRegexCache.clear();
|
|
38
|
+
const escaped = pattern
|
|
39
|
+
.replace(/[.+^${}()|[\]\\]/g, '\\$&')
|
|
40
|
+
.replace(/\*/g, '.*')
|
|
41
|
+
.replace(/\?/g, '.');
|
|
42
|
+
re = new RegExp(`^${escaped}$`);
|
|
43
|
+
globRegexCache.set(pattern, re);
|
|
44
|
+
}
|
|
45
|
+
return re.test(tag);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Determines whether `candidateTag` is either identical to `parentTag`
|
|
50
|
+
* or a child descendant in the hierarchy separated by `delimiter`.
|
|
51
|
+
*
|
|
52
|
+
* Example:
|
|
53
|
+
* isChildTag('org:123', 'org:123', ':') -> true
|
|
54
|
+
* isChildTag('org:123', 'org:123:dept', ':') -> true
|
|
55
|
+
* isChildTag('org:123', 'org:1234', ':') -> false (must match delimiter boundary)
|
|
56
|
+
*/
|
|
57
|
+
export function isChildTag(parentTag: string, candidateTag: string, delimiter = ':'): boolean {
|
|
58
|
+
if (candidateTag === parentTag) return true;
|
|
59
|
+
const prefix = parentTag.endsWith(delimiter) ? parentTag : `${parentTag}${delimiter}`;
|
|
60
|
+
return candidateTag.startsWith(prefix);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Expands hierarchical tags into all ancestor namespace levels.
|
|
65
|
+
*
|
|
66
|
+
* Example:
|
|
67
|
+
* expandHierarchicalTags(['org:123:dept:456', 'role:admin'], ':')
|
|
68
|
+
* => ['org', 'org:123', 'org:123:dept', 'org:123:dept:456', 'role', 'role:admin']
|
|
69
|
+
*/
|
|
70
|
+
export function expandHierarchicalTags(tags: string[], delimiter = ':'): string[] {
|
|
71
|
+
if (!tags.length) return [];
|
|
72
|
+
const result = new Set<string>();
|
|
73
|
+
|
|
74
|
+
for (const tag of tags) {
|
|
75
|
+
if (!tag) continue;
|
|
76
|
+
const parts = tag.split(delimiter).filter(p => p.length > 0);
|
|
77
|
+
if (parts.length <= 1) {
|
|
78
|
+
result.add(tag);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let accumulated = '';
|
|
83
|
+
for (let i = 0; i < parts.length; i++) {
|
|
84
|
+
accumulated = i === 0 ? parts[i] : `${accumulated}${delimiter}${parts[i]}`;
|
|
85
|
+
result.add(accumulated);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return Array.from(result);
|
|
90
|
+
}
|