tricache 1.0.0 → 1.0.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 (43) hide show
  1. package/README.md +1 -1
  2. package/dist/{cache-service-pJ-nFcVj.d.ts → cache-service-CREOp7MM.d.ts} +29 -1
  3. package/dist/{cache-service-26GCO3UU.js → cache-service-MG7PN6MX.js} +1 -1
  4. package/dist/{chunk-VXFTFZ5N.js → chunk-3I5L7K6I.js} +17 -0
  5. package/dist/{chunk-7SZSQS55.js → chunk-JYQFQJNJ.js} +533 -295
  6. package/dist/cli.cjs +534 -296
  7. package/dist/cli.js +2 -2
  8. package/dist/dashboard/index.d.ts +2 -2
  9. package/dist/drizzle/index.cjs +533 -295
  10. package/dist/drizzle/index.d.ts +2 -2
  11. package/dist/drizzle/index.js +1 -1
  12. package/dist/edge/index.cjs +55 -32
  13. package/dist/edge/index.d.ts +3 -3
  14. package/dist/edge/index.js +55 -32
  15. package/dist/hono/index.cjs +533 -295
  16. package/dist/hono/index.d.ts +2 -2
  17. package/dist/hono/index.js +1 -1
  18. package/dist/{hono-DuNvciyq.d.ts → hono-DSV2ve66.d.ts} +8 -1
  19. package/dist/http/index.cjs +585 -316
  20. package/dist/http/index.d.ts +3 -3
  21. package/dist/http/index.js +55 -24
  22. package/dist/index.cjs +550 -295
  23. package/dist/index.d.ts +5 -4
  24. package/dist/index.js +2 -2
  25. package/dist/nestjs/index.cjs +533 -295
  26. package/dist/nestjs/index.d.ts +2 -2
  27. package/dist/nestjs/index.js +2 -2
  28. package/dist/next/index.cjs +535 -303
  29. package/dist/next/index.d.ts +2 -2
  30. package/dist/next/index.js +3 -9
  31. package/dist/prisma/index.d.ts +2 -2
  32. package/dist/{types-5HXqBfAf.d.ts → types-Dyip5wAl.d.ts} +2 -0
  33. package/package.json +1 -1
  34. package/src/adapters/node-redis.ts +19 -0
  35. package/src/cache-service.ts +599 -320
  36. package/src/cli.ts +1 -1
  37. package/src/edge/cache.ts +60 -35
  38. package/src/http/express.ts +55 -16
  39. package/src/http/fastify.ts +3 -7
  40. package/src/next/cache-handler.ts +1 -4
  41. package/src/next/isr-handler.ts +1 -4
  42. package/src/smart-memory-cache.ts +3 -2
  43. package/src/types.ts +2 -0
@@ -1,5 +1,5 @@
1
- import { C as CacheService } from '../cache-service-pJ-nFcVj.js';
2
- import { W as WrapOptions } from '../types-5HXqBfAf.js';
1
+ import { C as CacheService } from '../cache-service-CREOp7MM.js';
2
+ import { W as WrapOptions } from '../types-Dyip5wAl.js';
3
3
  import 'crypto';
4
4
 
5
5
  /**
@@ -9,7 +9,7 @@ async function withCache(query, options = {}) {
9
9
  const { cache, key, ttl = 300, swr, priority, tags, dependsOn, refreshAhead, xfetchBeta, notFoundTtl } = options;
10
10
  let activeCache = cache;
11
11
  if (!activeCache) {
12
- const { CacheService } = await import("../cache-service-26GCO3UU.js");
12
+ const { CacheService } = await import("../cache-service-MG7PN6MX.js");
13
13
  activeCache = CacheService.create();
14
14
  }
15
15
  let cacheKey = key;
@@ -438,7 +438,7 @@ function isChildTag(parentTag, candidateTag, delimiter = ":") {
438
438
  }
439
439
 
440
440
  // src/edge/cache.ts
441
- var EdgeCacheService = class {
441
+ var EdgeCacheService = class _EdgeCacheService {
442
442
  maxKeys;
443
443
  maxBytes;
444
444
  defaultTtl;
@@ -455,6 +455,7 @@ var EdgeCacheService = class {
455
455
  // Tag indexing for edge
456
456
  tagIndex = /* @__PURE__ */ new Map();
457
457
  // Tag version in-isolate cache with 800ms micro-TTL (prevents HTTP fetch amplification on L1 hits)
458
+ static MAX_TAG_VERSION_CACHE = 1e4;
458
459
  tagVersionCache = /* @__PURE__ */ new Map();
459
460
  tagVersionTtlMs = 800;
460
461
  constructor(options) {
@@ -537,6 +538,21 @@ var EdgeCacheService = class {
537
538
  }
538
539
  };
539
540
  }
541
+ _setTagVersionCache(tag, version, expiresAt) {
542
+ if (this.tagVersionCache.size >= _EdgeCacheService.MAX_TAG_VERSION_CACHE && !this.tagVersionCache.has(tag)) {
543
+ const now = Date.now();
544
+ for (const [k, v] of this.tagVersionCache) {
545
+ if (v.expiresAt <= now) {
546
+ this.tagVersionCache.delete(k);
547
+ }
548
+ }
549
+ if (this.tagVersionCache.size >= _EdgeCacheService.MAX_TAG_VERSION_CACHE) {
550
+ const oldestKey = this.tagVersionCache.keys().next().value;
551
+ if (oldestKey !== void 0) this.tagVersionCache.delete(oldestKey);
552
+ }
553
+ }
554
+ this.tagVersionCache.set(tag, { version, expiresAt });
555
+ }
540
556
  async _getTagVersion(tag) {
541
557
  const cached = this.tagVersionCache.get(tag);
542
558
  const now = Date.now();
@@ -558,7 +574,7 @@ var EdgeCacheService = class {
558
574
  } else {
559
575
  version = cached?.version ?? 1;
560
576
  }
561
- this.tagVersionCache.set(tag, { version, expiresAt: now + this.tagVersionTtlMs });
577
+ this._setTagVersionCache(tag, version, now + this.tagVersionTtlMs);
562
578
  return version;
563
579
  }
564
580
  async _batchGetTagVersions(tags) {
@@ -582,7 +598,7 @@ var EdgeCacheService = class {
582
598
  const t = missingTags[i];
583
599
  const v = remoteVersions[this.nk(t)] ?? remoteVersions[t] ?? 1;
584
600
  result[t] = v;
585
- this.tagVersionCache.set(t, { version: v, expiresAt: now + this.tagVersionTtlMs });
601
+ this._setTagVersionCache(t, v, now + this.tagVersionTtlMs);
586
602
  }
587
603
  } else {
588
604
  await Promise.all(missingTags.map(async (t) => {
@@ -815,6 +831,40 @@ var EdgeCacheService = class {
815
831
  span.end();
816
832
  }
817
833
  }
834
+ /**
835
+ * Executes invalidation for a single already-resolved concrete tag without
836
+ * re-entering wildcard/cascade resolution.
837
+ */
838
+ async _invalidateConcreteTag(tag) {
839
+ let newVer = 1;
840
+ if (this.remoteStorage) {
841
+ try {
842
+ if (typeof this.remoteStorage.incrementTagVersion === "function") {
843
+ newVer = await this.remoteStorage.incrementTagVersion(this.nk(tag));
844
+ } else {
845
+ const key = this.nk(`tag_ver:${tag}`);
846
+ const current = await this._getTagVersion(tag);
847
+ newVer = Math.max(current + 1, Date.now());
848
+ await this.remoteStorage.set(key, String(newVer));
849
+ }
850
+ } catch {
851
+ const current = this.tagVersionCache.get(tag)?.version ?? 1;
852
+ newVer = current + 1;
853
+ }
854
+ } else {
855
+ const current = this.tagVersionCache.get(tag)?.version ?? 1;
856
+ newVer = current + 1;
857
+ }
858
+ this._setTagVersionCache(tag, newVer, Date.now() + 6e4);
859
+ const keys = this.tagIndex.get(tag);
860
+ if (keys) {
861
+ const keysArr = Array.from(keys);
862
+ for (const k of keysArr) {
863
+ this._deleteL1(k);
864
+ }
865
+ this.tagIndex.delete(tag);
866
+ }
867
+ }
818
868
  /**
819
869
  * Invalidates all cached entries associated with a tag.
820
870
  */
@@ -841,38 +891,11 @@ var EdgeCacheService = class {
841
891
  }
842
892
  if (shouldCascade && !isPattern) matched.add(tag);
843
893
  for (const t of matched) {
844
- await this.invalidateTag(t);
894
+ await this._invalidateConcreteTag(t);
845
895
  }
846
896
  return;
847
897
  }
848
- let newVer = 1;
849
- if (this.remoteStorage) {
850
- try {
851
- if (typeof this.remoteStorage.incrementTagVersion === "function") {
852
- newVer = await this.remoteStorage.incrementTagVersion(this.nk(tag));
853
- } else {
854
- const key = this.nk(`tag_ver:${tag}`);
855
- const current = await this._getTagVersion(tag);
856
- newVer = Math.max(current + 1, Date.now());
857
- await this.remoteStorage.set(key, String(newVer));
858
- }
859
- } catch {
860
- const current = this.tagVersionCache.get(tag)?.version ?? 1;
861
- newVer = current + 1;
862
- }
863
- } else {
864
- const current = this.tagVersionCache.get(tag)?.version ?? 1;
865
- newVer = current + 1;
866
- }
867
- this.tagVersionCache.set(tag, { version: newVer, expiresAt: Date.now() + 6e4 });
868
- const keys = this.tagIndex.get(tag);
869
- if (keys) {
870
- const keysArr = Array.from(keys);
871
- for (const k of keysArr) {
872
- this._deleteL1(k);
873
- }
874
- this.tagIndex.delete(tag);
875
- }
898
+ await this._invalidateConcreteTag(tag);
876
899
  } catch (err) {
877
900
  span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
878
901
  span.recordException?.(err);
@@ -1,7 +1,7 @@
1
- import { I as IEdgeRemoteStorage, U as UpstashRedisOptions, a as CloudflareKVNamespace, d as CloudflareDOStorage } from '../hono-DuNvciyq.js';
2
- export { C as CachedEdgeHttpResponse, e as CloudflareR2Bucket, f as CloudflareR2Object, E as EdgeCacheOptions, g as EdgeCacheService, i as EdgeExportSnapshotOptions, j as EdgeGetOptions, k as EdgeHydrateOptions, l as EdgeSnapshotPayload, m as EdgeSnapshotSource, H as HonoEdgeCacheOptions, n as IEdgeBloomFilter, b as buildEdgeDeterministicKey, c as computeEdgeETag, h as honoEdgeCache } from '../hono-DuNvciyq.js';
1
+ import { I as IEdgeRemoteStorage, U as UpstashRedisOptions, a as CloudflareKVNamespace, d as CloudflareDOStorage } from '../hono-DSV2ve66.js';
2
+ export { C as CachedEdgeHttpResponse, e as CloudflareR2Bucket, f as CloudflareR2Object, E as EdgeCacheOptions, g as EdgeCacheService, i as EdgeExportSnapshotOptions, j as EdgeGetOptions, k as EdgeHydrateOptions, l as EdgeSnapshotPayload, m as EdgeSnapshotSource, H as HonoEdgeCacheOptions, n as IEdgeBloomFilter, b as buildEdgeDeterministicKey, c as computeEdgeETag, h as honoEdgeCache } from '../hono-DSV2ve66.js';
3
3
  export { B as BloomFilterStats, M as Murmur3BloomFilter, W as WasmBloomFilter, m as murmur3_32 } from '../murmur3-CMGw9PbV.js';
4
- import '../types-5HXqBfAf.js';
4
+ import '../types-Dyip5wAl.js';
5
5
  import 'crypto';
6
6
 
7
7
  type WebCryptoMode = 'aes-256-gcm' | 'aes-128-gcm';
@@ -152,7 +152,7 @@ var WebCryptoEncryption = class {
152
152
  };
153
153
 
154
154
  // src/edge/cache.ts
155
- var EdgeCacheService = class {
155
+ var EdgeCacheService = class _EdgeCacheService {
156
156
  maxKeys;
157
157
  maxBytes;
158
158
  defaultTtl;
@@ -169,6 +169,7 @@ var EdgeCacheService = class {
169
169
  // Tag indexing for edge
170
170
  tagIndex = /* @__PURE__ */ new Map();
171
171
  // Tag version in-isolate cache with 800ms micro-TTL (prevents HTTP fetch amplification on L1 hits)
172
+ static MAX_TAG_VERSION_CACHE = 1e4;
172
173
  tagVersionCache = /* @__PURE__ */ new Map();
173
174
  tagVersionTtlMs = 800;
174
175
  constructor(options) {
@@ -251,6 +252,21 @@ var EdgeCacheService = class {
251
252
  }
252
253
  };
253
254
  }
255
+ _setTagVersionCache(tag, version, expiresAt) {
256
+ if (this.tagVersionCache.size >= _EdgeCacheService.MAX_TAG_VERSION_CACHE && !this.tagVersionCache.has(tag)) {
257
+ const now = Date.now();
258
+ for (const [k, v] of this.tagVersionCache) {
259
+ if (v.expiresAt <= now) {
260
+ this.tagVersionCache.delete(k);
261
+ }
262
+ }
263
+ if (this.tagVersionCache.size >= _EdgeCacheService.MAX_TAG_VERSION_CACHE) {
264
+ const oldestKey = this.tagVersionCache.keys().next().value;
265
+ if (oldestKey !== void 0) this.tagVersionCache.delete(oldestKey);
266
+ }
267
+ }
268
+ this.tagVersionCache.set(tag, { version, expiresAt });
269
+ }
254
270
  async _getTagVersion(tag) {
255
271
  const cached = this.tagVersionCache.get(tag);
256
272
  const now = Date.now();
@@ -272,7 +288,7 @@ var EdgeCacheService = class {
272
288
  } else {
273
289
  version = cached?.version ?? 1;
274
290
  }
275
- this.tagVersionCache.set(tag, { version, expiresAt: now + this.tagVersionTtlMs });
291
+ this._setTagVersionCache(tag, version, now + this.tagVersionTtlMs);
276
292
  return version;
277
293
  }
278
294
  async _batchGetTagVersions(tags) {
@@ -296,7 +312,7 @@ var EdgeCacheService = class {
296
312
  const t = missingTags[i];
297
313
  const v = remoteVersions[this.nk(t)] ?? remoteVersions[t] ?? 1;
298
314
  result[t] = v;
299
- this.tagVersionCache.set(t, { version: v, expiresAt: now + this.tagVersionTtlMs });
315
+ this._setTagVersionCache(t, v, now + this.tagVersionTtlMs);
300
316
  }
301
317
  } else {
302
318
  await Promise.all(missingTags.map(async (t) => {
@@ -529,6 +545,40 @@ var EdgeCacheService = class {
529
545
  span.end();
530
546
  }
531
547
  }
548
+ /**
549
+ * Executes invalidation for a single already-resolved concrete tag without
550
+ * re-entering wildcard/cascade resolution.
551
+ */
552
+ async _invalidateConcreteTag(tag) {
553
+ let newVer = 1;
554
+ if (this.remoteStorage) {
555
+ try {
556
+ if (typeof this.remoteStorage.incrementTagVersion === "function") {
557
+ newVer = await this.remoteStorage.incrementTagVersion(this.nk(tag));
558
+ } else {
559
+ const key = this.nk(`tag_ver:${tag}`);
560
+ const current = await this._getTagVersion(tag);
561
+ newVer = Math.max(current + 1, Date.now());
562
+ await this.remoteStorage.set(key, String(newVer));
563
+ }
564
+ } catch {
565
+ const current = this.tagVersionCache.get(tag)?.version ?? 1;
566
+ newVer = current + 1;
567
+ }
568
+ } else {
569
+ const current = this.tagVersionCache.get(tag)?.version ?? 1;
570
+ newVer = current + 1;
571
+ }
572
+ this._setTagVersionCache(tag, newVer, Date.now() + 6e4);
573
+ const keys = this.tagIndex.get(tag);
574
+ if (keys) {
575
+ const keysArr = Array.from(keys);
576
+ for (const k of keysArr) {
577
+ this._deleteL1(k);
578
+ }
579
+ this.tagIndex.delete(tag);
580
+ }
581
+ }
532
582
  /**
533
583
  * Invalidates all cached entries associated with a tag.
534
584
  */
@@ -555,38 +605,11 @@ var EdgeCacheService = class {
555
605
  }
556
606
  if (shouldCascade && !isPattern) matched.add(tag);
557
607
  for (const t of matched) {
558
- await this.invalidateTag(t);
608
+ await this._invalidateConcreteTag(t);
559
609
  }
560
610
  return;
561
611
  }
562
- let newVer = 1;
563
- if (this.remoteStorage) {
564
- try {
565
- if (typeof this.remoteStorage.incrementTagVersion === "function") {
566
- newVer = await this.remoteStorage.incrementTagVersion(this.nk(tag));
567
- } else {
568
- const key = this.nk(`tag_ver:${tag}`);
569
- const current = await this._getTagVersion(tag);
570
- newVer = Math.max(current + 1, Date.now());
571
- await this.remoteStorage.set(key, String(newVer));
572
- }
573
- } catch {
574
- const current = this.tagVersionCache.get(tag)?.version ?? 1;
575
- newVer = current + 1;
576
- }
577
- } else {
578
- const current = this.tagVersionCache.get(tag)?.version ?? 1;
579
- newVer = current + 1;
580
- }
581
- this.tagVersionCache.set(tag, { version: newVer, expiresAt: Date.now() + 6e4 });
582
- const keys = this.tagIndex.get(tag);
583
- if (keys) {
584
- const keysArr = Array.from(keys);
585
- for (const k of keysArr) {
586
- this._deleteL1(k);
587
- }
588
- this.tagIndex.delete(tag);
589
- }
612
+ await this._invalidateConcreteTag(tag);
590
613
  } catch (err) {
591
614
  span.setStatus({ code: 2, message: err instanceof Error ? err.message : String(err) });
592
615
  span.recordException?.(err);