tricache 0.6.7 → 0.7.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.
Files changed (55) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1428 -970
  3. package/bin/tricache.js +8 -0
  4. package/dist/cache-service-5GATUYAK.js +7 -0
  5. package/dist/cache-service-D8tIXmzb.d.ts +1498 -0
  6. package/dist/chunk-EUGYR7KR.js +4629 -0
  7. package/dist/chunk-UBSR5UYA.js +0 -0
  8. package/dist/{chunk-FKY5R3CE.js → chunk-X5AXHPVD.js} +139 -6
  9. package/dist/cli.cjs +5238 -0
  10. package/dist/cli.d.ts +11 -0
  11. package/dist/cli.js +130 -0
  12. package/dist/drizzle/index.cjs +5218 -0
  13. package/dist/drizzle/index.d.ts +44 -0
  14. package/dist/drizzle/index.js +46 -0
  15. package/dist/http/index.cjs +5374 -0
  16. package/dist/http/index.d.ts +33 -0
  17. package/dist/http/index.js +200 -0
  18. package/dist/index.cjs +1245 -209
  19. package/dist/index.d.ts +15 -1230
  20. package/dist/index.js +11 -3705
  21. package/dist/nestjs/index.cjs +5362 -0
  22. package/dist/nestjs/index.d.ts +210 -0
  23. package/dist/nestjs/index.js +250 -0
  24. package/dist/next/index.cjs +5407 -0
  25. package/dist/next/index.d.ts +208 -0
  26. package/dist/next/index.js +297 -0
  27. package/dist/prisma/index.cjs +123 -0
  28. package/dist/prisma/index.d.ts +60 -0
  29. package/dist/prisma/index.js +87 -0
  30. package/dist/serialize-worker.cjs +190 -15
  31. package/dist/serialize-worker.js +63 -12
  32. package/package.json +71 -21
  33. package/src/cache-service.ts +3294 -2429
  34. package/src/cli.ts +143 -0
  35. package/src/compression.ts +56 -0
  36. package/src/disk-tier.ts +821 -757
  37. package/src/drizzle/index.ts +86 -0
  38. package/src/encryption.ts +513 -442
  39. package/src/http/index.ts +265 -0
  40. package/src/index.ts +7 -0
  41. package/src/nestjs/decorators.ts +161 -0
  42. package/src/nestjs/index.ts +23 -0
  43. package/src/nestjs/tricache.module.ts +104 -0
  44. package/src/nestjs/tricache.store.ts +97 -0
  45. package/src/nestjs/types.ts +79 -0
  46. package/src/next/cache-handler.ts +268 -0
  47. package/src/next/index.ts +37 -0
  48. package/src/next/isr-handler.ts +105 -0
  49. package/src/next/types.ts +130 -0
  50. package/src/prisma/index.ts +154 -0
  51. package/src/serialize-worker.ts +77 -13
  52. package/src/smart-memory-cache.ts +1026 -1010
  53. package/src/types.ts +911 -732
  54. package/src/wasm/bloom-filter-wasm.ts +3 -2
  55. package/src/worker-pool.ts +156 -42
@@ -0,0 +1,210 @@
1
+ import { f as CacheOptions, C as CacheService, c as CachePriority } from '../cache-service-D8tIXmzb.js';
2
+
3
+ declare const TRICACHE_SERVICE = "TRICACHE_SERVICE";
4
+ declare const CACHE_MANAGER = "CACHE_MANAGER";
5
+ type InjectionToken = string | symbol | Function | Type<unknown>;
6
+ interface Type<T = unknown> extends Function {
7
+ new (...args: unknown[]): T;
8
+ }
9
+ interface ForwardReference<T = unknown> {
10
+ forwardRef: () => T;
11
+ }
12
+ interface ValueProvider<T = unknown> {
13
+ provide: InjectionToken;
14
+ useValue: T;
15
+ }
16
+ interface ClassProvider<T = unknown> {
17
+ provide: InjectionToken;
18
+ useClass: Type<T>;
19
+ }
20
+ interface ExistingProvider {
21
+ provide: InjectionToken;
22
+ useExisting: InjectionToken;
23
+ }
24
+ interface FactoryProvider<T = unknown> {
25
+ provide: InjectionToken;
26
+ useFactory: (...args: any[]) => T | Promise<T>;
27
+ inject?: Array<InjectionToken | any>;
28
+ }
29
+ type Provider<T = unknown> = Type<unknown> | ValueProvider<T> | ClassProvider<T> | ExistingProvider | FactoryProvider<T>;
30
+ interface DynamicModule {
31
+ module: Type<unknown>;
32
+ providers?: Provider[];
33
+ exports?: Array<DynamicModule | Promise<DynamicModule> | string | symbol | Provider | Function | unknown>;
34
+ imports?: Array<Type<unknown> | DynamicModule | Promise<DynamicModule> | ForwardReference>;
35
+ global?: boolean;
36
+ }
37
+ interface TriCacheOptionsFactory {
38
+ createTriCacheOptions(): Promise<CacheOptions> | CacheOptions;
39
+ }
40
+ interface TriCacheAsyncOptions {
41
+ imports?: Array<Type<unknown> | DynamicModule | Promise<DynamicModule> | ForwardReference>;
42
+ useExisting?: Type<TriCacheOptionsFactory>;
43
+ useClass?: Type<TriCacheOptionsFactory>;
44
+ useFactory?: (...args: unknown[]) => Promise<CacheOptions> | CacheOptions;
45
+ inject?: Array<InjectionToken | unknown>;
46
+ extraProviders?: Provider[];
47
+ }
48
+ interface NestCacheStore {
49
+ get<T>(key: string): Promise<T | undefined>;
50
+ set<T>(key: string, value: T, ttl?: number): Promise<void>;
51
+ del(key: string): Promise<void>;
52
+ reset(): Promise<void>;
53
+ mget<T>(...keys: string[]): Promise<(T | undefined)[]>;
54
+ mset(entries: Array<{
55
+ key: string;
56
+ value: unknown;
57
+ ttl?: number;
58
+ }>): Promise<void>;
59
+ mdel(...keys: string[]): Promise<void>;
60
+ keys(pattern?: string): Promise<string[]>;
61
+ ttl(key: string): Promise<number | undefined>;
62
+ }
63
+
64
+ /**
65
+ * TriCacheModule — NestJS dynamic module providing TriCacheService and standard CacheStore.
66
+ *
67
+ * Can be registered synchronously via `TriCacheModule.register(options)`
68
+ * or asynchronously via `TriCacheModule.registerAsync({ useFactory: ... })`.
69
+ *
70
+ * Exports:
71
+ * - `TRICACHE_SERVICE` (`CacheService` instance)
72
+ * - `CACHE_MANAGER` (`TriCacheStore` adapter conforming to @nestjs/cache-manager)
73
+ */
74
+ declare class TriCacheModule {
75
+ /**
76
+ * Register TriCache module synchronously with static configuration options.
77
+ */
78
+ static register(options?: CacheOptions): DynamicModule;
79
+ /**
80
+ * Register TriCache module asynchronously with factory or class injection.
81
+ */
82
+ static registerAsync(asyncOptions: TriCacheAsyncOptions): DynamicModule;
83
+ private static createAsyncProviders;
84
+ private static createAsyncOptionsProvider;
85
+ }
86
+
87
+ /**
88
+ * TriCacheStore — drop-in CacheStore conforming to @nestjs/cache-manager and cache-manager v5/v6.
89
+ *
90
+ * Implements millisecond-based TTL semantics expected by cache-manager v5/v6
91
+ * while dispatching to TriCache's three-tier engine with L1 memory, L1.5 NVMe disk spill,
92
+ * and L2 Redis/Valkey tiers.
93
+ */
94
+ declare class TriCacheStore implements NestCacheStore {
95
+ readonly cache: CacheService;
96
+ constructor(cache: CacheService);
97
+ /**
98
+ * Retrieve an item from the cache.
99
+ * Returns undefined on miss (matching cache-manager v5/v6 contract).
100
+ */
101
+ get<T>(key: string): Promise<T | undefined>;
102
+ /**
103
+ * Set an item in the cache.
104
+ * @param key - Cache key
105
+ * @param value - Cache value
106
+ * @param ttl - TTL in milliseconds (0 = indefinite / default engine TTL)
107
+ */
108
+ set<T>(key: string, value: T, ttl?: number): Promise<void>;
109
+ /**
110
+ * Remove an item from the cache.
111
+ */
112
+ del(key: string): Promise<void>;
113
+ /**
114
+ * Clear the entire cache namespace.
115
+ */
116
+ reset(): Promise<void>;
117
+ /**
118
+ * Batch get multiple items from the cache.
119
+ */
120
+ mget<T>(...keys: string[]): Promise<(T | undefined)[]>;
121
+ /**
122
+ * Batch set multiple entries with per-entry millisecond TTL.
123
+ */
124
+ mset(entries: Array<{
125
+ key: string;
126
+ value: unknown;
127
+ ttl?: number;
128
+ }>): Promise<void>;
129
+ /**
130
+ * Batch delete multiple keys.
131
+ */
132
+ mdel(...keys: string[]): Promise<void>;
133
+ /**
134
+ * Scan keys currently resident in the cache.
135
+ */
136
+ keys(pattern?: string): Promise<string[]>;
137
+ /**
138
+ * Get remaining TTL for a key in milliseconds.
139
+ */
140
+ ttl(key: string): Promise<number | undefined>;
141
+ }
142
+
143
+ interface CacheableOptions {
144
+ /**
145
+ * Static key string or dynamic key generator function.
146
+ * Example: (id: string) => `user:${id}`
147
+ */
148
+ key?: string | ((...args: any[]) => string);
149
+ /** TTL in seconds (or milliseconds if specified / detected). Default: 300 s. */
150
+ ttl?: number;
151
+ /** Explicit time unit for the ttl option. Default: 'seconds'. */
152
+ ttlUnit?: 'seconds' | 'milliseconds';
153
+ /** Stale-While-Revalidate grace period in seconds. */
154
+ swr?: number;
155
+ /** Semantic tags for invalidation. */
156
+ tags?: string[] | ((...args: any[]) => string[]);
157
+ /** Dependency cascade patterns. */
158
+ dependsOn?: string[] | ((...args: any[]) => string[]);
159
+ /** Cache eviction priority. */
160
+ priority?: CachePriority;
161
+ /** 0–1 fraction of TTL elapsed for background recompute. */
162
+ refreshAhead?: number;
163
+ /** XFetch probabilistic early expiration factor (0.5–2.0). */
164
+ xfetchBeta?: number;
165
+ /** Condition predicate to determine whether caching should occur. */
166
+ condition?: (...args: any[]) => boolean;
167
+ }
168
+ interface CacheEvictOptions {
169
+ /** Key or pattern to delete. */
170
+ key?: string | ((...args: any[]) => string);
171
+ /** Tags to invalidate. */
172
+ tags?: string[] | ((...args: any[]) => string[]);
173
+ /** Invalidate before method execution (default: false = after successful execution). */
174
+ beforeInvocation?: boolean;
175
+ }
176
+ /**
177
+ * Declarative method decorator that caches the return value of an async method.
178
+ *
179
+ * Automatically intercepts calls, checks TriCache (L1 -> L1.5 -> L2), and falls
180
+ * back to executing the decorated method on cache miss with thundering-herd protection.
181
+ *
182
+ * @example
183
+ * ```typescript
184
+ * @Injectable()
185
+ * export class UserService {
186
+ * @Cacheable({ key: (id) => `user:${id}`, ttl: 300, tags: ['users'] })
187
+ * async findById(id: string) {
188
+ * return this.db.find(id);
189
+ * }
190
+ * }
191
+ * ```
192
+ */
193
+ declare function Cacheable(options?: CacheableOptions): MethodDecorator;
194
+ /**
195
+ * Declarative method decorator that invalidates cache keys or tags upon method execution.
196
+ *
197
+ * @example
198
+ * ```typescript
199
+ * @Injectable()
200
+ * export class UserService {
201
+ * @CacheEvict({ tags: ['users'] })
202
+ * async update(id: string, dto: UpdateUserDto) {
203
+ * return this.db.update(id, dto);
204
+ * }
205
+ * }
206
+ * ```
207
+ */
208
+ declare function CacheEvict(options?: CacheEvictOptions): MethodDecorator;
209
+
210
+ export { CACHE_MANAGER, CacheEvict, type CacheEvictOptions, Cacheable, type CacheableOptions, type DynamicModule, type InjectionToken, type NestCacheStore, type Provider, TRICACHE_SERVICE, type TriCacheAsyncOptions, TriCacheModule, type TriCacheOptionsFactory, TriCacheStore, type Type, TriCacheModule as default };
@@ -0,0 +1,250 @@
1
+ import "../chunk-UBSR5UYA.js";
2
+ import {
3
+ CacheService
4
+ } from "../chunk-EUGYR7KR.js";
5
+ import "../chunk-X5AXHPVD.js";
6
+
7
+ // src/nestjs/tricache.store.ts
8
+ var TriCacheStore = class {
9
+ constructor(cache) {
10
+ this.cache = cache;
11
+ }
12
+ cache;
13
+ /**
14
+ * Retrieve an item from the cache.
15
+ * Returns undefined on miss (matching cache-manager v5/v6 contract).
16
+ */
17
+ async get(key) {
18
+ const val = await this.cache.peek(key);
19
+ return val !== null ? val : void 0;
20
+ }
21
+ /**
22
+ * Set an item in the cache.
23
+ * @param key - Cache key
24
+ * @param value - Cache value
25
+ * @param ttl - TTL in milliseconds (0 = indefinite / default engine TTL)
26
+ */
27
+ async set(key, value, ttl) {
28
+ const ttlSeconds = typeof ttl === "number" && ttl > 0 ? Math.round(ttl / 1e3) : ttl === 0 ? 0 : void 0;
29
+ await this.cache.set(key, value, ttlSeconds);
30
+ }
31
+ /**
32
+ * Remove an item from the cache.
33
+ */
34
+ async del(key) {
35
+ await this.cache.delete(key);
36
+ }
37
+ /**
38
+ * Clear the entire cache namespace.
39
+ */
40
+ async reset() {
41
+ await this.cache.clear();
42
+ }
43
+ /**
44
+ * Batch get multiple items from the cache.
45
+ */
46
+ async mget(...keys) {
47
+ if (keys.length === 0) return [];
48
+ return this.cache.mget(keys, async () => ({}));
49
+ }
50
+ /**
51
+ * Batch set multiple entries with per-entry millisecond TTL.
52
+ */
53
+ async mset(entries) {
54
+ if (entries.length === 0) return;
55
+ const batch = {};
56
+ for (const e of entries) {
57
+ batch[e.key] = {
58
+ value: e.value,
59
+ ttl: typeof e.ttl === "number" && e.ttl > 0 ? Math.round(e.ttl / 1e3) : e.ttl === 0 ? 0 : void 0
60
+ };
61
+ }
62
+ await this.cache.mset(batch);
63
+ }
64
+ /**
65
+ * Batch delete multiple keys.
66
+ */
67
+ async mdel(...keys) {
68
+ if (keys.length === 0) return;
69
+ await this.cache.mdel(keys);
70
+ }
71
+ /**
72
+ * Scan keys currently resident in the cache.
73
+ */
74
+ async keys(pattern) {
75
+ const all = [];
76
+ for (const k of this.cache.keys()) {
77
+ if (!pattern || k.includes(pattern)) all.push(k);
78
+ }
79
+ return all;
80
+ }
81
+ /**
82
+ * Get remaining TTL for a key in milliseconds.
83
+ */
84
+ async ttl(key) {
85
+ const rem = this.cache.ttl(key);
86
+ return rem !== null ? rem * 1e3 : void 0;
87
+ }
88
+ };
89
+
90
+ // src/nestjs/types.ts
91
+ var TRICACHE_SERVICE = "TRICACHE_SERVICE";
92
+ var CACHE_MANAGER = "CACHE_MANAGER";
93
+
94
+ // src/nestjs/tricache.module.ts
95
+ var TriCacheModule = class _TriCacheModule {
96
+ /**
97
+ * Register TriCache module synchronously with static configuration options.
98
+ */
99
+ static register(options = {}) {
100
+ const cache = CacheService.create(options);
101
+ const store = new TriCacheStore(cache);
102
+ return {
103
+ module: _TriCacheModule,
104
+ global: true,
105
+ providers: [
106
+ { provide: TRICACHE_SERVICE, useValue: cache },
107
+ { provide: CACHE_MANAGER, useValue: store }
108
+ ],
109
+ exports: [TRICACHE_SERVICE, CACHE_MANAGER]
110
+ };
111
+ }
112
+ /**
113
+ * Register TriCache module asynchronously with factory or class injection.
114
+ */
115
+ static registerAsync(asyncOptions) {
116
+ const asyncProviders = this.createAsyncProviders(asyncOptions);
117
+ return {
118
+ module: _TriCacheModule,
119
+ global: true,
120
+ imports: asyncOptions.imports || [],
121
+ providers: [
122
+ ...asyncProviders,
123
+ {
124
+ provide: CACHE_MANAGER,
125
+ useFactory: (cache) => new TriCacheStore(cache),
126
+ inject: [TRICACHE_SERVICE]
127
+ },
128
+ ...asyncOptions.extraProviders || []
129
+ ],
130
+ exports: [TRICACHE_SERVICE, CACHE_MANAGER]
131
+ };
132
+ }
133
+ static createAsyncProviders(options) {
134
+ if (options.useExisting || options.useFactory) {
135
+ return [this.createAsyncOptionsProvider(options)];
136
+ }
137
+ if (options.useClass) {
138
+ return [
139
+ this.createAsyncOptionsProvider(options),
140
+ {
141
+ provide: options.useClass,
142
+ useClass: options.useClass
143
+ }
144
+ ];
145
+ }
146
+ return [];
147
+ }
148
+ static createAsyncOptionsProvider(options) {
149
+ if (options.useFactory) {
150
+ return {
151
+ provide: TRICACHE_SERVICE,
152
+ useFactory: async (...args) => {
153
+ const cacheOptions = await options.useFactory(...args);
154
+ return CacheService.create(cacheOptions);
155
+ },
156
+ inject: options.inject || []
157
+ };
158
+ }
159
+ const injectToken = options.useExisting || options.useClass;
160
+ return {
161
+ provide: TRICACHE_SERVICE,
162
+ useFactory: async (optionsFactory) => {
163
+ const cacheOptions = await optionsFactory.createTriCacheOptions();
164
+ return CacheService.create(cacheOptions);
165
+ },
166
+ inject: [injectToken]
167
+ };
168
+ }
169
+ };
170
+
171
+ // src/nestjs/decorators.ts
172
+ function resolveCacheInstance(ctx) {
173
+ return ctx?.cacheService || ctx?.cache || ctx?.cacheStore?.cache || CacheService.create();
174
+ }
175
+ function Cacheable(options = {}) {
176
+ return function(target, propertyKey, descriptor) {
177
+ const originalMethod = descriptor.value;
178
+ const methodName = String(propertyKey);
179
+ const className = target?.constructor?.name || "Service";
180
+ descriptor.value = async function(...args) {
181
+ if (options.condition && !options.condition(...args)) {
182
+ return originalMethod.apply(this, args);
183
+ }
184
+ const cache = resolveCacheInstance(this);
185
+ const key = typeof options.key === "function" ? options.key(...args) : typeof options.key === "string" ? options.key : `${className}:${methodName}:${JSON.stringify(args)}`;
186
+ let ttlSeconds = options.ttl ?? 300;
187
+ const rawTtl = options.ttl;
188
+ if (typeof rawTtl === "number") {
189
+ if (options.ttlUnit === "milliseconds" || rawTtl >= 1e3 && rawTtl % 1e3 === 0 && options.ttlUnit !== "seconds") {
190
+ ttlSeconds = Math.round(rawTtl / 1e3);
191
+ }
192
+ }
193
+ const tags = typeof options.tags === "function" ? options.tags(...args) : options.tags;
194
+ const dependsOn = typeof options.dependsOn === "function" ? options.dependsOn(...args) : options.dependsOn;
195
+ return cache.wrap(
196
+ key,
197
+ () => originalMethod.apply(this, args),
198
+ {
199
+ ttl: ttlSeconds,
200
+ swr: options.swr,
201
+ tags,
202
+ dependsOn,
203
+ priority: options.priority,
204
+ refreshAhead: options.refreshAhead,
205
+ xfetchBeta: options.xfetchBeta
206
+ }
207
+ );
208
+ };
209
+ return descriptor;
210
+ };
211
+ }
212
+ function CacheEvict(options = {}) {
213
+ return function(_target, _propertyKey, descriptor) {
214
+ const originalMethod = descriptor.value;
215
+ descriptor.value = async function(...args) {
216
+ const cache = resolveCacheInstance(this);
217
+ const performEviction = async () => {
218
+ if (options.key) {
219
+ const k = typeof options.key === "function" ? options.key(...args) : options.key;
220
+ await cache.delete(k);
221
+ }
222
+ if (options.tags) {
223
+ const t = typeof options.tags === "function" ? options.tags(...args) : options.tags;
224
+ await cache.invalidateTags(t);
225
+ }
226
+ };
227
+ if (options.beforeInvocation) {
228
+ await performEviction();
229
+ }
230
+ const result = await originalMethod.apply(this, args);
231
+ if (!options.beforeInvocation) {
232
+ await performEviction();
233
+ }
234
+ return result;
235
+ };
236
+ return descriptor;
237
+ };
238
+ }
239
+
240
+ // src/nestjs/index.ts
241
+ var nestjs_default = TriCacheModule;
242
+ export {
243
+ CACHE_MANAGER,
244
+ CacheEvict,
245
+ Cacheable,
246
+ TRICACHE_SERVICE,
247
+ TriCacheModule,
248
+ TriCacheStore,
249
+ nestjs_default as default
250
+ };