tricache 1.0.0 → 1.0.2

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.
Files changed (97) hide show
  1. package/README.md +36 -28
  2. package/dist/{cache-service-pJ-nFcVj.d.ts → cache-service-B108FzER.d.ts} +63 -3
  3. package/dist/cache-service-CWI4jkS7.d.cts +963 -0
  4. package/dist/{cache-service-26GCO3UU.js → cache-service-GYPZFB7O.js} +2 -2
  5. package/dist/{chunk-IITRFWHY.js → chunk-EUKB7RKP.js} +21 -4
  6. package/dist/{chunk-2AJKEQMZ.js → chunk-H4GFLQ26.js} +7 -4
  7. package/dist/{chunk-7RV5ZVBB.js → chunk-JNSLMK4Q.js} +111 -29
  8. package/dist/chunk-OYDU3SMI.js +89 -0
  9. package/dist/{chunk-VXFTFZ5N.js → chunk-VNRDL5RA.js} +117 -138
  10. package/dist/{chunk-7SZSQS55.js → chunk-WYF7NOAT.js} +1831 -759
  11. package/dist/cli.cjs +1757 -777
  12. package/dist/cli.d.cts +12 -0
  13. package/dist/cli.js +27 -6
  14. package/dist/dashboard/index.cjs +111 -29
  15. package/dist/dashboard/index.d.cts +185 -0
  16. package/dist/dashboard/index.d.ts +5 -3
  17. package/dist/dashboard/index.js +1 -1
  18. package/dist/drizzle/index.cjs +1757 -773
  19. package/dist/drizzle/index.d.cts +46 -0
  20. package/dist/drizzle/index.d.ts +2 -2
  21. package/dist/drizzle/index.js +1 -1
  22. package/dist/edge/index.cjs +280 -153
  23. package/dist/edge/index.d.cts +127 -0
  24. package/dist/edge/index.d.ts +3 -3
  25. package/dist/edge/index.js +254 -147
  26. package/dist/hono/index.cjs +1805 -793
  27. package/dist/hono/index.d.cts +93 -0
  28. package/dist/hono/index.d.ts +7 -5
  29. package/dist/hono/index.js +16 -9
  30. package/dist/{hono-DuNvciyq.d.ts → hono-3rDMYVtY.d.ts} +23 -3
  31. package/dist/hono-C886ok3X.d.cts +391 -0
  32. package/dist/http/index.cjs +1946 -842
  33. package/dist/http/index.d.cts +103 -0
  34. package/dist/http/index.d.ts +13 -5
  35. package/dist/http/index.js +137 -57
  36. package/dist/index.cjs +2073 -937
  37. package/dist/index.d.cts +1006 -0
  38. package/dist/index.d.ts +40 -8
  39. package/dist/index.js +8 -8
  40. package/dist/murmur3-CMGw9PbV.d.cts +98 -0
  41. package/dist/nestjs/index.cjs +1849 -806
  42. package/dist/nestjs/index.d.cts +285 -0
  43. package/dist/nestjs/index.d.ts +103 -30
  44. package/dist/nestjs/index.js +115 -32
  45. package/dist/next/index.cjs +1913 -887
  46. package/dist/next/index.d.cts +232 -0
  47. package/dist/next/index.d.ts +33 -11
  48. package/dist/next/index.js +174 -107
  49. package/dist/prisma/index.cjs +11 -1
  50. package/dist/prisma/index.d.cts +62 -0
  51. package/dist/prisma/index.d.ts +2 -2
  52. package/dist/prisma/index.js +11 -1
  53. package/dist/serialize-worker.d.cts +2 -0
  54. package/dist/{types-5HXqBfAf.d.ts → types-BA4pz1A0.d.cts} +26 -9
  55. package/dist/types-BA4pz1A0.d.ts +1519 -0
  56. package/dist/{utils-CEsU_Z0s.d.ts → utils-Bvzn-rKS.d.cts} +3 -1
  57. package/dist/utils-Bvzn-rKS.d.ts +29 -0
  58. package/package.json +96 -36
  59. package/src/adapters/auto-pipeliner.ts +107 -24
  60. package/src/adapters/node-redis.ts +123 -25
  61. package/src/cache-service.ts +1238 -659
  62. package/src/cli.ts +24 -6
  63. package/src/cross-region.ts +21 -3
  64. package/src/dashboard/handler.ts +26 -6
  65. package/src/dashboard/html.ts +35 -10
  66. package/src/dashboard/index.ts +35 -13
  67. package/src/dashboard/types.ts +3 -1
  68. package/src/disk-tier.ts +171 -65
  69. package/src/edge/cache.ts +202 -129
  70. package/src/edge/hono.ts +23 -6
  71. package/src/edge/types.ts +2 -0
  72. package/src/hono/index.ts +19 -9
  73. package/src/http/express.ts +110 -26
  74. package/src/http/fastify.ts +38 -28
  75. package/src/http/utils.ts +54 -20
  76. package/src/ipc-telemetry.ts +35 -9
  77. package/src/nestjs/decorators.ts +1 -1
  78. package/src/nestjs/index.ts +2 -0
  79. package/src/nestjs/keyv-adapter.ts +68 -0
  80. package/src/nestjs/tricache.module.ts +13 -11
  81. package/src/nestjs/tricache.store.ts +51 -20
  82. package/src/nestjs/types.ts +29 -15
  83. package/src/next/cache-handler.ts +132 -57
  84. package/src/next/index.ts +1 -0
  85. package/src/next/isr-handler.ts +1 -4
  86. package/src/next/types.ts +21 -0
  87. package/src/prisma/index.ts +12 -1
  88. package/src/smart-memory-cache.ts +65 -27
  89. package/src/types.ts +27 -2
  90. package/src/utils/cgroup.ts +12 -0
  91. package/src/utils/deadline.ts +14 -0
  92. package/src/utils/diagnostics.ts +27 -0
  93. package/src/utils/redis-slot.ts +13 -0
  94. package/src/wasm/bloom-filter-wasm.ts +9 -4
  95. package/src/worker-pool.ts +17 -8
  96. package/src/wtiny-lfu.ts +20 -6
  97. package/dist/chunk-BQRURGGD.js +0 -60
@@ -1,25 +1,28 @@
1
- import "../chunk-VXFTFZ5N.js";
1
+ import "../chunk-VNRDL5RA.js";
2
2
  import {
3
3
  CacheService
4
- } from "../chunk-7SZSQS55.js";
4
+ } from "../chunk-WYF7NOAT.js";
5
5
  import "../chunk-UBWLPKTI.js";
6
- import "../chunk-7RV5ZVBB.js";
6
+ import "../chunk-JNSLMK4Q.js";
7
7
  import "../chunk-UGD4VEPD.js";
8
- import "../chunk-2AJKEQMZ.js";
8
+ import "../chunk-H4GFLQ26.js";
9
9
 
10
10
  // src/nestjs/tricache.store.ts
11
- var TriCacheStore = class {
11
+ import { EventEmitter } from "events";
12
+ var TriCacheStore = class extends EventEmitter {
12
13
  constructor(cache) {
14
+ super();
13
15
  this.cache = cache;
14
16
  }
15
17
  cache;
16
- /**
17
- * Retrieve an item from the cache.
18
- * Returns undefined on miss (matching cache-manager v5/v6 contract).
19
- */
20
- async get(key) {
18
+ async get(key, options) {
21
19
  const val = await this.cache.peek(key);
22
- return val !== null ? val : void 0;
20
+ if (val === null) return void 0;
21
+ if (options?.raw) {
22
+ const remaining = this.cache.ttl(key);
23
+ return { value: val, expires: remaining === null ? void 0 : Date.now() + remaining * 1e3 };
24
+ }
25
+ return val;
23
26
  }
24
27
  /**
25
28
  * Set an item in the cache.
@@ -28,48 +31,49 @@ var TriCacheStore = class {
28
31
  * @param ttl - TTL in milliseconds (0 = indefinite / default engine TTL)
29
32
  */
30
33
  async set(key, value, ttl) {
31
- const ttlSeconds = typeof ttl === "number" && ttl > 0 ? Math.round(ttl / 1e3) : ttl === 0 ? 0 : void 0;
34
+ const ttlSeconds = typeof ttl === "number" && ttl > 0 ? ttl / 1e3 : ttl === 0 ? 0 : void 0;
32
35
  await this.cache.set(key, value, ttlSeconds);
36
+ return value;
33
37
  }
34
38
  /**
35
39
  * Remove an item from the cache.
36
40
  */
37
41
  async del(key) {
38
42
  await this.cache.delete(key);
43
+ return true;
39
44
  }
40
45
  /**
41
46
  * Clear the entire cache namespace.
42
47
  */
43
48
  async reset() {
44
49
  await this.cache.clear();
50
+ return true;
45
51
  }
46
- /**
47
- * Batch get multiple items from the cache.
48
- */
49
- async mget(...keys) {
52
+ async mget(keysOrFirst = [], ...rest) {
53
+ const keys = Array.isArray(keysOrFirst) ? keysOrFirst : [keysOrFirst, ...rest];
50
54
  if (keys.length === 0) return [];
51
- return this.cache.mget(keys, async () => ({}));
55
+ return this.cache.mget(keys);
52
56
  }
53
57
  /**
54
58
  * Batch set multiple entries with per-entry millisecond TTL.
55
59
  */
56
60
  async mset(entries) {
57
- if (entries.length === 0) return;
61
+ if (entries.length === 0) return [];
58
62
  const batch = {};
59
63
  for (const e of entries) {
60
64
  batch[e.key] = {
61
65
  value: e.value,
62
- ttl: typeof e.ttl === "number" && e.ttl > 0 ? Math.round(e.ttl / 1e3) : e.ttl === 0 ? 0 : void 0
66
+ ttl: typeof e.ttl === "number" && e.ttl > 0 ? e.ttl / 1e3 : e.ttl === 0 ? 0 : void 0
63
67
  };
64
68
  }
65
69
  await this.cache.mset(batch);
70
+ return entries;
66
71
  }
67
- /**
68
- * Batch delete multiple keys.
69
- */
70
- async mdel(...keys) {
71
- if (keys.length === 0) return;
72
+ async mdel(keysOrFirst = [], ...rest) {
73
+ const keys = Array.isArray(keysOrFirst) ? keysOrFirst : [keysOrFirst, ...rest];
74
+ if (keys.length === 0) return true;
72
75
  await this.cache.mdel(keys);
76
+ return true;
73
77
  }
74
78
  /**
75
79
  * Scan keys currently resident in the cache.
@@ -88,6 +92,24 @@ var TriCacheStore = class {
88
92
  const rem = this.cache.ttl(key);
89
93
  return rem !== null ? rem * 1e3 : void 0;
90
94
  }
95
+ async wrap(key, fetch, ttl, refreshThreshold) {
96
+ const seconds = ttl === void 0 ? 0 : ttl / 1e3;
97
+ const refreshAhead = ttl && refreshThreshold ? Math.max(0, Math.min(1, (ttl - refreshThreshold) / ttl)) : void 0;
98
+ return this.cache.wrap(key, async () => await fetch(), { ttl: seconds, refreshAhead });
99
+ }
100
+ cacheId() {
101
+ return `tricache:${this.cache.options.namespace ?? "default"}`;
102
+ }
103
+ get stores() {
104
+ return [this];
105
+ }
106
+ async clear() {
107
+ await this.reset();
108
+ return true;
109
+ }
110
+ async disconnect() {
111
+ return void 0;
112
+ }
91
113
  };
92
114
 
93
115
  // src/nestjs/types.ts
@@ -99,17 +121,17 @@ var TriCacheModule = class _TriCacheModule {
99
121
  /**
100
122
  * Register TriCache module synchronously with static configuration options.
101
123
  */
102
- static register(options = {}) {
124
+ static register(options = {}, moduleOptions = {}) {
103
125
  const cache = CacheService.create(options);
104
- const store = new TriCacheStore(cache);
126
+ const provideCacheManager = moduleOptions.provideCacheManager ?? true;
105
127
  return {
106
128
  module: _TriCacheModule,
107
129
  global: true,
108
130
  providers: [
109
131
  { provide: TRICACHE_SERVICE, useValue: cache },
110
- { provide: CACHE_MANAGER, useValue: store }
132
+ ...provideCacheManager ? [{ provide: CACHE_MANAGER, useValue: new TriCacheStore(cache) }] : []
111
133
  ],
112
- exports: [TRICACHE_SERVICE, CACHE_MANAGER]
134
+ exports: [TRICACHE_SERVICE, ...provideCacheManager ? [CACHE_MANAGER] : []]
113
135
  };
114
136
  }
115
137
  /**
@@ -117,20 +139,21 @@ var TriCacheModule = class _TriCacheModule {
117
139
  */
118
140
  static registerAsync(asyncOptions) {
119
141
  const asyncProviders = this.createAsyncProviders(asyncOptions);
142
+ const provideCacheManager = asyncOptions.provideCacheManager ?? true;
120
143
  return {
121
144
  module: _TriCacheModule,
122
145
  global: true,
123
146
  imports: asyncOptions.imports || [],
124
147
  providers: [
125
148
  ...asyncProviders,
126
- {
149
+ ...provideCacheManager ? [{
127
150
  provide: CACHE_MANAGER,
128
151
  useFactory: (cache) => new TriCacheStore(cache),
129
152
  inject: [TRICACHE_SERVICE]
130
- },
153
+ }] : [],
131
154
  ...asyncOptions.extraProviders || []
132
155
  ],
133
- exports: [TRICACHE_SERVICE, CACHE_MANAGER]
156
+ exports: [TRICACHE_SERVICE, ...provideCacheManager ? [CACHE_MANAGER] : []]
134
157
  };
135
158
  }
136
159
  static createAsyncProviders(options) {
@@ -171,6 +194,65 @@ var TriCacheModule = class _TriCacheModule {
171
194
  }
172
195
  };
173
196
 
197
+ // src/nestjs/keyv-adapter.ts
198
+ import { EventEmitter as EventEmitter2 } from "events";
199
+ var TriCacheKeyvAdapter = class extends EventEmitter2 {
200
+ constructor(cache) {
201
+ super();
202
+ this.cache = cache;
203
+ }
204
+ cache;
205
+ _namespace;
206
+ get namespace() {
207
+ return this._namespace;
208
+ }
209
+ set namespace(value) {
210
+ if (value && ["*", "?", "[", "]", "\\"].some((character) => value.includes(character))) {
211
+ throw new Error("Keyv namespace must not contain glob characters");
212
+ }
213
+ this._namespace = value;
214
+ }
215
+ async get(key) {
216
+ const value = await this.cache.peek(key);
217
+ return value === null ? void 0 : value;
218
+ }
219
+ async set(key, value, ttl) {
220
+ if (ttl !== void 0 && (!Number.isFinite(ttl) || ttl < 0)) throw new Error("Keyv TTL must be finite and non-negative");
221
+ const ttlSeconds = ttl === void 0 ? 0 : ttl / 1e3;
222
+ await this.cache.set(key, value, ttlSeconds);
223
+ return true;
224
+ }
225
+ async delete(key) {
226
+ const found = await this.has(key);
227
+ await this.cache.delete(key);
228
+ return found;
229
+ }
230
+ async clear() {
231
+ await this.cache.clear(this._namespace ? `${this._namespace}:` : void 0);
232
+ }
233
+ async has(key) {
234
+ return await this.cache.peek(key) !== null;
235
+ }
236
+ async getMany(keys) {
237
+ return this.cache.mget(keys);
238
+ }
239
+ async deleteMany(keys) {
240
+ const found = await this.cache.mget(keys);
241
+ await this.cache.mdel(keys);
242
+ return found.every((value) => value !== void 0 && value !== null);
243
+ }
244
+ async setMany(entries) {
245
+ const results = await Promise.all(entries.map((entry) => this.set(entry.key, entry.value, entry.ttl)));
246
+ return results;
247
+ }
248
+ async hasMany(keys) {
249
+ const values = await this.cache.mget(keys);
250
+ return values.map((value) => value !== null && value !== void 0);
251
+ }
252
+ async disconnect() {
253
+ }
254
+ };
255
+
174
256
  // src/nestjs/decorators.ts
175
257
  function resolveCacheInstance(ctx) {
176
258
  return ctx?.cacheService || ctx?.cache || ctx?.cacheStore?.cache || CacheService.create();
@@ -190,7 +272,7 @@ function Cacheable(options = {}) {
190
272
  const rawTtl = options.ttl;
191
273
  if (typeof rawTtl === "number") {
192
274
  if (options.ttlUnit === "milliseconds" || rawTtl >= 1e3 && rawTtl % 1e3 === 0 && options.ttlUnit !== "seconds") {
193
- ttlSeconds = Math.round(rawTtl / 1e3);
275
+ ttlSeconds = rawTtl / 1e3;
194
276
  }
195
277
  }
196
278
  const tags = typeof options.tags === "function" ? options.tags(...args) : options.tags;
@@ -247,6 +329,7 @@ export {
247
329
  CacheEvict,
248
330
  Cacheable,
249
331
  TRICACHE_SERVICE,
332
+ TriCacheKeyvAdapter,
250
333
  TriCacheModule,
251
334
  TriCacheStore,
252
335
  nestjs_default as default