tricache 1.0.1 → 2.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.
Files changed (127) hide show
  1. package/README.md +89 -93
  2. package/dashboards/tricache-alerts.yaml +80 -4
  3. package/dashboards/tricache-grafana.json +54 -1
  4. package/dashboards/tricache-inventory.rules.yml +21 -0
  5. package/dist/cache-service-BV5T8-20.d.cts +1064 -0
  6. package/dist/cache-service-UZXOQGEY.js +14 -0
  7. package/dist/{cache-service-CREOp7MM.d.ts → cache-service-tpyrXvlH.d.ts} +157 -24
  8. package/dist/{chunk-UBWLPKTI.js → chunk-EJXG7WO6.js} +152 -32
  9. package/dist/{chunk-IITRFWHY.js → chunk-EUKB7RKP.js} +21 -4
  10. package/dist/{chunk-7RV5ZVBB.js → chunk-JNSLMK4Q.js} +111 -29
  11. package/dist/{chunk-2AJKEQMZ.js → chunk-KV7KISK2.js} +54 -5
  12. package/dist/{chunk-JYQFQJNJ.js → chunk-MB3DMVWN.js} +3424 -1022
  13. package/dist/chunk-RBJRTGFK.js +94 -0
  14. package/dist/chunk-S5N65I45.js +17 -0
  15. package/dist/cli.cjs +3694 -1070
  16. package/dist/cli.d.cts +12 -0
  17. package/dist/cli.js +210 -14
  18. package/dist/dashboard/index.cjs +111 -29
  19. package/dist/dashboard/index.d.cts +185 -0
  20. package/dist/dashboard/index.d.ts +5 -3
  21. package/dist/dashboard/index.js +1 -1
  22. package/dist/drizzle/index.cjs +3578 -1068
  23. package/dist/drizzle/index.d.cts +46 -0
  24. package/dist/drizzle/index.d.ts +2 -2
  25. package/dist/drizzle/index.js +6 -2
  26. package/dist/edge/index.cjs +368 -162
  27. package/dist/edge/index.d.cts +134 -0
  28. package/dist/edge/index.d.ts +11 -4
  29. package/dist/edge/index.js +314 -155
  30. package/dist/hono/index.cjs +3614 -1087
  31. package/dist/hono/index.d.cts +93 -0
  32. package/dist/hono/index.d.ts +7 -5
  33. package/dist/hono/index.js +16 -9
  34. package/dist/hono-CMv2VdQ6.d.cts +404 -0
  35. package/dist/{hono-DSV2ve66.d.ts → hono-Dvq04kX9.d.ts} +33 -7
  36. package/dist/http/index.cjs +3712 -1124
  37. package/dist/http/index.d.cts +103 -0
  38. package/dist/http/index.d.ts +13 -5
  39. package/dist/http/index.js +92 -43
  40. package/dist/index.cjs +4067 -1252
  41. package/dist/index.d.cts +1071 -0
  42. package/dist/index.d.ts +123 -27
  43. package/dist/index.js +878 -21
  44. package/dist/{murmur3-CMGw9PbV.d.ts → murmur3-DF8BFRnu.d.cts} +1 -0
  45. package/dist/murmur3-DF8BFRnu.d.ts +99 -0
  46. package/dist/nestjs/index.cjs +3635 -1112
  47. package/dist/nestjs/index.d.cts +299 -0
  48. package/dist/nestjs/index.d.ts +120 -33
  49. package/dist/nestjs/index.js +132 -43
  50. package/dist/next/index.cjs +3704 -1175
  51. package/dist/next/index.d.cts +238 -0
  52. package/dist/next/index.d.ts +39 -11
  53. package/dist/next/index.js +202 -102
  54. package/dist/prisma/index.cjs +29 -18
  55. package/dist/prisma/index.d.cts +62 -0
  56. package/dist/prisma/index.d.ts +2 -2
  57. package/dist/prisma/index.js +17 -18
  58. package/dist/serialize-worker.cjs +162 -39
  59. package/dist/serialize-worker.d.cts +2 -0
  60. package/dist/serialize-worker.js +9 -7
  61. package/dist/{types-Dyip5wAl.d.ts → types-F8vRWMrE.d.cts} +186 -54
  62. package/dist/types-F8vRWMrE.d.ts +1636 -0
  63. package/dist/{utils-CEsU_Z0s.d.ts → utils-Bvzn-rKS.d.cts} +3 -1
  64. package/dist/utils-Bvzn-rKS.d.ts +29 -0
  65. package/package.json +116 -37
  66. package/src/adapters/auto-pipeliner.ts +150 -26
  67. package/src/adapters/node-redis.ts +106 -27
  68. package/src/cache-service.ts +2215 -793
  69. package/src/cli.ts +25 -14
  70. package/src/codec.ts +3 -3
  71. package/src/compression.ts +39 -16
  72. package/src/cross-region.ts +22 -4
  73. package/src/dashboard/handler.ts +26 -6
  74. package/src/dashboard/html.ts +35 -10
  75. package/src/dashboard/index.ts +35 -13
  76. package/src/dashboard/types.ts +3 -1
  77. package/src/disk-tier.ts +448 -102
  78. package/src/drizzle/index.ts +2 -1
  79. package/src/edge/adapters/cloudflare-do.ts +25 -10
  80. package/src/edge/adapters/cloudflare-kv.ts +12 -9
  81. package/src/edge/adapters/upstash.ts +27 -11
  82. package/src/edge/cache.ts +200 -110
  83. package/src/edge/hono.ts +23 -6
  84. package/src/edge/types.ts +17 -5
  85. package/src/encryption.ts +50 -18
  86. package/src/env.d.ts +1 -1
  87. package/src/errors.ts +12 -0
  88. package/src/hono/index.ts +19 -9
  89. package/src/http/express.ts +62 -17
  90. package/src/http/fastify.ts +35 -21
  91. package/src/http/utils.ts +60 -20
  92. package/src/index.ts +6 -0
  93. package/src/ipc-telemetry.ts +35 -9
  94. package/src/latency-watchdog.ts +5 -1
  95. package/src/nestjs/decorators.ts +11 -6
  96. package/src/nestjs/index.ts +2 -0
  97. package/src/nestjs/keyv-adapter.ts +68 -0
  98. package/src/nestjs/tricache.module.ts +15 -12
  99. package/src/nestjs/tricache.store.ts +69 -26
  100. package/src/nestjs/types.ts +30 -16
  101. package/src/next/cache-handler.ts +151 -53
  102. package/src/next/index.ts +1 -0
  103. package/src/next/types.ts +25 -0
  104. package/src/origin-capacity.ts +134 -0
  105. package/src/prisma/index.ts +17 -24
  106. package/src/remote-snapshot.ts +37 -27
  107. package/src/serialize-worker.ts +8 -5
  108. package/src/sigv4-snapshot-adapter.ts +37 -27
  109. package/src/smart-memory-cache.ts +121 -44
  110. package/src/types.ts +117 -37
  111. package/src/utils/cgroup.ts +12 -0
  112. package/src/utils/config-comparison.ts +29 -0
  113. package/src/utils/deadline.ts +14 -0
  114. package/src/utils/diagnostics.ts +27 -0
  115. package/src/utils/lz4-size.ts +74 -0
  116. package/src/utils/namespace-key.ts +48 -0
  117. package/src/utils/query-key.ts +13 -0
  118. package/src/utils/redis-slot.ts +13 -0
  119. package/src/utils/snapshot-transfer.ts +83 -0
  120. package/src/utils/work-limits.ts +73 -0
  121. package/src/utils/write-revision.ts +4 -0
  122. package/src/wasm/bloom-filter-wasm.ts +25 -5
  123. package/src/worker-pool.ts +44 -15
  124. package/src/wtiny-lfu.ts +82 -20
  125. package/dist/cache-service-MG7PN6MX.js +0 -12
  126. package/dist/chunk-3I5L7K6I.js +0 -704
  127. package/dist/chunk-BQRURGGD.js +0 -60
@@ -13,6 +13,7 @@
13
13
  import type { CacheService } from '../cache-service.js';
14
14
  import type { WrapOptions } from '../types.js';
15
15
  import crypto from 'crypto';
16
+ import { deterministicStringify } from '../utils/query-key.js';
16
17
 
17
18
  export interface DrizzleCacheOptions extends WrapOptions {
18
19
  /** TriCache instance to use. If omitted, uses process-level singleton `CacheService.create()`. */
@@ -31,7 +32,7 @@ export interface DrizzleExecutableQuery<T> {
31
32
  * Generate a deterministic SHA-256 hash key for a Drizzle query based on its compiled SQL and parameters.
32
33
  */
33
34
  export function generateDrizzleCacheKey(sqlInfo: { sql: string; params: unknown[] }): string {
34
- const normalized = JSON.stringify(sqlInfo);
35
+ const normalized = deterministicStringify({ sql: sqlInfo.sql, params: sqlInfo.params });
35
36
  const hash = crypto.createHash('sha256').update(normalized, 'utf8').digest('hex').slice(0, 32);
36
37
  return `drizzle:${hash}`;
37
38
  }
@@ -10,6 +10,7 @@ interface DOEntry {
10
10
  * Enables strongly-consistent edge caching and coordination across edge isolates.
11
11
  */
12
12
  export class CloudflareDOStorageAdapter implements IEdgeRemoteStorage {
13
+ readonly tagConsistency = 'atomic' as const;
13
14
  private readonly storage: CloudflareDOStorage;
14
15
 
15
16
  constructor(storage: CloudflareDOStorage) {
@@ -84,18 +85,32 @@ export class CloudflareDOStorageAdapter implements IEdgeRemoteStorage {
84
85
  await this.storage.put(record);
85
86
  }
86
87
 
87
- async clear(prefix?: string): Promise<void> {
88
- if (!this.storage.list) return;
88
+ async *scanKeys(prefix: string): AsyncIterable<string> {
89
+ if (!this.storage.list) throw new Error('Durable Object storage does not support list');
90
+ let startAfter: string | undefined;
91
+ while (true) {
92
+ const page = await this.storage.list({ prefix, startAfter, limit: 256 });
93
+ if (page.size === 0) return;
94
+ const keys = [...page.keys()];
95
+ for (const key of keys) if (key.startsWith(prefix)) yield key;
96
+ const next = keys[keys.length - 1];
97
+ if (next === startAfter) throw new Error('Durable Object list did not advance');
98
+ startAfter = next;
99
+ if (page.size < 256) return;
100
+ }
101
+ }
89
102
 
90
- const map = await this.storage.list({ prefix });
91
- const keysToDelete = Array.from(map.keys());
92
- if (keysToDelete.length > 0) {
93
- await this.storage.delete(keysToDelete);
103
+ async clear(prefix = ''): Promise<void> {
104
+ let keys: string[] = [];
105
+ for await (const key of this.scanKeys(prefix)) {
106
+ keys.push(key);
107
+ if (keys.length === 256) { await this.storage.delete(keys); keys = []; }
94
108
  }
109
+ if (keys.length) await this.storage.delete(keys);
95
110
  }
96
111
 
97
112
  async incrementTagVersion(tag: string): Promise<number> {
98
- const key = `tag_ver:${tag}`;
113
+ const key = tag;
99
114
  const raw = await this.storage.get<DOEntry>(key);
100
115
  const prev = (raw && typeof raw === 'object' && 'val' in raw)
101
116
  ? parseInt(raw.val, 10) || 0
@@ -106,7 +121,7 @@ export class CloudflareDOStorageAdapter implements IEdgeRemoteStorage {
106
121
  }
107
122
 
108
123
  async getTagVersion(tag: string): Promise<number> {
109
- const raw = await this.storage.get<DOEntry>(`tag_ver:${tag}`);
124
+ const raw = await this.storage.get<DOEntry>(tag);
110
125
  if (raw && typeof raw === 'object' && 'val' in raw) {
111
126
  return parseInt(raw.val, 10) || 1;
112
127
  }
@@ -114,11 +129,11 @@ export class CloudflareDOStorageAdapter implements IEdgeRemoteStorage {
114
129
  }
115
130
 
116
131
  async batchGetTagVersions(tags: string[]): Promise<Record<string, number>> {
117
- const keys = tags.map(t => `tag_ver:${t}`);
132
+ const keys = tags;
118
133
  const map = await this.storage.get<DOEntry>(keys);
119
134
  const result: Record<string, number> = {};
120
135
  for (const tag of tags) {
121
- const entry = map.get(`tag_ver:${tag}`);
136
+ const entry = map.get(tag);
122
137
  if (entry && typeof entry === 'object' && 'val' in entry) {
123
138
  result[tag] = parseInt(entry.val, 10) || 1;
124
139
  } else {
@@ -14,6 +14,7 @@ const MIN_CF_KV_TTL = 60; // Cloudflare KV requires expirationTtl >= 60s
14
14
  * TTLs in a lightweight timestamp envelope and validating expiry on read.
15
15
  */
16
16
  export class CloudflareKVAdapter implements IEdgeRemoteStorage {
17
+ readonly tagConsistency = 'eventual' as const;
17
18
  private readonly kv: CloudflareKVNamespace;
18
19
 
19
20
  constructor(kv: CloudflareKVNamespace) {
@@ -77,21 +78,23 @@ export class CloudflareKVAdapter implements IEdgeRemoteStorage {
77
78
  await Promise.all(keys.map(k => this.set(k, entries[k], ttlSeconds)));
78
79
  }
79
80
 
80
- async clear(prefix?: string): Promise<void> {
81
- if (!this.kv.list) return;
82
-
81
+ async *scanKeys(prefix: string): AsyncIterable<string> {
82
+ if (!this.kv.list) throw new Error('Workers KV binding does not support list');
83
83
  let cursor: string | undefined;
84
84
  do {
85
- const res = await this.kv.list({ prefix, cursor });
86
- if (res.keys.length > 0) {
87
- await Promise.all(res.keys.map(item => this.kv.delete(item.name)));
88
- }
85
+ const res = await this.kv.list({ prefix, cursor, limit: 256 });
86
+ for (const item of res.keys) if (item.name.startsWith(prefix)) yield item.name;
89
87
  cursor = res.list_complete ? undefined : res.cursor;
88
+ if (!res.list_complete && !cursor) throw new Error('Workers KV list omitted its continuation cursor');
90
89
  } while (cursor);
91
90
  }
92
91
 
92
+ async clear(prefix = ''): Promise<void> {
93
+ for await (const key of this.scanKeys(prefix)) await this.kv.delete(key);
94
+ }
95
+
93
96
  async incrementTagVersion(tag: string): Promise<number> {
94
- const key = `tag_ver:${tag}`;
97
+ const key = tag;
95
98
  const raw = await this.kv.get(key);
96
99
  const prev = raw ? parseInt(raw, 10) || 0 : 0;
97
100
  const nextVer = Math.max(prev + 1, Date.now());
@@ -100,7 +103,7 @@ export class CloudflareKVAdapter implements IEdgeRemoteStorage {
100
103
  }
101
104
 
102
105
  async getTagVersion(tag: string): Promise<number> {
103
- const raw = await this.kv.get(`tag_ver:${tag}`);
106
+ const raw = await this.kv.get(tag);
104
107
  return raw ? parseInt(raw, 10) || 1 : 1;
105
108
  }
106
109
 
@@ -7,9 +7,11 @@ import type { IEdgeRemoteStorage, UpstashRedisOptions } from '../types';
7
7
  * Communicates over HTTPS REST with zero native socket dependencies.
8
8
  */
9
9
  export class UpstashRedisAdapter implements IEdgeRemoteStorage {
10
+ readonly tagConsistency = 'atomic' as const;
10
11
  private readonly baseUrl: string;
11
12
  private readonly token: string;
12
13
  private readonly customFetch: typeof globalThis.fetch;
14
+ private readonly dangerouslyFlushDatabase: boolean;
13
15
 
14
16
  constructor(options: UpstashRedisOptions) {
15
17
  if (!options?.url || !options?.token) {
@@ -18,6 +20,7 @@ export class UpstashRedisAdapter implements IEdgeRemoteStorage {
18
20
  this.baseUrl = options.url.replace(/\/+$/, '');
19
21
  this.token = options.token;
20
22
  this.customFetch = options.fetch ?? globalThis.fetch.bind(globalThis);
23
+ this.dangerouslyFlushDatabase = options.dangerouslyFlushDatabase ?? false;
21
24
  }
22
25
 
23
26
  private async _executeCommand<T = unknown>(command: unknown[]): Promise<T> {
@@ -107,32 +110,45 @@ export class UpstashRedisAdapter implements IEdgeRemoteStorage {
107
110
  }
108
111
  }
109
112
 
113
+ async *scanKeys(prefix: string): AsyncIterable<string> {
114
+ const pattern = prefix.replaceAll('\\', '\\\\').replaceAll('*', '\\*')
115
+ .replaceAll('?', '\\?').replaceAll('[', '\\[').replaceAll(']', '\\]') + '*';
116
+ let cursor = '0';
117
+ do {
118
+ const result = await this._executeCommand<[string, string[]]>(['SCAN', cursor, 'MATCH', pattern, 'COUNT', 256]);
119
+ if (!Array.isArray(result) || !Array.isArray(result[1])) throw new Error('Invalid Upstash SCAN response');
120
+ cursor = String(result[0]);
121
+ for (const key of result[1]) if (key.startsWith(prefix)) yield key;
122
+ } while (cursor !== '0');
123
+ }
124
+
110
125
  async clear(prefix?: string): Promise<void> {
111
- if (prefix) {
112
- const pattern = prefix.endsWith('*') ? prefix : `${prefix}*`;
113
- const keys = await this._executeCommand<string[]>(['KEYS', pattern]);
114
- if (Array.isArray(keys) && keys.length > 0) {
115
- await this._executeCommand(['DEL', ...keys]);
116
- }
117
- } else {
126
+ if (!prefix) {
127
+ if (!this.dangerouslyFlushDatabase) throw new Error('Unscoped Upstash clear requires dangerouslyFlushDatabase: true');
118
128
  await this._executeCommand(['FLUSHDB']);
129
+ return;
130
+ }
131
+ let keys: string[] = [];
132
+ for await (const key of this.scanKeys(prefix)) {
133
+ keys.push(key);
134
+ if (keys.length === 256) { await this._executeCommand(['DEL', ...keys]); keys = []; }
119
135
  }
136
+ if (keys.length) await this._executeCommand(['DEL', ...keys]);
120
137
  }
121
138
 
122
139
  async incrementTagVersion(tag: string): Promise<number> {
123
- const res = await this._executeCommand<number>(['INCR', `tag_ver:${tag}`]);
140
+ const res = await this._executeCommand<number>(['INCR', tag]);
124
141
  return typeof res === 'number' ? res : 1;
125
142
  }
126
143
 
127
144
  async getTagVersion(tag: string): Promise<number> {
128
- const res = await this._executeCommand<string | null>(['GET', `tag_ver:${tag}`]);
145
+ const res = await this._executeCommand<string | null>(['GET', tag]);
129
146
  return res ? parseInt(res, 10) || 1 : 1;
130
147
  }
131
148
 
132
149
  async batchGetTagVersions(tags: string[]): Promise<Record<string, number>> {
133
150
  if (tags.length === 0) return {};
134
- const keys = tags.map(t => `tag_ver:${t}`);
135
- const res = await this.mget(keys);
151
+ const res = await this.mget(tags);
136
152
  const result: Record<string, number> = {};
137
153
  for (let i = 0; i < tags.length; i++) {
138
154
  const raw = res[i];