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.
- package/LICENSE +1 -1
- package/README.md +1428 -970
- package/bin/tricache.js +8 -0
- package/dist/cache-service-5GATUYAK.js +7 -0
- package/dist/cache-service-D8tIXmzb.d.ts +1498 -0
- package/dist/chunk-EUGYR7KR.js +4629 -0
- package/dist/chunk-UBSR5UYA.js +0 -0
- package/dist/{chunk-FKY5R3CE.js → chunk-X5AXHPVD.js} +139 -6
- package/dist/cli.cjs +5238 -0
- package/dist/cli.d.ts +11 -0
- package/dist/cli.js +130 -0
- package/dist/drizzle/index.cjs +5218 -0
- package/dist/drizzle/index.d.ts +44 -0
- package/dist/drizzle/index.js +46 -0
- package/dist/http/index.cjs +5374 -0
- package/dist/http/index.d.ts +33 -0
- package/dist/http/index.js +200 -0
- package/dist/index.cjs +1245 -209
- package/dist/index.d.ts +15 -1230
- package/dist/index.js +11 -3705
- package/dist/nestjs/index.cjs +5362 -0
- package/dist/nestjs/index.d.ts +210 -0
- package/dist/nestjs/index.js +250 -0
- package/dist/next/index.cjs +5407 -0
- package/dist/next/index.d.ts +208 -0
- package/dist/next/index.js +297 -0
- package/dist/prisma/index.cjs +123 -0
- package/dist/prisma/index.d.ts +60 -0
- package/dist/prisma/index.js +87 -0
- package/dist/serialize-worker.cjs +190 -15
- package/dist/serialize-worker.js +63 -12
- package/package.json +71 -21
- package/src/cache-service.ts +3294 -2429
- package/src/cli.ts +143 -0
- package/src/compression.ts +56 -0
- package/src/disk-tier.ts +821 -757
- package/src/drizzle/index.ts +86 -0
- package/src/encryption.ts +513 -442
- package/src/http/index.ts +265 -0
- package/src/index.ts +7 -0
- package/src/nestjs/decorators.ts +161 -0
- package/src/nestjs/index.ts +23 -0
- package/src/nestjs/tricache.module.ts +104 -0
- package/src/nestjs/tricache.store.ts +97 -0
- package/src/nestjs/types.ts +79 -0
- package/src/next/cache-handler.ts +268 -0
- package/src/next/index.ts +37 -0
- package/src/next/isr-handler.ts +105 -0
- package/src/next/types.ts +130 -0
- package/src/prisma/index.ts +154 -0
- package/src/serialize-worker.ts +77 -13
- package/src/smart-memory-cache.ts +1026 -1010
- package/src/types.ts +911 -732
- package/src/wasm/bloom-filter-wasm.ts +3 -2
- package/src/worker-pool.ts +156 -42
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tricache/drizzle — First-class Drizzle ORM query caching wrapper for TriCache.
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* import { withCache } from 'tricache/drizzle';
|
|
6
|
+
*
|
|
7
|
+
* const activeUsers = await withCache(
|
|
8
|
+
* db.select().from(users).where(eq(users.active, true)),
|
|
9
|
+
* { cache, ttl: 300, tags: ['users'] }
|
|
10
|
+
* );
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { CacheService } from '../cache-service.js';
|
|
14
|
+
import type { WrapOptions } from '../types.js';
|
|
15
|
+
import crypto from 'crypto';
|
|
16
|
+
|
|
17
|
+
export interface DrizzleCacheOptions extends WrapOptions {
|
|
18
|
+
/** TriCache instance to use. If omitted, uses process-level singleton `CacheService.create()`. */
|
|
19
|
+
cache?: CacheService;
|
|
20
|
+
/** Explicit cache key. If omitted, automatically derived from SHA-256 hash of `{ sql, params }`. */
|
|
21
|
+
key?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface DrizzleExecutableQuery<T> {
|
|
25
|
+
toSQL(): { sql: string; params: unknown[] };
|
|
26
|
+
execute?(): Promise<T>;
|
|
27
|
+
then?: (onfulfilled?: (value: T) => unknown, onrejected?: (reason: unknown) => unknown) => Promise<unknown>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Generate a deterministic SHA-256 hash key for a Drizzle query based on its compiled SQL and parameters.
|
|
32
|
+
*/
|
|
33
|
+
export function generateDrizzleCacheKey(sqlInfo: { sql: string; params: unknown[] }): string {
|
|
34
|
+
const normalized = JSON.stringify(sqlInfo);
|
|
35
|
+
const hash = crypto.createHash('sha256').update(normalized, 'utf8').digest('hex').slice(0, 32);
|
|
36
|
+
return `drizzle:${hash}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Wraps a Drizzle ORM query with TriCache caching.
|
|
41
|
+
*
|
|
42
|
+
* @param query Drizzle query builder or executable query instance.
|
|
43
|
+
* @param options Cache options including TTL, tags, SWR, priority, and optional key.
|
|
44
|
+
*/
|
|
45
|
+
export async function withCache<T>(
|
|
46
|
+
query: DrizzleExecutableQuery<T>,
|
|
47
|
+
options: DrizzleCacheOptions = {},
|
|
48
|
+
): Promise<T> {
|
|
49
|
+
const { cache, key, ttl = 300, swr, priority, tags, dependsOn, refreshAhead, xfetchBeta, notFoundTtl } = options;
|
|
50
|
+
|
|
51
|
+
let activeCache = cache;
|
|
52
|
+
if (!activeCache) {
|
|
53
|
+
const { CacheService } = await import('../cache-service.js');
|
|
54
|
+
activeCache = CacheService.create();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
let cacheKey = key;
|
|
58
|
+
if (!cacheKey) {
|
|
59
|
+
if (typeof query.toSQL === 'function') {
|
|
60
|
+
const sqlInfo = query.toSQL();
|
|
61
|
+
cacheKey = generateDrizzleCacheKey(sqlInfo);
|
|
62
|
+
} else {
|
|
63
|
+
throw new Error('tricache/drizzle: query must provide a .toSQL() method or an explicit key option.');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const executeFn = async (): Promise<T> => {
|
|
68
|
+
if (typeof query.execute === 'function') {
|
|
69
|
+
return await query.execute();
|
|
70
|
+
} else if (typeof query.then === 'function') {
|
|
71
|
+
return await (query as unknown as Promise<T>);
|
|
72
|
+
}
|
|
73
|
+
throw new Error('tricache/drizzle: query is neither executable via .execute() nor thenable.');
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
return activeCache.wrap(cacheKey, executeFn, {
|
|
77
|
+
ttl,
|
|
78
|
+
swr,
|
|
79
|
+
priority,
|
|
80
|
+
tags,
|
|
81
|
+
dependsOn,
|
|
82
|
+
refreshAhead,
|
|
83
|
+
xfetchBeta,
|
|
84
|
+
notFoundTtl,
|
|
85
|
+
});
|
|
86
|
+
}
|