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.
- package/README.md +36 -28
- package/dist/{cache-service-pJ-nFcVj.d.ts → cache-service-B108FzER.d.ts} +63 -3
- package/dist/cache-service-CWI4jkS7.d.cts +963 -0
- package/dist/{cache-service-26GCO3UU.js → cache-service-GYPZFB7O.js} +2 -2
- package/dist/{chunk-IITRFWHY.js → chunk-EUKB7RKP.js} +21 -4
- package/dist/{chunk-2AJKEQMZ.js → chunk-H4GFLQ26.js} +7 -4
- package/dist/{chunk-7RV5ZVBB.js → chunk-JNSLMK4Q.js} +111 -29
- package/dist/chunk-OYDU3SMI.js +89 -0
- package/dist/{chunk-VXFTFZ5N.js → chunk-VNRDL5RA.js} +117 -138
- package/dist/{chunk-7SZSQS55.js → chunk-WYF7NOAT.js} +1831 -759
- package/dist/cli.cjs +1757 -777
- package/dist/cli.d.cts +12 -0
- package/dist/cli.js +27 -6
- package/dist/dashboard/index.cjs +111 -29
- package/dist/dashboard/index.d.cts +185 -0
- package/dist/dashboard/index.d.ts +5 -3
- package/dist/dashboard/index.js +1 -1
- package/dist/drizzle/index.cjs +1757 -773
- package/dist/drizzle/index.d.cts +46 -0
- package/dist/drizzle/index.d.ts +2 -2
- package/dist/drizzle/index.js +1 -1
- package/dist/edge/index.cjs +280 -153
- package/dist/edge/index.d.cts +127 -0
- package/dist/edge/index.d.ts +3 -3
- package/dist/edge/index.js +254 -147
- package/dist/hono/index.cjs +1805 -793
- package/dist/hono/index.d.cts +93 -0
- package/dist/hono/index.d.ts +7 -5
- package/dist/hono/index.js +16 -9
- package/dist/{hono-DuNvciyq.d.ts → hono-3rDMYVtY.d.ts} +23 -3
- package/dist/hono-C886ok3X.d.cts +391 -0
- package/dist/http/index.cjs +1946 -842
- package/dist/http/index.d.cts +103 -0
- package/dist/http/index.d.ts +13 -5
- package/dist/http/index.js +137 -57
- package/dist/index.cjs +2073 -937
- package/dist/index.d.cts +1006 -0
- package/dist/index.d.ts +40 -8
- package/dist/index.js +8 -8
- package/dist/murmur3-CMGw9PbV.d.cts +98 -0
- package/dist/nestjs/index.cjs +1849 -806
- package/dist/nestjs/index.d.cts +285 -0
- package/dist/nestjs/index.d.ts +103 -30
- package/dist/nestjs/index.js +115 -32
- package/dist/next/index.cjs +1913 -887
- package/dist/next/index.d.cts +232 -0
- package/dist/next/index.d.ts +33 -11
- package/dist/next/index.js +174 -107
- package/dist/prisma/index.cjs +11 -1
- package/dist/prisma/index.d.cts +62 -0
- package/dist/prisma/index.d.ts +2 -2
- package/dist/prisma/index.js +11 -1
- package/dist/serialize-worker.d.cts +2 -0
- package/dist/{types-5HXqBfAf.d.ts → types-BA4pz1A0.d.cts} +26 -9
- package/dist/types-BA4pz1A0.d.ts +1519 -0
- package/dist/{utils-CEsU_Z0s.d.ts → utils-Bvzn-rKS.d.cts} +3 -1
- package/dist/utils-Bvzn-rKS.d.ts +29 -0
- package/package.json +96 -36
- package/src/adapters/auto-pipeliner.ts +107 -24
- package/src/adapters/node-redis.ts +123 -25
- package/src/cache-service.ts +1238 -659
- package/src/cli.ts +24 -6
- package/src/cross-region.ts +21 -3
- package/src/dashboard/handler.ts +26 -6
- package/src/dashboard/html.ts +35 -10
- package/src/dashboard/index.ts +35 -13
- package/src/dashboard/types.ts +3 -1
- package/src/disk-tier.ts +171 -65
- package/src/edge/cache.ts +202 -129
- package/src/edge/hono.ts +23 -6
- package/src/edge/types.ts +2 -0
- package/src/hono/index.ts +19 -9
- package/src/http/express.ts +110 -26
- package/src/http/fastify.ts +38 -28
- package/src/http/utils.ts +54 -20
- package/src/ipc-telemetry.ts +35 -9
- package/src/nestjs/decorators.ts +1 -1
- package/src/nestjs/index.ts +2 -0
- package/src/nestjs/keyv-adapter.ts +68 -0
- package/src/nestjs/tricache.module.ts +13 -11
- package/src/nestjs/tricache.store.ts +51 -20
- package/src/nestjs/types.ts +29 -15
- package/src/next/cache-handler.ts +132 -57
- package/src/next/index.ts +1 -0
- package/src/next/isr-handler.ts +1 -4
- package/src/next/types.ts +21 -0
- package/src/prisma/index.ts +12 -1
- package/src/smart-memory-cache.ts +65 -27
- package/src/types.ts +27 -2
- package/src/utils/cgroup.ts +12 -0
- package/src/utils/deadline.ts +14 -0
- package/src/utils/diagnostics.ts +27 -0
- package/src/utils/redis-slot.ts +13 -0
- package/src/wasm/bloom-filter-wasm.ts +9 -4
- package/src/worker-pool.ts +17 -8
- package/src/wtiny-lfu.ts +20 -6
- package/dist/chunk-BQRURGGD.js +0 -60
package/src/ipc-telemetry.ts
CHANGED
|
@@ -56,6 +56,8 @@ export class IpcTelemetryServer {
|
|
|
56
56
|
private server: net.Server | null = null;
|
|
57
57
|
public readonly socketPath: string;
|
|
58
58
|
private _closed = false;
|
|
59
|
+
private _starting: Promise<void> | null = null;
|
|
60
|
+
private readonly sockets = new Set<net.Socket>();
|
|
59
61
|
private _exitHandler: (() => void) | null = null;
|
|
60
62
|
private _sigintHandler: (() => void) | null = null;
|
|
61
63
|
private _sigtermHandler: (() => void) | null = null;
|
|
@@ -71,12 +73,20 @@ export class IpcTelemetryServer {
|
|
|
71
73
|
* Start listening on the IPC socket / pipe.
|
|
72
74
|
*/
|
|
73
75
|
async start(): Promise<void> {
|
|
74
|
-
if (this.
|
|
76
|
+
if (this._closed || this.server?.listening) return;
|
|
77
|
+
if (this._starting) return this._starting;
|
|
78
|
+
this._starting = this.startInternal();
|
|
79
|
+
try { await this._starting; }
|
|
80
|
+
finally { this._starting = null; }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private async startInternal(): Promise<void> {
|
|
75
84
|
|
|
76
85
|
// Hygiene on POSIX: check if an orphaned socket file exists from a crashed run
|
|
77
86
|
if (process.platform !== 'win32' && fs.existsSync(this.socketPath)) {
|
|
78
87
|
await new Promise<void>((resolve) => {
|
|
79
88
|
const testSocket = net.createConnection(this.socketPath);
|
|
89
|
+
testSocket.setTimeout(1000, () => { testSocket.destroy(); resolve(); });
|
|
80
90
|
testSocket.once('connect', () => {
|
|
81
91
|
testSocket.destroy();
|
|
82
92
|
// Active server already listening on this socket
|
|
@@ -94,22 +104,30 @@ export class IpcTelemetryServer {
|
|
|
94
104
|
});
|
|
95
105
|
});
|
|
96
106
|
}
|
|
107
|
+
if (this._closed) return;
|
|
97
108
|
|
|
98
109
|
this.server = net.createServer((socket) => {
|
|
110
|
+
if (this._closed) { socket.destroy(); return; }
|
|
111
|
+
this.sockets.add(socket);
|
|
112
|
+
socket.once('close', () => this.sockets.delete(socket));
|
|
113
|
+
socket.setTimeout(5000, () => socket.destroy());
|
|
99
114
|
let buffer = '';
|
|
115
|
+
let pending = 0;
|
|
100
116
|
|
|
101
117
|
socket.on('data', (chunk) => {
|
|
118
|
+
if (Buffer.byteLength(buffer) + chunk.length > 4096 || socket.writableLength > 65536) { socket.destroy(); return; }
|
|
102
119
|
buffer += chunk.toString('utf8');
|
|
103
120
|
let newlineIndex: number;
|
|
104
121
|
while ((newlineIndex = buffer.indexOf('\n')) !== -1) {
|
|
105
122
|
const line = buffer.slice(0, newlineIndex).trim();
|
|
106
123
|
buffer = buffer.slice(newlineIndex + 1);
|
|
107
124
|
if (!line) continue;
|
|
125
|
+
if (++pending > 8) { socket.destroy(); return; }
|
|
108
126
|
|
|
109
127
|
// Non-blocking telemetry pull: pull stats snapshot on tick boundaries
|
|
110
128
|
// using setImmediate so serialization never introduces event loop stalls.
|
|
111
129
|
setImmediate(() => {
|
|
112
|
-
if (socket.destroyed) return;
|
|
130
|
+
if (socket.destroyed) { pending--; return; }
|
|
113
131
|
try {
|
|
114
132
|
let cmd = line;
|
|
115
133
|
try {
|
|
@@ -138,6 +156,8 @@ export class IpcTelemetryServer {
|
|
|
138
156
|
if (!socket.destroyed) {
|
|
139
157
|
socket.write(JSON.stringify({ error: (err as Error).message }) + '\n');
|
|
140
158
|
}
|
|
159
|
+
} finally {
|
|
160
|
+
pending--;
|
|
141
161
|
}
|
|
142
162
|
});
|
|
143
163
|
}
|
|
@@ -147,24 +167,29 @@ export class IpcTelemetryServer {
|
|
|
147
167
|
// Suppress client disconnection errors on IPC socket
|
|
148
168
|
});
|
|
149
169
|
});
|
|
170
|
+
this.server.maxConnections = 128;
|
|
150
171
|
|
|
151
172
|
await new Promise<void>((resolve, reject) => {
|
|
152
|
-
this.server
|
|
153
|
-
|
|
154
|
-
|
|
173
|
+
const server = this.server!;
|
|
174
|
+
const cleanup = () => { server.off('error', failed); server.off('close', closed); };
|
|
175
|
+
const failed = (error: Error) => { cleanup(); reject(error); };
|
|
176
|
+
const closed = () => { cleanup(); resolve(); }; // close() can cancel listen before its callback fires.
|
|
177
|
+
server.once('error', failed);
|
|
178
|
+
server.once('close', closed);
|
|
179
|
+
server.listen(this.socketPath, () => {
|
|
180
|
+
cleanup();
|
|
155
181
|
resolve();
|
|
156
182
|
});
|
|
157
183
|
});
|
|
184
|
+
if (this._closed) { if (this.server.listening) this.server.close(); return; }
|
|
158
185
|
|
|
159
186
|
// Register process termination handlers to guarantee socket hygiene
|
|
160
187
|
this._exitHandler = () => this._unlinkSocket();
|
|
161
188
|
this._sigintHandler = () => {
|
|
162
189
|
this._unlinkSocket();
|
|
163
|
-
process.exit(0);
|
|
164
190
|
};
|
|
165
191
|
this._sigtermHandler = () => {
|
|
166
192
|
this._unlinkSocket();
|
|
167
|
-
process.exit(0);
|
|
168
193
|
};
|
|
169
194
|
|
|
170
195
|
process.once('exit', this._exitHandler);
|
|
@@ -198,6 +223,7 @@ export class IpcTelemetryServer {
|
|
|
198
223
|
private _unlinkSocket(): void {
|
|
199
224
|
if (this._closed) return;
|
|
200
225
|
this._closed = true;
|
|
226
|
+
for (const socket of this.sockets) socket.destroy();
|
|
201
227
|
try {
|
|
202
228
|
this.server?.close();
|
|
203
229
|
} catch {
|
|
@@ -218,6 +244,8 @@ export class IpcTelemetryServer {
|
|
|
218
244
|
* Gracefully close the IPC telemetry server and clean up handlers and sockets.
|
|
219
245
|
*/
|
|
220
246
|
async close(): Promise<void> {
|
|
247
|
+
this._unlinkSocket();
|
|
248
|
+
await this._starting?.catch(() => {});
|
|
221
249
|
if (this._exitHandler) {
|
|
222
250
|
process.removeListener('exit', this._exitHandler);
|
|
223
251
|
this._exitHandler = null;
|
|
@@ -231,8 +259,6 @@ export class IpcTelemetryServer {
|
|
|
231
259
|
this._sigtermHandler = null;
|
|
232
260
|
}
|
|
233
261
|
|
|
234
|
-
this._unlinkSocket();
|
|
235
|
-
|
|
236
262
|
return new Promise<void>((resolve) => {
|
|
237
263
|
if (!this.server || !this.server.listening) {
|
|
238
264
|
resolve();
|
package/src/nestjs/decorators.ts
CHANGED
|
@@ -81,7 +81,7 @@ export function Cacheable(options: CacheableOptions = {}): MethodDecorator {
|
|
|
81
81
|
const rawTtl = options.ttl;
|
|
82
82
|
if (typeof rawTtl === 'number') {
|
|
83
83
|
if (options.ttlUnit === 'milliseconds' || (rawTtl >= 1000 && rawTtl % 1000 === 0 && options.ttlUnit !== 'seconds')) {
|
|
84
|
-
ttlSeconds =
|
|
84
|
+
ttlSeconds = rawTtl / 1000;
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
|
package/src/nestjs/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { TriCacheModule } from './tricache.module.js';
|
|
|
2
2
|
|
|
3
3
|
export { TriCacheModule };
|
|
4
4
|
export { TriCacheStore } from './tricache.store.js';
|
|
5
|
+
export { TriCacheKeyvAdapter } from './keyv-adapter.js';
|
|
5
6
|
export {
|
|
6
7
|
Cacheable,
|
|
7
8
|
CacheEvict,
|
|
@@ -18,6 +19,7 @@ export {
|
|
|
18
19
|
type TriCacheAsyncOptions,
|
|
19
20
|
type TriCacheOptionsFactory,
|
|
20
21
|
type NestCacheStore,
|
|
22
|
+
type TriCacheModuleOptions,
|
|
21
23
|
} from './types.js';
|
|
22
24
|
|
|
23
25
|
export default TriCacheModule;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import type { CacheService } from '../cache-service.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Relative-millisecond storage adapter for Keyv 5 / cache-manager 7.
|
|
6
|
+
* Wrap it in Keyv before passing it to Nest's CacheModule. Unlike TriCacheStore,
|
|
7
|
+
* this is a storage adapter, not the injected cache-manager facade.
|
|
8
|
+
* The supplied engine remains caller-owned when Keyv disconnects.
|
|
9
|
+
*/
|
|
10
|
+
export class TriCacheKeyvAdapter extends EventEmitter {
|
|
11
|
+
private _namespace?: string;
|
|
12
|
+
|
|
13
|
+
constructor(public readonly cache: CacheService) { super(); }
|
|
14
|
+
|
|
15
|
+
get namespace(): string | undefined { return this._namespace; }
|
|
16
|
+
set namespace(value: string | undefined) {
|
|
17
|
+
if (value && ['*', '?', '[', ']', '\\'].some(character => value.includes(character))) {
|
|
18
|
+
throw new Error('Keyv namespace must not contain glob characters');
|
|
19
|
+
}
|
|
20
|
+
this._namespace = value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async get<T = unknown>(key: string): Promise<T | undefined> {
|
|
24
|
+
const value = await this.cache.peek<T>(key);
|
|
25
|
+
return value === null ? undefined : value;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async set(key: string, value: unknown, ttl?: number): Promise<boolean> {
|
|
29
|
+
if (ttl !== undefined && (!Number.isFinite(ttl) || ttl < 0)) throw new Error('Keyv TTL must be finite and non-negative');
|
|
30
|
+
const ttlSeconds = ttl === undefined ? 0 : ttl / 1000;
|
|
31
|
+
await this.cache.set(key, value, ttlSeconds);
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async delete(key: string): Promise<boolean> {
|
|
36
|
+
const found = await this.has(key);
|
|
37
|
+
await this.cache.delete(key);
|
|
38
|
+
return found;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async clear(): Promise<void> {
|
|
42
|
+
await this.cache.clear(this._namespace ? `${this._namespace}:` : undefined);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async has(key: string): Promise<boolean> { return await this.cache.peek(key) !== null; }
|
|
46
|
+
|
|
47
|
+
async getMany<T = unknown>(keys: string[]): Promise<(T | undefined)[]> {
|
|
48
|
+
return this.cache.mget<T>(keys);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async deleteMany(keys: string[]): Promise<boolean> {
|
|
52
|
+
const found = await this.cache.mget(keys);
|
|
53
|
+
await this.cache.mdel(keys);
|
|
54
|
+
return found.every(value => value !== undefined && value !== null);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async setMany(entries: Array<{ key: string; value: unknown; ttl?: number }>): Promise<boolean[]> {
|
|
58
|
+
const results = await Promise.all(entries.map(entry => this.set(entry.key, entry.value, entry.ttl)));
|
|
59
|
+
return results;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async hasMany(keys: string[]): Promise<boolean[]> {
|
|
63
|
+
const values = await this.cache.mget(keys);
|
|
64
|
+
return values.map(value => value !== null && value !== undefined);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async disconnect(): Promise<void> { /* The caller owns and closes the shared engine. */ }
|
|
68
|
+
}
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
type Provider,
|
|
8
8
|
type TriCacheAsyncOptions,
|
|
9
9
|
type TriCacheOptionsFactory,
|
|
10
|
+
type TriCacheModuleOptions,
|
|
10
11
|
} from './types.js';
|
|
11
12
|
|
|
12
13
|
/**
|
|
@@ -23,18 +24,18 @@ export class TriCacheModule {
|
|
|
23
24
|
/**
|
|
24
25
|
* Register TriCache module synchronously with static configuration options.
|
|
25
26
|
*/
|
|
26
|
-
static register(options: CacheOptions = {}): DynamicModule {
|
|
27
|
+
static register(options: CacheOptions = {}, moduleOptions: TriCacheModuleOptions = {}): DynamicModule {
|
|
27
28
|
const cache = CacheService.create(options);
|
|
28
|
-
const
|
|
29
|
+
const provideCacheManager = moduleOptions.provideCacheManager ?? true;
|
|
29
30
|
|
|
30
31
|
return {
|
|
31
32
|
module: TriCacheModule,
|
|
32
33
|
global: true,
|
|
33
34
|
providers: [
|
|
34
35
|
{ provide: TRICACHE_SERVICE, useValue: cache },
|
|
35
|
-
{ provide: CACHE_MANAGER, useValue:
|
|
36
|
+
...(provideCacheManager ? [{ provide: CACHE_MANAGER, useValue: new TriCacheStore(cache) }] : []),
|
|
36
37
|
],
|
|
37
|
-
exports: [TRICACHE_SERVICE, CACHE_MANAGER],
|
|
38
|
+
exports: [TRICACHE_SERVICE, ...(provideCacheManager ? [CACHE_MANAGER] : [])],
|
|
38
39
|
};
|
|
39
40
|
}
|
|
40
41
|
|
|
@@ -43,6 +44,7 @@ export class TriCacheModule {
|
|
|
43
44
|
*/
|
|
44
45
|
static registerAsync(asyncOptions: TriCacheAsyncOptions): DynamicModule {
|
|
45
46
|
const asyncProviders = this.createAsyncProviders(asyncOptions);
|
|
47
|
+
const provideCacheManager = asyncOptions.provideCacheManager ?? true;
|
|
46
48
|
|
|
47
49
|
return {
|
|
48
50
|
module: TriCacheModule,
|
|
@@ -50,14 +52,14 @@ export class TriCacheModule {
|
|
|
50
52
|
imports: asyncOptions.imports || [],
|
|
51
53
|
providers: [
|
|
52
54
|
...asyncProviders,
|
|
53
|
-
{
|
|
55
|
+
...(provideCacheManager ? [{
|
|
54
56
|
provide: CACHE_MANAGER,
|
|
55
|
-
useFactory: (cache:
|
|
57
|
+
useFactory: (cache: CacheService) => new TriCacheStore(cache),
|
|
56
58
|
inject: [TRICACHE_SERVICE],
|
|
57
|
-
},
|
|
59
|
+
}] : []),
|
|
58
60
|
...(asyncOptions.extraProviders || []),
|
|
59
61
|
],
|
|
60
|
-
exports: [TRICACHE_SERVICE, CACHE_MANAGER],
|
|
62
|
+
exports: [TRICACHE_SERVICE, ...(provideCacheManager ? [CACHE_MANAGER] : [])],
|
|
61
63
|
};
|
|
62
64
|
}
|
|
63
65
|
|
|
@@ -83,7 +85,7 @@ export class TriCacheModule {
|
|
|
83
85
|
if (options.useFactory) {
|
|
84
86
|
return {
|
|
85
87
|
provide: TRICACHE_SERVICE,
|
|
86
|
-
useFactory: async (...args:
|
|
88
|
+
useFactory: async (...args: unknown[]) => {
|
|
87
89
|
const cacheOptions = await options.useFactory!(...args);
|
|
88
90
|
return CacheService.create(cacheOptions);
|
|
89
91
|
},
|
|
@@ -94,8 +96,8 @@ export class TriCacheModule {
|
|
|
94
96
|
const injectToken = options.useExisting || options.useClass!;
|
|
95
97
|
return {
|
|
96
98
|
provide: TRICACHE_SERVICE,
|
|
97
|
-
useFactory: async (optionsFactory:
|
|
98
|
-
const cacheOptions = await
|
|
99
|
+
useFactory: async (optionsFactory: TriCacheOptionsFactory) => {
|
|
100
|
+
const cacheOptions = await optionsFactory.createTriCacheOptions();
|
|
99
101
|
return CacheService.create(cacheOptions);
|
|
100
102
|
},
|
|
101
103
|
inject: [injectToken],
|
|
@@ -1,23 +1,31 @@
|
|
|
1
1
|
import type { CacheService } from '../index.js';
|
|
2
2
|
import type { NestCacheStore } from './types.js';
|
|
3
|
+
import { EventEmitter } from 'node:events';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
|
-
* TriCacheStore —
|
|
6
|
+
* TriCacheStore — Nest CACHE_MANAGER facade for CacheInterceptor and basic cache calls.
|
|
6
7
|
*
|
|
7
|
-
* Implements millisecond-based TTL semantics
|
|
8
|
-
* while dispatching to TriCache's three-tier engine with L1 memory, L1.5
|
|
8
|
+
* Implements millisecond-based TTL semantics for cache-manager v7's common API
|
|
9
|
+
* while dispatching to TriCache's three-tier engine with L1 memory, L1.5 disk spill,
|
|
9
10
|
* and L2 Redis/Valkey tiers.
|
|
10
11
|
*/
|
|
11
|
-
export class TriCacheStore implements NestCacheStore {
|
|
12
|
-
constructor(public readonly cache: CacheService) {}
|
|
12
|
+
export class TriCacheStore extends EventEmitter implements NestCacheStore {
|
|
13
|
+
constructor(public readonly cache: CacheService) { super(); }
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
16
|
* Retrieve an item from the cache.
|
|
16
|
-
* Returns undefined on miss (matching cache-manager
|
|
17
|
+
* Returns undefined on miss (matching Keyv/cache-manager contract).
|
|
17
18
|
*/
|
|
18
|
-
async get<T>(key: string): Promise<T | undefined
|
|
19
|
+
async get<T>(key: string): Promise<T | undefined>;
|
|
20
|
+
async get<T>(key: string, options: { raw: true }): Promise<{ value: T; expires?: number } | undefined>;
|
|
21
|
+
async get<T>(key: string, options?: { raw?: boolean }): Promise<T | { value: T; expires?: number } | undefined> {
|
|
19
22
|
const val = await this.cache.peek<T>(key);
|
|
20
|
-
|
|
23
|
+
if (val === null) return undefined;
|
|
24
|
+
if (options?.raw) {
|
|
25
|
+
const remaining = this.cache.ttl(key);
|
|
26
|
+
return { value: val, expires: remaining === null ? undefined : Date.now() + remaining * 1000 };
|
|
27
|
+
}
|
|
28
|
+
return val;
|
|
21
29
|
}
|
|
22
30
|
|
|
23
31
|
/**
|
|
@@ -26,54 +34,66 @@ export class TriCacheStore implements NestCacheStore {
|
|
|
26
34
|
* @param value - Cache value
|
|
27
35
|
* @param ttl - TTL in milliseconds (0 = indefinite / default engine TTL)
|
|
28
36
|
*/
|
|
29
|
-
async set<T>(key: string, value: T, ttl?: number): Promise<
|
|
30
|
-
const ttlSeconds = typeof ttl === 'number' && ttl > 0 ?
|
|
37
|
+
async set<T>(key: string, value: T, ttl?: number): Promise<T> {
|
|
38
|
+
const ttlSeconds = typeof ttl === 'number' && ttl > 0 ? ttl / 1000 : (ttl === 0 ? 0 : undefined);
|
|
31
39
|
await this.cache.set(key, value, ttlSeconds);
|
|
40
|
+
return value;
|
|
32
41
|
}
|
|
33
42
|
|
|
34
43
|
/**
|
|
35
44
|
* Remove an item from the cache.
|
|
36
45
|
*/
|
|
37
|
-
async del(key: string): Promise<
|
|
46
|
+
async del(key: string): Promise<boolean> {
|
|
38
47
|
await this.cache.delete(key);
|
|
48
|
+
return true;
|
|
39
49
|
}
|
|
40
50
|
|
|
41
51
|
/**
|
|
42
52
|
* Clear the entire cache namespace.
|
|
43
53
|
*/
|
|
44
|
-
async reset(): Promise<
|
|
54
|
+
async reset(): Promise<boolean> {
|
|
45
55
|
await this.cache.clear();
|
|
56
|
+
return true;
|
|
46
57
|
}
|
|
47
58
|
|
|
48
59
|
/**
|
|
49
|
-
* Batch get multiple items from the cache.
|
|
60
|
+
* Batch get multiple items from the cache. Accepts cache-manager 7's key array
|
|
61
|
+
* and the legacy cache-store variadic form.
|
|
50
62
|
*/
|
|
51
|
-
async mget<T>(
|
|
63
|
+
async mget<T>(keys: string[]): Promise<(T | undefined)[]>;
|
|
64
|
+
async mget<T>(...keys: string[]): Promise<(T | undefined)[]>;
|
|
65
|
+
async mget<T>(keysOrFirst: string[] | string = [], ...rest: string[]): Promise<(T | undefined)[]> {
|
|
66
|
+
const keys = Array.isArray(keysOrFirst) ? keysOrFirst : [keysOrFirst, ...rest];
|
|
52
67
|
if (keys.length === 0) return [];
|
|
53
|
-
return this.cache.mget<T>(keys
|
|
68
|
+
return this.cache.mget<T>(keys);
|
|
54
69
|
}
|
|
55
70
|
|
|
56
71
|
/**
|
|
57
72
|
* Batch set multiple entries with per-entry millisecond TTL.
|
|
58
73
|
*/
|
|
59
|
-
async mset(entries: Array<{ key: string; value: unknown; ttl?: number }>): Promise<
|
|
60
|
-
if (entries.length === 0) return;
|
|
74
|
+
async mset(entries: Array<{ key: string; value: unknown; ttl?: number }>): Promise<Array<{ key: string; value: unknown; ttl?: number }>> {
|
|
75
|
+
if (entries.length === 0) return [];
|
|
61
76
|
const batch: Record<string, { value: unknown; ttl?: number }> = {};
|
|
62
77
|
for (const e of entries) {
|
|
63
78
|
batch[e.key] = {
|
|
64
79
|
value: e.value,
|
|
65
|
-
ttl: typeof e.ttl === 'number' && e.ttl > 0 ?
|
|
80
|
+
ttl: typeof e.ttl === 'number' && e.ttl > 0 ? e.ttl / 1000 : (e.ttl === 0 ? 0 : undefined),
|
|
66
81
|
};
|
|
67
82
|
}
|
|
68
83
|
await this.cache.mset(batch);
|
|
84
|
+
return entries;
|
|
69
85
|
}
|
|
70
86
|
|
|
71
87
|
/**
|
|
72
88
|
* Batch delete multiple keys.
|
|
73
89
|
*/
|
|
74
|
-
async mdel(
|
|
75
|
-
|
|
90
|
+
async mdel(keys: string[]): Promise<boolean>;
|
|
91
|
+
async mdel(...keys: string[]): Promise<boolean>;
|
|
92
|
+
async mdel(keysOrFirst: string[] | string = [], ...rest: string[]): Promise<boolean> {
|
|
93
|
+
const keys = Array.isArray(keysOrFirst) ? keysOrFirst : [keysOrFirst, ...rest];
|
|
94
|
+
if (keys.length === 0) return true;
|
|
76
95
|
await this.cache.mdel(keys);
|
|
96
|
+
return true;
|
|
77
97
|
}
|
|
78
98
|
|
|
79
99
|
/**
|
|
@@ -94,4 +114,15 @@ export class TriCacheStore implements NestCacheStore {
|
|
|
94
114
|
const rem = this.cache.ttl(key);
|
|
95
115
|
return rem !== null ? rem * 1000 : undefined;
|
|
96
116
|
}
|
|
117
|
+
|
|
118
|
+
async wrap<T>(key: string, fetch: () => T | Promise<T>, ttl?: number, refreshThreshold?: number): Promise<T> {
|
|
119
|
+
const seconds = ttl === undefined ? 0 : ttl / 1000;
|
|
120
|
+
const refreshAhead = ttl && refreshThreshold ? Math.max(0, Math.min(1, (ttl - refreshThreshold) / ttl)) : undefined;
|
|
121
|
+
return this.cache.wrap(key, async () => await fetch(), { ttl: seconds, refreshAhead });
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
cacheId(): string { return `tricache:${this.cache.options.namespace ?? 'default'}`; }
|
|
125
|
+
get stores(): unknown[] { return [this]; }
|
|
126
|
+
async clear(): Promise<boolean> { await this.reset(); return true; }
|
|
127
|
+
async disconnect(): Promise<undefined> { return undefined; }
|
|
97
128
|
}
|
package/src/nestjs/types.ts
CHANGED
|
@@ -3,10 +3,10 @@ import type { CacheOptions } from '../index.js';
|
|
|
3
3
|
export const TRICACHE_SERVICE = 'TRICACHE_SERVICE';
|
|
4
4
|
export const CACHE_MANAGER = 'CACHE_MANAGER';
|
|
5
5
|
|
|
6
|
-
export type InjectionToken = string | symbol | Function | Type<
|
|
6
|
+
export type InjectionToken = string | symbol | Function | Type<any>;
|
|
7
7
|
|
|
8
8
|
export interface Type<T = unknown> extends Function {
|
|
9
|
-
new (...args:
|
|
9
|
+
new (...args: any[]): T;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
export interface ForwardReference<T = unknown> {
|
|
@@ -25,6 +25,8 @@ export interface ValueProvider<T = unknown> {
|
|
|
25
25
|
export interface ClassProvider<T = unknown> {
|
|
26
26
|
provide: InjectionToken;
|
|
27
27
|
useClass: Type<T>;
|
|
28
|
+
scope?: 0 | 1 | 2;
|
|
29
|
+
durable?: boolean;
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
export interface ExistingProvider {
|
|
@@ -35,7 +37,9 @@ export interface ExistingProvider {
|
|
|
35
37
|
export interface FactoryProvider<T = unknown> {
|
|
36
38
|
provide: InjectionToken;
|
|
37
39
|
useFactory: (...args: any[]) => T | Promise<T>;
|
|
38
|
-
inject?: Array<InjectionToken |
|
|
40
|
+
inject?: Array<InjectionToken | { token: InjectionToken; optional: boolean }>;
|
|
41
|
+
scope?: 0 | 1 | 2;
|
|
42
|
+
durable?: boolean;
|
|
39
43
|
}
|
|
40
44
|
|
|
41
45
|
export type Provider<T = unknown> =
|
|
@@ -46,10 +50,11 @@ export type Provider<T = unknown> =
|
|
|
46
50
|
| FactoryProvider<T>;
|
|
47
51
|
|
|
48
52
|
export interface DynamicModule {
|
|
49
|
-
module: Type<
|
|
53
|
+
module: Type<any>;
|
|
50
54
|
providers?: Provider[];
|
|
51
|
-
exports?: Array<DynamicModule |
|
|
52
|
-
imports?: Array<Type<
|
|
55
|
+
exports?: Array<DynamicModule | string | symbol | Provider | ForwardReference | Abstract<any> | Function>;
|
|
56
|
+
imports?: Array<Type<any> | DynamicModule | ForwardReference>;
|
|
57
|
+
controllers?: Type<any>[];
|
|
53
58
|
global?: boolean;
|
|
54
59
|
}
|
|
55
60
|
|
|
@@ -58,22 +63,31 @@ export interface TriCacheOptionsFactory {
|
|
|
58
63
|
}
|
|
59
64
|
|
|
60
65
|
export interface TriCacheAsyncOptions {
|
|
61
|
-
imports?: Array<Type<
|
|
62
|
-
useExisting?:
|
|
66
|
+
imports?: Array<Type<any> | DynamicModule | ForwardReference>;
|
|
67
|
+
useExisting?: InjectionToken;
|
|
63
68
|
useClass?: Type<TriCacheOptionsFactory>;
|
|
64
|
-
useFactory?: (...args:
|
|
65
|
-
inject?: Array<InjectionToken |
|
|
69
|
+
useFactory?: (...args: any[]) => Promise<CacheOptions> | CacheOptions;
|
|
70
|
+
inject?: Array<InjectionToken | { token: InjectionToken; optional: boolean }>;
|
|
66
71
|
extraProviders?: Provider[];
|
|
72
|
+
/** Whether to bind CACHE_MANAGER to TriCacheStore. Default: true. */
|
|
73
|
+
provideCacheManager?: boolean;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface TriCacheModuleOptions {
|
|
77
|
+
/** Set false when importing @nestjs/cache-manager CacheModule in the same application. Default: true. */
|
|
78
|
+
provideCacheManager?: boolean;
|
|
67
79
|
}
|
|
68
80
|
|
|
69
81
|
export interface NestCacheStore {
|
|
70
82
|
get<T>(key: string): Promise<T | undefined>;
|
|
71
|
-
set<T>(key: string, value: T, ttl?: number): Promise<
|
|
72
|
-
del(key: string): Promise<
|
|
73
|
-
reset(): Promise<
|
|
83
|
+
set<T>(key: string, value: T, ttl?: number): Promise<T>;
|
|
84
|
+
del(key: string): Promise<boolean>;
|
|
85
|
+
reset(): Promise<boolean>;
|
|
86
|
+
mget<T>(keys: string[]): Promise<(T | undefined)[]>;
|
|
74
87
|
mget<T>(...keys: string[]): Promise<(T | undefined)[]>;
|
|
75
|
-
mset(entries: Array<{ key: string; value: unknown; ttl?: number }>): Promise<
|
|
76
|
-
mdel(
|
|
88
|
+
mset(entries: Array<{ key: string; value: unknown; ttl?: number }>): Promise<Array<{ key: string; value: unknown; ttl?: number }>>;
|
|
89
|
+
mdel(keys: string[]): Promise<boolean>;
|
|
90
|
+
mdel(...keys: string[]): Promise<boolean>;
|
|
77
91
|
keys(pattern?: string): Promise<string[]>;
|
|
78
92
|
ttl(key: string): Promise<number | undefined>;
|
|
79
93
|
}
|