tricache 0.9.0 → 1.0.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 +7 -3
- package/dist/{cache-service-KI5ACF3F.js → cache-service-26GCO3UU.js} +3 -3
- package/dist/{cache-service-DVvroyqA.d.ts → cache-service-pJ-nFcVj.d.ts} +29 -12
- package/dist/{chunk-XZL45ABR.js → chunk-2AJKEQMZ.js} +54 -1
- package/dist/{chunk-PL5J3ZNT.js → chunk-7SZSQS55.js} +201 -34
- package/dist/{chunk-LJ5QJB5O.js → chunk-UBWLPKTI.js} +127 -11
- package/dist/cli.cjs +376 -50
- package/dist/cli.js +4 -4
- package/dist/dashboard/index.d.ts +2 -2
- package/dist/drizzle/index.cjs +384 -51
- package/dist/drizzle/index.d.ts +2 -2
- package/dist/drizzle/index.js +1 -1
- package/dist/edge/index.cjs +64 -8
- package/dist/edge/index.d.ts +3 -3
- package/dist/edge/index.js +37 -9
- package/dist/hono/index.cjs +384 -51
- package/dist/hono/index.d.ts +2 -2
- package/dist/hono/index.js +1 -1
- package/dist/{hono-VA9A1m0I.d.ts → hono-DuNvciyq.d.ts} +4 -4
- package/dist/http/index.cjs +384 -51
- package/dist/http/index.d.ts +3 -3
- package/dist/http/index.js +8 -8
- package/dist/index.cjs +385 -49
- package/dist/index.d.ts +35 -6
- package/dist/index.js +16 -6
- package/dist/nestjs/index.cjs +375 -49
- package/dist/nestjs/index.d.ts +2 -2
- package/dist/nestjs/index.js +4 -4
- package/dist/next/index.cjs +375 -49
- package/dist/next/index.d.ts +2 -2
- package/dist/next/index.js +3 -3
- 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-5HXqBfAf.d.ts} +51 -7
- package/package.json +7 -4
- package/src/cache-service.ts +238 -33
- package/src/cli.ts +1 -1
- package/src/compression.ts +193 -15
- package/src/cross-region.ts +1 -1
- package/src/edge/cache.ts +38 -11
- package/src/env.d.ts +13 -0
- package/src/index.ts +9 -1
- package/src/serialize-worker.ts +6 -2
- package/src/types.ts +43 -5
- package/src/utils/tag-matcher.ts +90 -0
- /package/dist/{chunk-QNU7B43J.js → chunk-VXFTFZ5N.js} +0 -0
package/dist/nestjs/index.cjs
CHANGED
|
@@ -1915,13 +1915,38 @@ var import_os2 = __toESM(require("os"), 1);
|
|
|
1915
1915
|
|
|
1916
1916
|
// src/compression.ts
|
|
1917
1917
|
var import_zlib = __toESM(require("zlib"), 1);
|
|
1918
|
+
var import_lz4js = __toESM(require("lz4js"), 1);
|
|
1919
|
+
var import_zstd_codec = require("zstd-codec");
|
|
1918
1920
|
var PREFIX_COMPRESSED = "cmp:v1:";
|
|
1919
1921
|
var PREFIX_ENC_COMPRESSED = "ecp:v1:";
|
|
1920
1922
|
var COMPRESSION_MAGIC = Buffer.from([84, 82, 67, 1]);
|
|
1923
|
+
var zstdSimple = null;
|
|
1924
|
+
var zstdInitPromise = null;
|
|
1925
|
+
function initZstd() {
|
|
1926
|
+
if (zstdSimple) return Promise.resolve(zstdSimple);
|
|
1927
|
+
if (!zstdInitPromise) {
|
|
1928
|
+
zstdInitPromise = new Promise((resolve) => {
|
|
1929
|
+
try {
|
|
1930
|
+
import_zstd_codec.ZstdCodec.run((zstd) => {
|
|
1931
|
+
zstdSimple = new zstd.Simple();
|
|
1932
|
+
resolve(zstdSimple);
|
|
1933
|
+
});
|
|
1934
|
+
} catch {
|
|
1935
|
+
resolve(null);
|
|
1936
|
+
}
|
|
1937
|
+
});
|
|
1938
|
+
}
|
|
1939
|
+
return zstdInitPromise;
|
|
1940
|
+
}
|
|
1941
|
+
initZstd().catch(() => {
|
|
1942
|
+
});
|
|
1921
1943
|
function isCompressed(buf) {
|
|
1922
1944
|
return buf.length >= 4 && buf[0] === 84 && buf[1] === 82 && buf[2] === 67 && buf[3] === 1;
|
|
1923
1945
|
}
|
|
1924
|
-
function compressBuffer(buf, algorithm) {
|
|
1946
|
+
function compressBuffer(buf, algorithm, custom) {
|
|
1947
|
+
if (custom) {
|
|
1948
|
+
return custom.compress(buf);
|
|
1949
|
+
}
|
|
1925
1950
|
if (algorithm === "brotli") {
|
|
1926
1951
|
return import_zlib.default.brotliCompressSync(buf, {
|
|
1927
1952
|
params: {
|
|
@@ -1933,37 +1958,127 @@ function compressBuffer(buf, algorithm) {
|
|
|
1933
1958
|
if (algorithm === "gzip") {
|
|
1934
1959
|
return import_zlib.default.gzipSync(buf, { level: 6 });
|
|
1935
1960
|
}
|
|
1961
|
+
if (algorithm === "lz4") {
|
|
1962
|
+
return Buffer.from(import_lz4js.default.compress(buf));
|
|
1963
|
+
}
|
|
1964
|
+
if (algorithm === "zstd") {
|
|
1965
|
+
if (!zstdSimple) {
|
|
1966
|
+
return import_zlib.default.brotliCompressSync(buf, {
|
|
1967
|
+
params: { [import_zlib.default.constants.BROTLI_PARAM_QUALITY]: 4 }
|
|
1968
|
+
});
|
|
1969
|
+
}
|
|
1970
|
+
return Buffer.from(zstdSimple.compress(buf));
|
|
1971
|
+
}
|
|
1936
1972
|
return buf;
|
|
1937
1973
|
}
|
|
1938
|
-
|
|
1974
|
+
var MAX_DECOMPRESS_BYTES = 25 * 1024 * 1024;
|
|
1975
|
+
function decompressBuffer(buf, algorithm = "zstd", custom) {
|
|
1976
|
+
if (custom) {
|
|
1977
|
+
const res = custom.decompress(buf);
|
|
1978
|
+
if (res.length > MAX_DECOMPRESS_BYTES) {
|
|
1979
|
+
throw new Error(`tricache: decompressed payload exceeds maximum safety threshold of ${MAX_DECOMPRESS_BYTES} bytes`);
|
|
1980
|
+
}
|
|
1981
|
+
return res;
|
|
1982
|
+
}
|
|
1983
|
+
const checkSafety = (decomp) => {
|
|
1984
|
+
if (decomp.length > MAX_DECOMPRESS_BYTES) {
|
|
1985
|
+
throw new Error(`tricache: decompressed payload exceeds maximum safety threshold of ${MAX_DECOMPRESS_BYTES} bytes`);
|
|
1986
|
+
}
|
|
1987
|
+
return decomp;
|
|
1988
|
+
};
|
|
1989
|
+
if (buf.length >= 4 && buf[0] === 40 && buf[1] === 181 && buf[2] === 47 && buf[3] === 253) {
|
|
1990
|
+
if (zstdSimple) {
|
|
1991
|
+
try {
|
|
1992
|
+
return checkSafety(Buffer.from(zstdSimple.decompress(buf)));
|
|
1993
|
+
} catch (err) {
|
|
1994
|
+
if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
|
|
1995
|
+
}
|
|
1996
|
+
}
|
|
1997
|
+
}
|
|
1998
|
+
if (buf.length >= 4 && buf[0] === 4 && buf[1] === 34 && buf[2] === 77 && buf[3] === 24) {
|
|
1999
|
+
try {
|
|
2000
|
+
return checkSafety(Buffer.from(import_lz4js.default.decompress(buf)));
|
|
2001
|
+
} catch (err) {
|
|
2002
|
+
if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
|
|
2003
|
+
}
|
|
2004
|
+
}
|
|
2005
|
+
if (buf.length >= 2 && buf[0] === 31 && buf[1] === 139) {
|
|
2006
|
+
try {
|
|
2007
|
+
return import_zlib.default.gunzipSync(buf, { maxOutputLength: MAX_DECOMPRESS_BYTES });
|
|
2008
|
+
} catch (err) {
|
|
2009
|
+
if (err?.code === "ERR_BUFFER_TOO_LARGE") {
|
|
2010
|
+
throw new Error(`tricache: decompressed payload exceeds maximum safety threshold of ${MAX_DECOMPRESS_BYTES} bytes`);
|
|
2011
|
+
}
|
|
2012
|
+
}
|
|
2013
|
+
}
|
|
2014
|
+
if (algorithm === "none") {
|
|
2015
|
+
return buf;
|
|
2016
|
+
}
|
|
2017
|
+
if (algorithm === "lz4") {
|
|
2018
|
+
try {
|
|
2019
|
+
return checkSafety(Buffer.from(import_lz4js.default.decompress(buf)));
|
|
2020
|
+
} catch (err) {
|
|
2021
|
+
if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
|
|
2022
|
+
}
|
|
2023
|
+
}
|
|
2024
|
+
if (algorithm === "zstd" && zstdSimple) {
|
|
2025
|
+
try {
|
|
2026
|
+
return checkSafety(Buffer.from(zstdSimple.decompress(buf)));
|
|
2027
|
+
} catch (err) {
|
|
2028
|
+
if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
|
|
2029
|
+
}
|
|
2030
|
+
}
|
|
1939
2031
|
const tryBrotli = () => {
|
|
1940
2032
|
try {
|
|
1941
|
-
return import_zlib.default.brotliDecompressSync(buf);
|
|
1942
|
-
} catch {
|
|
2033
|
+
return import_zlib.default.brotliDecompressSync(buf, { maxOutputLength: MAX_DECOMPRESS_BYTES });
|
|
2034
|
+
} catch (err) {
|
|
2035
|
+
if (err?.code === "ERR_BUFFER_TOO_LARGE") {
|
|
2036
|
+
throw new Error(`tricache: decompressed payload exceeds maximum safety threshold of ${MAX_DECOMPRESS_BYTES} bytes`);
|
|
2037
|
+
}
|
|
1943
2038
|
return null;
|
|
1944
2039
|
}
|
|
1945
2040
|
};
|
|
1946
2041
|
const tryGzip = () => {
|
|
1947
2042
|
try {
|
|
1948
|
-
return import_zlib.default.gunzipSync(buf);
|
|
1949
|
-
} catch {
|
|
2043
|
+
return import_zlib.default.gunzipSync(buf, { maxOutputLength: MAX_DECOMPRESS_BYTES });
|
|
2044
|
+
} catch (err) {
|
|
2045
|
+
if (err?.code === "ERR_BUFFER_TOO_LARGE") {
|
|
2046
|
+
throw new Error(`tricache: decompressed payload exceeds maximum safety threshold of ${MAX_DECOMPRESS_BYTES} bytes`);
|
|
2047
|
+
}
|
|
2048
|
+
return null;
|
|
2049
|
+
}
|
|
2050
|
+
};
|
|
2051
|
+
const tryLz4 = () => {
|
|
2052
|
+
try {
|
|
2053
|
+
return checkSafety(Buffer.from(import_lz4js.default.decompress(buf)));
|
|
2054
|
+
} catch (err) {
|
|
2055
|
+
if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
|
|
1950
2056
|
return null;
|
|
1951
2057
|
}
|
|
1952
2058
|
};
|
|
1953
|
-
const
|
|
2059
|
+
const tryZstd = () => {
|
|
2060
|
+
if (!zstdSimple) return null;
|
|
2061
|
+
try {
|
|
2062
|
+
return checkSafety(Buffer.from(zstdSimple.decompress(buf)));
|
|
2063
|
+
} catch (err) {
|
|
2064
|
+
if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
|
|
2065
|
+
return null;
|
|
2066
|
+
}
|
|
2067
|
+
};
|
|
2068
|
+
const result = (algorithm === "gzip" ? tryGzip() : null) ?? (algorithm === "lz4" ? tryLz4() : null) ?? (algorithm === "zstd" ? tryZstd() : null) ?? tryBrotli() ?? tryGzip() ?? tryLz4() ?? tryZstd();
|
|
1954
2069
|
if (result === null) {
|
|
1955
2070
|
throw new Error(`tricache: corrupt compressed payload (${buf.length} bytes, algorithm=${algorithm})`);
|
|
1956
2071
|
}
|
|
1957
2072
|
return result;
|
|
1958
2073
|
}
|
|
1959
|
-
function compressWithHeader(buf, algorithm) {
|
|
2074
|
+
function compressWithHeader(buf, algorithm, custom) {
|
|
1960
2075
|
if (algorithm === "none") return buf;
|
|
1961
|
-
const compressed = compressBuffer(buf, algorithm);
|
|
2076
|
+
const compressed = compressBuffer(buf, algorithm, custom);
|
|
1962
2077
|
return Buffer.concat([COMPRESSION_MAGIC, compressed]);
|
|
1963
2078
|
}
|
|
1964
|
-
function decompressWithHeader(buf, algorithm = "
|
|
2079
|
+
function decompressWithHeader(buf, algorithm = "zstd", custom) {
|
|
1965
2080
|
if (isCompressed(buf)) {
|
|
1966
|
-
return decompressBuffer(buf.subarray(4), algorithm);
|
|
2081
|
+
return decompressBuffer(buf.subarray(4), algorithm, custom);
|
|
1967
2082
|
}
|
|
1968
2083
|
return buf;
|
|
1969
2084
|
}
|
|
@@ -3578,6 +3693,55 @@ var IpcTelemetryServer = class {
|
|
|
3578
3693
|
}
|
|
3579
3694
|
};
|
|
3580
3695
|
|
|
3696
|
+
// src/utils/tag-matcher.ts
|
|
3697
|
+
var globRegexCache = /* @__PURE__ */ new Map();
|
|
3698
|
+
function isWildcardTag(tag) {
|
|
3699
|
+
return tag.includes("*") || tag.includes("?");
|
|
3700
|
+
}
|
|
3701
|
+
function matchesTagPattern(pattern, tag) {
|
|
3702
|
+
if (pattern === "*" || pattern === tag) return true;
|
|
3703
|
+
if (!pattern.includes("*") && !pattern.includes("?")) return pattern === tag;
|
|
3704
|
+
if (pattern.endsWith("*") && !pattern.slice(0, -1).includes("*") && !pattern.includes("?")) {
|
|
3705
|
+
const prefix = pattern.slice(0, -1);
|
|
3706
|
+
return tag.startsWith(prefix);
|
|
3707
|
+
}
|
|
3708
|
+
if (pattern.startsWith("*") && !pattern.slice(1).includes("*") && !pattern.includes("?")) {
|
|
3709
|
+
const suffix = pattern.slice(1);
|
|
3710
|
+
return tag.endsWith(suffix);
|
|
3711
|
+
}
|
|
3712
|
+
let re = globRegexCache.get(pattern);
|
|
3713
|
+
if (!re) {
|
|
3714
|
+
if (globRegexCache.size >= 1e3) globRegexCache.clear();
|
|
3715
|
+
const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\?/g, ".");
|
|
3716
|
+
re = new RegExp(`^${escaped}$`);
|
|
3717
|
+
globRegexCache.set(pattern, re);
|
|
3718
|
+
}
|
|
3719
|
+
return re.test(tag);
|
|
3720
|
+
}
|
|
3721
|
+
function isChildTag(parentTag, candidateTag, delimiter = ":") {
|
|
3722
|
+
if (candidateTag === parentTag) return true;
|
|
3723
|
+
const prefix = parentTag.endsWith(delimiter) ? parentTag : `${parentTag}${delimiter}`;
|
|
3724
|
+
return candidateTag.startsWith(prefix);
|
|
3725
|
+
}
|
|
3726
|
+
function expandHierarchicalTags(tags, delimiter = ":") {
|
|
3727
|
+
if (!tags.length) return [];
|
|
3728
|
+
const result = /* @__PURE__ */ new Set();
|
|
3729
|
+
for (const tag of tags) {
|
|
3730
|
+
if (!tag) continue;
|
|
3731
|
+
const parts = tag.split(delimiter).filter((p) => p.length > 0);
|
|
3732
|
+
if (parts.length <= 1) {
|
|
3733
|
+
result.add(tag);
|
|
3734
|
+
continue;
|
|
3735
|
+
}
|
|
3736
|
+
let accumulated = "";
|
|
3737
|
+
for (let i = 0; i < parts.length; i++) {
|
|
3738
|
+
accumulated = i === 0 ? parts[i] : `${accumulated}${delimiter}${parts[i]}`;
|
|
3739
|
+
result.add(accumulated);
|
|
3740
|
+
}
|
|
3741
|
+
}
|
|
3742
|
+
return Array.from(result);
|
|
3743
|
+
}
|
|
3744
|
+
|
|
3581
3745
|
// src/cache-service.ts
|
|
3582
3746
|
var LUA_COMPARE_AND_DELETE = `
|
|
3583
3747
|
local currentTs = redis.call('HGET', KEYS[1], 't')
|
|
@@ -3596,21 +3760,21 @@ var DEFAULT_CATEGORY_LIMITS = {
|
|
|
3596
3760
|
var DEFAULT_FORBIDDEN_PREFIXES = ["auth:", "session:", "mfa:", "rate_limit:"];
|
|
3597
3761
|
var DEFAULT_COUNTER_TTL_SECONDS = 60;
|
|
3598
3762
|
var GLOB_REGEX_CACHE_MAX = 256;
|
|
3599
|
-
var
|
|
3763
|
+
var globRegexCache2 = /* @__PURE__ */ new Map();
|
|
3600
3764
|
function getGlobRegex(pattern) {
|
|
3601
|
-
let re =
|
|
3765
|
+
let re = globRegexCache2.get(pattern);
|
|
3602
3766
|
if (re) {
|
|
3603
|
-
|
|
3604
|
-
|
|
3767
|
+
globRegexCache2.delete(pattern);
|
|
3768
|
+
globRegexCache2.set(pattern, re);
|
|
3605
3769
|
return re;
|
|
3606
3770
|
}
|
|
3607
3771
|
const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*+/g, ".*");
|
|
3608
3772
|
re = new RegExp("^" + escaped + "$");
|
|
3609
|
-
if (
|
|
3610
|
-
const oldestKey =
|
|
3611
|
-
if (oldestKey !== void 0)
|
|
3773
|
+
if (globRegexCache2.size >= GLOB_REGEX_CACHE_MAX) {
|
|
3774
|
+
const oldestKey = globRegexCache2.keys().next().value;
|
|
3775
|
+
if (oldestKey !== void 0) globRegexCache2.delete(oldestKey);
|
|
3612
3776
|
}
|
|
3613
|
-
|
|
3777
|
+
globRegexCache2.set(pattern, re);
|
|
3614
3778
|
return re;
|
|
3615
3779
|
}
|
|
3616
3780
|
var L2CircuitBreaker = class {
|
|
@@ -3864,6 +4028,7 @@ var CacheService = class _CacheService {
|
|
|
3864
4028
|
opts;
|
|
3865
4029
|
/** Pre-computed once — opts.namespace never changes after construction. */
|
|
3866
4030
|
_namespace;
|
|
4031
|
+
prefixLen;
|
|
3867
4032
|
/** Pre-computed once — disableRedis and redisHost never change after construction. */
|
|
3868
4033
|
_redisDisabled;
|
|
3869
4034
|
watchdog;
|
|
@@ -4072,12 +4237,15 @@ var CacheService = class _CacheService {
|
|
|
4072
4237
|
redisSentinel: options.redisSentinel,
|
|
4073
4238
|
redisProtocol: options.redisProtocol,
|
|
4074
4239
|
useShardedPubSub: options.useShardedPubSub ?? false,
|
|
4075
|
-
compression: options.compression ?? "
|
|
4240
|
+
compression: options.compression ?? "zstd",
|
|
4241
|
+
customCompressor: options.customCompressor,
|
|
4076
4242
|
compressionThresholdBytes: options.compressionThresholdBytes ?? 1024,
|
|
4077
4243
|
strictSingleton: options.strictSingleton ?? false,
|
|
4078
4244
|
failClosed: options.failClosed ?? false,
|
|
4079
4245
|
tagStrategy: options.tagStrategy ?? "set",
|
|
4080
4246
|
tagVersionTtlMs: options.tagVersionTtlMs ?? 5e3,
|
|
4247
|
+
hierarchicalTags: options.hierarchicalTags ?? false,
|
|
4248
|
+
tagHierarchyDelimiter: options.tagHierarchyDelimiter ?? ":",
|
|
4081
4249
|
cloneStrategy: options.cloneStrategy ?? "none",
|
|
4082
4250
|
backplaneMode: options.backplaneMode ?? "pubsub",
|
|
4083
4251
|
backplaneStreamMaxLen: options.backplaneStreamMaxLen ?? 1e4,
|
|
@@ -4150,6 +4318,7 @@ var CacheService = class _CacheService {
|
|
|
4150
4318
|
diskCircuitBreakerCooldownMs: this.opts.diskCircuitBreakerCooldownMs
|
|
4151
4319
|
});
|
|
4152
4320
|
this._namespace = ns;
|
|
4321
|
+
this.prefixLen = ns ? ns.length + 1 : 0;
|
|
4153
4322
|
this._redisDisabled = this.opts.disableRedis || !this.opts.redisClient && !this.opts.redisHost && !this.opts.redisClusterNodes?.length && !this.opts.redisSentinel;
|
|
4154
4323
|
this.watchdog = new TierLatencyWatchdog({
|
|
4155
4324
|
enabled: options.diskLatencyWatchdog ?? true,
|
|
@@ -4259,6 +4428,9 @@ var CacheService = class _CacheService {
|
|
|
4259
4428
|
this.backplaneStreamKey = options.backplaneStreamKey ?? `tricache:stream:{${ns || "default"}}`;
|
|
4260
4429
|
this.initBackplane();
|
|
4261
4430
|
const startupPromises = [];
|
|
4431
|
+
if (this.opts.compression === "zstd") {
|
|
4432
|
+
startupPromises.push(initZstd().then(() => void 0));
|
|
4433
|
+
}
|
|
4262
4434
|
if (this.opts.remoteSnapshot) {
|
|
4263
4435
|
startupPromises.push(this.loadRemoteSnapshot().then(() => void 0));
|
|
4264
4436
|
}
|
|
@@ -4764,6 +4936,50 @@ var CacheService = class _CacheService {
|
|
|
4764
4936
|
const tag = key;
|
|
4765
4937
|
const ver = typeof tagVersion === "number" ? tagVersion : (this.tagVersions.get(tag)?.version ?? 0) + 1;
|
|
4766
4938
|
this._setLocalTagVersion(tag, ver, performance.now());
|
|
4939
|
+
} else if (op === "tag_pattern_incr") {
|
|
4940
|
+
const pattern = key;
|
|
4941
|
+
const now = performance.now();
|
|
4942
|
+
for (const [tag, info] of this.tagVersions) {
|
|
4943
|
+
if (matchesTagPattern(pattern, tag)) {
|
|
4944
|
+
this._setLocalTagVersion(tag, info.version + 1, now);
|
|
4945
|
+
}
|
|
4946
|
+
}
|
|
4947
|
+
this.l1.scan((k, entry) => {
|
|
4948
|
+
if (entry.tagVersions) {
|
|
4949
|
+
for (const tag of Object.keys(entry.tagVersions)) {
|
|
4950
|
+
if (matchesTagPattern(pattern, tag)) {
|
|
4951
|
+
this.l1.delete(k);
|
|
4952
|
+
this._bumpKeyMutation(k);
|
|
4953
|
+
break;
|
|
4954
|
+
}
|
|
4955
|
+
}
|
|
4956
|
+
}
|
|
4957
|
+
}, 0);
|
|
4958
|
+
} else if (op === "tag_pattern_del") {
|
|
4959
|
+
const pattern = key;
|
|
4960
|
+
const matchedTagKeys = [];
|
|
4961
|
+
const prefix = this.nk("_tag_:");
|
|
4962
|
+
for (const [tagKey, members] of this.tagIndex) {
|
|
4963
|
+
const tag = tagKey.startsWith(prefix) ? tagKey.slice(prefix.length) : tagKey;
|
|
4964
|
+
if (matchesTagPattern(pattern, tag)) {
|
|
4965
|
+
matchedTagKeys.push(tagKey);
|
|
4966
|
+
for (const k of members) {
|
|
4967
|
+
this.l1.delete(k);
|
|
4968
|
+
this._bumpKeyMutation(k);
|
|
4969
|
+
this._pendingDiskDeletes.add(k);
|
|
4970
|
+
setImmediate(() => {
|
|
4971
|
+
try {
|
|
4972
|
+
if (!this._diskDisabled) this.disk.delete(k);
|
|
4973
|
+
} finally {
|
|
4974
|
+
this._pendingDiskDeletes.delete(k);
|
|
4975
|
+
}
|
|
4976
|
+
});
|
|
4977
|
+
}
|
|
4978
|
+
}
|
|
4979
|
+
}
|
|
4980
|
+
for (const tagKey of matchedTagKeys) {
|
|
4981
|
+
this.tagIndex.delete(tagKey);
|
|
4982
|
+
}
|
|
4767
4983
|
}
|
|
4768
4984
|
this.logger.debug("Backplane: invalidation applied", { op, key: key.slice(0, 60) });
|
|
4769
4985
|
}
|
|
@@ -4771,7 +4987,7 @@ var CacheService = class _CacheService {
|
|
|
4771
4987
|
_handleBackplaneMessage(message) {
|
|
4772
4988
|
try {
|
|
4773
4989
|
const msg = JSON.parse(message);
|
|
4774
|
-
if (!msg || typeof msg !== "object" || Array.isArray(msg) || typeof msg.src !== "string" || typeof msg.key !== "string" || msg.op !== "del" && msg.op !== "del-glob" && msg.op !== "tag_incr") {
|
|
4990
|
+
if (!msg || typeof msg !== "object" || Array.isArray(msg) || typeof msg.src !== "string" || typeof msg.key !== "string" || msg.op !== "del" && msg.op !== "del-glob" && msg.op !== "tag_incr" && msg.op !== "tag_pattern_incr" && msg.op !== "tag_pattern_del") {
|
|
4775
4991
|
this.logger.warn("Backplane: rejected invalid pubsub message format", {
|
|
4776
4992
|
raw: typeof message === "string" ? message.slice(0, 100).replace(/[\r\n\t]/g, " ") : String(message)
|
|
4777
4993
|
});
|
|
@@ -4958,12 +5174,12 @@ var CacheService = class _CacheService {
|
|
|
4958
5174
|
* Offloads to worker thread if pool is active and payload exceeds threshold.
|
|
4959
5175
|
*/
|
|
4960
5176
|
async _serializeAndEncrypt(serialized) {
|
|
4961
|
-
const isCompressed2 = this.opts.compression && this.opts.compression !== "none" && serialized.length > this.opts.compressionThresholdBytes;
|
|
4962
|
-
if (this._workerPool && (this.enc.isEnabled || isCompressed2) && serialized.length > this.opts.workerThresholdBytes) {
|
|
5177
|
+
const isCompressed2 = (this.opts.customCompressor || this.opts.compression && this.opts.compression !== "none") && serialized.length > this.opts.compressionThresholdBytes;
|
|
5178
|
+
if (this._workerPool && !this.opts.customCompressor && (this.enc.isEnabled || isCompressed2) && serialized.length > this.opts.workerThresholdBytes) {
|
|
4963
5179
|
return this._workerPool.encrypt(serialized);
|
|
4964
5180
|
}
|
|
4965
5181
|
if (isCompressed2) {
|
|
4966
|
-
const compressed = compressBuffer(Buffer.from(serialized, "utf8"), this.opts.compression);
|
|
5182
|
+
const compressed = compressBuffer(Buffer.from(serialized, "utf8"), this.opts.compression, this.opts.customCompressor);
|
|
4967
5183
|
if (this.enc.isEnabled) {
|
|
4968
5184
|
const encrypted = this.enc.encryptBuffer(compressed);
|
|
4969
5185
|
return PREFIX_ENC_COMPRESSED + encrypted.toString("base64");
|
|
@@ -4978,16 +5194,16 @@ var CacheService = class _CacheService {
|
|
|
4978
5194
|
*/
|
|
4979
5195
|
async _decryptAndDeserialize(raw) {
|
|
4980
5196
|
let plain;
|
|
4981
|
-
if (this._workerPool && raw.length > this.opts.workerThresholdBytes) {
|
|
5197
|
+
if (this._workerPool && !this.opts.customCompressor && raw.length > this.opts.workerThresholdBytes) {
|
|
4982
5198
|
plain = await this._workerPool.decrypt(raw);
|
|
4983
5199
|
} else if (raw.startsWith(PREFIX_ENC_COMPRESSED)) {
|
|
4984
5200
|
const encBuf = Buffer.from(raw.slice(PREFIX_ENC_COMPRESSED.length), "base64");
|
|
4985
5201
|
const decBuf = this.enc.decryptBuffer(encBuf);
|
|
4986
|
-
const decompressed = decompressBuffer(decBuf, this.opts.compression !== "none" ? this.opts.compression : "brotli");
|
|
5202
|
+
const decompressed = decompressBuffer(decBuf, this.opts.compression !== "none" ? this.opts.compression : "brotli", this.opts.customCompressor);
|
|
4987
5203
|
plain = decompressed.toString("utf8");
|
|
4988
5204
|
} else if (raw.startsWith(PREFIX_COMPRESSED)) {
|
|
4989
5205
|
const cmpBuf = Buffer.from(raw.slice(PREFIX_COMPRESSED.length), "base64");
|
|
4990
|
-
const decompressed = decompressBuffer(cmpBuf, this.opts.compression !== "none" ? this.opts.compression : "brotli");
|
|
5206
|
+
const decompressed = decompressBuffer(cmpBuf, this.opts.compression !== "none" ? this.opts.compression : "brotli", this.opts.customCompressor);
|
|
4991
5207
|
plain = decompressed.toString("utf8");
|
|
4992
5208
|
} else if (raw.startsWith("enc:v1:") || this.enc.isEnabled) {
|
|
4993
5209
|
plain = this.enc.decrypt(raw);
|
|
@@ -5383,7 +5599,7 @@ var CacheService = class _CacheService {
|
|
|
5383
5599
|
const optRefreshAhead = opts.refreshAhead;
|
|
5384
5600
|
const optXfetchBeta = opts.xfetchBeta;
|
|
5385
5601
|
const optNotFoundTtl = opts.notFoundTtl;
|
|
5386
|
-
const optTags = opts.tags;
|
|
5602
|
+
const optTags = opts.tags ? this.opts.hierarchicalTags ? expandHierarchicalTags(opts.tags, this.opts.tagHierarchyDelimiter) : opts.tags : void 0;
|
|
5387
5603
|
const l1Hit = this.l1.get(k);
|
|
5388
5604
|
if (l1Hit !== null) {
|
|
5389
5605
|
let isGenerationalStale = false;
|
|
@@ -5841,15 +6057,16 @@ var CacheService = class _CacheService {
|
|
|
5841
6057
|
this._pendingDiskDeletes.delete(k);
|
|
5842
6058
|
this.counters.sets++;
|
|
5843
6059
|
let activeTagVersions;
|
|
5844
|
-
|
|
6060
|
+
const tagsToRegister = opts?.tags?.length ? this.opts.hierarchicalTags ? expandHierarchicalTags(opts.tags, this.opts.tagHierarchyDelimiter) : opts.tags : void 0;
|
|
6061
|
+
if (tagsToRegister?.length) {
|
|
5845
6062
|
if (this.opts.tagStrategy === "generational") {
|
|
5846
|
-
const vers = await Promise.all(
|
|
6063
|
+
const vers = await Promise.all(tagsToRegister.map((tag) => this._getTagVersion(tag)));
|
|
5847
6064
|
activeTagVersions = {};
|
|
5848
|
-
for (let i = 0; i <
|
|
5849
|
-
activeTagVersions[
|
|
6065
|
+
for (let i = 0; i < tagsToRegister.length; i++) {
|
|
6066
|
+
activeTagVersions[tagsToRegister[i]] = vers[i];
|
|
5850
6067
|
}
|
|
5851
6068
|
} else {
|
|
5852
|
-
await this._registerTags(k,
|
|
6069
|
+
await this._registerTags(k, tagsToRegister, ttlSeconds);
|
|
5853
6070
|
}
|
|
5854
6071
|
}
|
|
5855
6072
|
this.l1.set(k, data, ttlMs, p, void 0, void 0, activeTagVersions);
|
|
@@ -6464,16 +6681,23 @@ var CacheService = class _CacheService {
|
|
|
6464
6681
|
}
|
|
6465
6682
|
}
|
|
6466
6683
|
}
|
|
6467
|
-
if (missKeys.length > 0) {
|
|
6684
|
+
if (missKeys.length > 0 && fetchFn) {
|
|
6468
6685
|
this.counters.fetches++;
|
|
6469
6686
|
const fetched = await fetchFn(missKeys);
|
|
6687
|
+
const isMap = fetched instanceof Map;
|
|
6470
6688
|
const setPromises = [];
|
|
6471
6689
|
for (let j = 0; j < missKeys.length; j++) {
|
|
6472
|
-
const
|
|
6690
|
+
const k = missKeys[j];
|
|
6691
|
+
const v = isMap ? fetched.get(k) : fetched[k];
|
|
6473
6692
|
result[missIndexes[j]] = v;
|
|
6474
|
-
if (v !== void 0) {
|
|
6475
|
-
const resolvedTtl = typeof ttl === "function" ? ttl(
|
|
6476
|
-
setPromises.push(this.set(
|
|
6693
|
+
if (v !== void 0 && v !== null) {
|
|
6694
|
+
const resolvedTtl = typeof ttl === "function" ? ttl(k) : ttl;
|
|
6695
|
+
setPromises.push(this.set(k, v, resolvedTtl, priority));
|
|
6696
|
+
} else if (v == null && this.opts.notFoundTtl > 0) {
|
|
6697
|
+
setPromises.push(this.set(k, null, this.opts.notFoundTtl, priority));
|
|
6698
|
+
} else if (v === null) {
|
|
6699
|
+
const resolvedTtl = typeof ttl === "function" ? ttl(k) : ttl;
|
|
6700
|
+
setPromises.push(this.set(k, null, resolvedTtl, priority));
|
|
6477
6701
|
}
|
|
6478
6702
|
}
|
|
6479
6703
|
if (setPromises.length > 0) {
|
|
@@ -6616,21 +6840,109 @@ var CacheService = class _CacheService {
|
|
|
6616
6840
|
}
|
|
6617
6841
|
}
|
|
6618
6842
|
/**
|
|
6619
|
-
*
|
|
6620
|
-
*
|
|
6843
|
+
* Discovers all concrete tag names in the system that match `tag` either via
|
|
6844
|
+
* glob wildcards ('*' or '?') or parent-child hierarchy cascading.
|
|
6845
|
+
* Scans local in-memory indices (tagIndex, tagVersions, L1 entries) and Redis (if connected).
|
|
6846
|
+
*/
|
|
6847
|
+
async _resolveMatchingTags(tag, shouldCascade) {
|
|
6848
|
+
const isPattern = isWildcardTag(tag);
|
|
6849
|
+
const delim = this.opts.tagHierarchyDelimiter;
|
|
6850
|
+
const matched = /* @__PURE__ */ new Set();
|
|
6851
|
+
const tagMatches = (t) => {
|
|
6852
|
+
if (t === tag) return true;
|
|
6853
|
+
if (isPattern && matchesTagPattern(tag, t)) return true;
|
|
6854
|
+
if (shouldCascade && isChildTag(tag, t, delim)) return true;
|
|
6855
|
+
return false;
|
|
6856
|
+
};
|
|
6857
|
+
if (shouldCascade && !isPattern) {
|
|
6858
|
+
matched.add(tag);
|
|
6859
|
+
}
|
|
6860
|
+
const tagPrefix = this.nk("_tag_:");
|
|
6861
|
+
for (const tagKey of this.tagIndex.keys()) {
|
|
6862
|
+
const t = tagKey.startsWith(tagPrefix) ? tagKey.slice(tagPrefix.length) : tagKey;
|
|
6863
|
+
if (tagMatches(t)) matched.add(t);
|
|
6864
|
+
}
|
|
6865
|
+
for (const t of this.tagVersions.keys()) {
|
|
6866
|
+
if (tagMatches(t)) matched.add(t);
|
|
6867
|
+
}
|
|
6868
|
+
this.l1.scan((_k, entry) => {
|
|
6869
|
+
if (entry.tagVersions) {
|
|
6870
|
+
for (const t of Object.keys(entry.tagVersions)) {
|
|
6871
|
+
if (tagMatches(t)) matched.add(t);
|
|
6872
|
+
}
|
|
6873
|
+
}
|
|
6874
|
+
}, 0);
|
|
6875
|
+
if (!this._redisDisabled) {
|
|
6876
|
+
try {
|
|
6877
|
+
const client = await this.getRedis();
|
|
6878
|
+
if (this.opts.tagStrategy === "generational") {
|
|
6879
|
+
const redisPattern = isPattern ? this.nk(`tag_ver:${tag}`) : this.nk(`tag_ver:${tag}${delim}*`);
|
|
6880
|
+
const scannedKeys = await this._scanKeys(client, redisPattern);
|
|
6881
|
+
const pfx = this.nk("tag_ver:");
|
|
6882
|
+
for (const sk of scannedKeys) {
|
|
6883
|
+
const t = sk.startsWith(pfx) ? sk.slice(pfx.length) : sk;
|
|
6884
|
+
if (tagMatches(t)) matched.add(t);
|
|
6885
|
+
}
|
|
6886
|
+
} else {
|
|
6887
|
+
const redisPattern = isPattern ? this.nk(`_tag_:${tag}`) : this.nk(`_tag_:${tag}${delim}*`);
|
|
6888
|
+
const scannedKeys = await this._scanKeys(client, redisPattern);
|
|
6889
|
+
const pfx = this.nk("_tag_:");
|
|
6890
|
+
for (const sk of scannedKeys) {
|
|
6891
|
+
const t = sk.startsWith(pfx) ? sk.slice(pfx.length) : sk;
|
|
6892
|
+
if (tagMatches(t)) matched.add(t);
|
|
6893
|
+
}
|
|
6894
|
+
}
|
|
6895
|
+
} catch (err) {
|
|
6896
|
+
this.logger.debug("_resolveMatchingTags: Redis scan failed", { tag, error: err.message });
|
|
6897
|
+
}
|
|
6898
|
+
}
|
|
6899
|
+
return matched;
|
|
6900
|
+
}
|
|
6901
|
+
/**
|
|
6902
|
+
* Invalidate all keys associated with a tag, wildcard glob pattern, or hierarchical namespace.
|
|
6903
|
+
* Deletes from L1, disk, and Redis (both the keyed values and the tag metadata).
|
|
6904
|
+
*
|
|
6905
|
+
* @param tag Tag name or wildcard glob pattern (e.g. `'catalog'`, `'org:123:*'`, `'*:comments'`).
|
|
6906
|
+
* @param opts Optional invalidation options, including `{ cascade: true }` to invalidate child namespaces.
|
|
6621
6907
|
*
|
|
6622
6908
|
* @example
|
|
6623
6909
|
* await cache.set('product:1', data, 60, undefined, { tags: ['catalog'] });
|
|
6624
6910
|
* await cache.invalidateTag('catalog'); // clears product:1 and any other tagged entries
|
|
6911
|
+
*
|
|
6912
|
+
* @example
|
|
6913
|
+
* // Wildcard pattern invalidation
|
|
6914
|
+
* await cache.invalidateTag('org:123:*');
|
|
6915
|
+
*
|
|
6916
|
+
* @example
|
|
6917
|
+
* // Hierarchical tag cascading (clears 'org:123' and all child tags like 'org:123:dept:456')
|
|
6918
|
+
* await cache.invalidateTag('org:123', { cascade: true });
|
|
6625
6919
|
*/
|
|
6626
|
-
async invalidateTag(tag) {
|
|
6920
|
+
async invalidateTag(tag, opts) {
|
|
6627
6921
|
this._assertActive("invalidateTag");
|
|
6628
6922
|
const span = this._startSpan("tricache.invalidate_tag");
|
|
6629
6923
|
if (this.opts.tracer) {
|
|
6630
6924
|
span.setAttribute("cache.tag", tag);
|
|
6631
6925
|
span.setAttribute("cache.tag_strategy", this.opts.tagStrategy);
|
|
6926
|
+
if (opts?.cascade) span.setAttribute("cache.cascade", true);
|
|
6632
6927
|
}
|
|
6633
6928
|
try {
|
|
6929
|
+
const isPattern = isWildcardTag(tag);
|
|
6930
|
+
const shouldCascade = Boolean(opts?.cascade || this.opts.hierarchicalTags);
|
|
6931
|
+
if (isPattern || shouldCascade) {
|
|
6932
|
+
const matchedTags = await this._resolveMatchingTags(tag, shouldCascade);
|
|
6933
|
+
const delim = this.opts.tagHierarchyDelimiter;
|
|
6934
|
+
const broadcastPattern = isPattern ? tag : `${tag}${delim}*`;
|
|
6935
|
+
if (matchedTags.size > 0) {
|
|
6936
|
+
await this.invalidateTags(Array.from(matchedTags));
|
|
6937
|
+
}
|
|
6938
|
+
const op = this.opts.tagStrategy === "generational" ? "tag_pattern_incr" : "tag_pattern_del";
|
|
6939
|
+
if (this.opts.awaitInvalidationBackplane) {
|
|
6940
|
+
await this.publishInvalidation(op, broadcastPattern, void 0, false, true);
|
|
6941
|
+
} else {
|
|
6942
|
+
void this.publishInvalidation(op, broadcastPattern, void 0, false, true);
|
|
6943
|
+
}
|
|
6944
|
+
return;
|
|
6945
|
+
}
|
|
6634
6946
|
if (this.opts.tagStrategy === "generational") {
|
|
6635
6947
|
let newVer = 1;
|
|
6636
6948
|
if (!this._redisDisabled) {
|
|
@@ -6693,21 +7005,35 @@ var CacheService = class _CacheService {
|
|
|
6693
7005
|
}
|
|
6694
7006
|
/**
|
|
6695
7007
|
* Invalidate all keys associated with any of the given tags in a single operation.
|
|
6696
|
-
*
|
|
6697
|
-
*
|
|
7008
|
+
* Supports discrete tags, wildcard glob patterns, and hierarchical cascading.
|
|
7009
|
+
* Combines multiple tag invalidations into Redis pipeline round-trips.
|
|
6698
7010
|
*
|
|
6699
|
-
*
|
|
6700
|
-
*
|
|
7011
|
+
* @param tags Array of tag names or wildcard patterns (e.g. `['case:acme', 'org:*']`).
|
|
7012
|
+
* @param opts Optional invalidation options, including `{ cascade: true }`.
|
|
6701
7013
|
*
|
|
6702
7014
|
* @example
|
|
6703
|
-
*
|
|
6704
|
-
* await cache.invalidateTags(['case:acme', 'org:acme', 'ai-chat:acme']);
|
|
7015
|
+
* await cache.invalidateTags(['case:acme', 'org:acme', 'ai-chat:*']);
|
|
6705
7016
|
*/
|
|
6706
|
-
async invalidateTags(tags) {
|
|
7017
|
+
async invalidateTags(tags, opts) {
|
|
6707
7018
|
this._assertActive("invalidateTags");
|
|
6708
7019
|
if (tags.length === 0) return;
|
|
7020
|
+
const hasWildcards = tags.some((t) => isWildcardTag(t));
|
|
7021
|
+
const shouldCascade = Boolean(opts?.cascade || this.opts.hierarchicalTags);
|
|
7022
|
+
if (hasWildcards || shouldCascade) {
|
|
7023
|
+
const allResolvedTags = /* @__PURE__ */ new Set();
|
|
7024
|
+
for (const t of tags) {
|
|
7025
|
+
if (isWildcardTag(t) || shouldCascade) {
|
|
7026
|
+
const resolved = await this._resolveMatchingTags(t, shouldCascade);
|
|
7027
|
+
for (const rt of resolved) allResolvedTags.add(rt);
|
|
7028
|
+
} else {
|
|
7029
|
+
allResolvedTags.add(t);
|
|
7030
|
+
}
|
|
7031
|
+
}
|
|
7032
|
+
if (allResolvedTags.size === 0) return;
|
|
7033
|
+
tags = Array.from(allResolvedTags);
|
|
7034
|
+
}
|
|
6709
7035
|
if (tags.length === 1) {
|
|
6710
|
-
await this.invalidateTag(tags[0]);
|
|
7036
|
+
await this.invalidateTag(tags[0], opts);
|
|
6711
7037
|
return;
|
|
6712
7038
|
}
|
|
6713
7039
|
if (this.opts.tagStrategy === "generational") {
|
package/dist/nestjs/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { C as CacheOptions,
|
|
2
|
-
import { C as CacheService } from '../cache-service-
|
|
1
|
+
import { C as CacheOptions, f as CachePriority } from '../types-5HXqBfAf.js';
|
|
2
|
+
import { C as CacheService } from '../cache-service-pJ-nFcVj.js';
|
|
3
3
|
import 'crypto';
|
|
4
4
|
|
|
5
5
|
declare const TRICACHE_SERVICE = "TRICACHE_SERVICE";
|
package/dist/nestjs/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import "../chunk-
|
|
1
|
+
import "../chunk-VXFTFZ5N.js";
|
|
2
2
|
import {
|
|
3
3
|
CacheService
|
|
4
|
-
} from "../chunk-
|
|
5
|
-
import "../chunk-
|
|
4
|
+
} from "../chunk-7SZSQS55.js";
|
|
5
|
+
import "../chunk-UBWLPKTI.js";
|
|
6
6
|
import "../chunk-7RV5ZVBB.js";
|
|
7
7
|
import "../chunk-UGD4VEPD.js";
|
|
8
|
-
import "../chunk-
|
|
8
|
+
import "../chunk-2AJKEQMZ.js";
|
|
9
9
|
|
|
10
10
|
// src/nestjs/tricache.store.ts
|
|
11
11
|
var TriCacheStore = class {
|