layercache 3.0.0 → 3.1.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.
package/dist/edge.cjs CHANGED
@@ -389,30 +389,34 @@ var PatternMatcher = class _PatternMatcher {
389
389
  };
390
390
 
391
391
  // src/invalidation/TagIndex.ts
392
- var MAX_PATTERN_RECURSION_DEPTH = 500;
392
+ var DEFAULT_TOUCH_REFRESH_INTERVAL_MS = 1e3;
393
393
  var TagIndex = class {
394
394
  tagToKeys = /* @__PURE__ */ new Map();
395
395
  keyToTags = /* @__PURE__ */ new Map();
396
396
  knownKeys = /* @__PURE__ */ new Map();
397
397
  maxKnownKeys;
398
+ touchRefreshIntervalMs;
398
399
  nextNodeId = 1;
399
400
  root = this.createTrieNode();
400
401
  constructor(options = {}) {
401
402
  this.maxKnownKeys = options.maxKnownKeys ?? 1e5;
403
+ this.touchRefreshIntervalMs = options.touchRefreshIntervalMs ?? DEFAULT_TOUCH_REFRESH_INTERVAL_MS;
402
404
  }
403
405
  /**
404
406
  * Records a key as known without changing tag assignments.
405
407
  */
406
408
  async touch(key) {
407
- this.insertKnownKey(key);
408
- this.pruneKnownKeysIfNeeded();
409
+ if (this.insertKnownKey(key)) {
410
+ this.pruneKnownKeysIfNeeded();
411
+ }
409
412
  }
410
413
  /**
411
414
  * Replaces the tags associated with a key and records the key as known.
412
415
  */
413
416
  async track(key, tags) {
414
- this.insertKnownKey(key);
415
- this.pruneKnownKeysIfNeeded();
417
+ if (this.insertKnownKey(key)) {
418
+ this.pruneKnownKeysIfNeeded();
419
+ }
416
420
  if (tags.length === 0) {
417
421
  return;
418
422
  }
@@ -482,9 +486,14 @@ var TagIndex = class {
482
486
  * Returns known keys matching a wildcard pattern.
483
487
  */
484
488
  async matchPattern(pattern) {
485
- const matches = /* @__PURE__ */ new Set();
486
- this.collectPatternMatches(this.root, "", pattern, 0, matches, /* @__PURE__ */ new Set(), 0);
487
- return [...matches];
489
+ const literalPrefix = this.literalPrefix(pattern);
490
+ const node = this.findNode(literalPrefix);
491
+ if (!node) {
492
+ return [];
493
+ }
494
+ const candidates = [];
495
+ this.collectFromNode(node, literalPrefix, candidates);
496
+ return candidates.filter((key) => PatternMatcher.matches(pattern, key));
488
497
  }
489
498
  /**
490
499
  * Visits known keys matching a wildcard pattern.
@@ -514,13 +523,18 @@ var TagIndex = class {
514
523
  };
515
524
  }
516
525
  insertKnownKey(key) {
517
- const isNew = !this.knownKeys.has(key);
526
+ const previousTouch = this.knownKeys.get(key);
527
+ const isNew = previousTouch === void 0;
528
+ const now = Date.now();
529
+ if (!isNew && now - previousTouch < this.touchRefreshIntervalMs) {
530
+ return false;
531
+ }
518
532
  if (!isNew) {
519
533
  this.knownKeys.delete(key);
520
534
  }
521
- this.knownKeys.set(key, Date.now());
535
+ this.knownKeys.set(key, now);
522
536
  if (!isNew) {
523
- return;
537
+ return true;
524
538
  }
525
539
  let node = this.root;
526
540
  for (const character of key) {
@@ -532,6 +546,7 @@ var TagIndex = class {
532
546
  node = child;
533
547
  }
534
548
  node.terminal = true;
549
+ return true;
535
550
  }
536
551
  findNode(prefix) {
537
552
  let node = this.root;
@@ -544,74 +559,41 @@ var TagIndex = class {
544
559
  return node;
545
560
  }
546
561
  collectFromNode(node, prefix, matches) {
547
- if (node.terminal) {
548
- matches.push(prefix);
549
- }
550
- for (const [character, child] of node.children) {
551
- this.collectFromNode(child, `${prefix}${character}`, matches);
562
+ const stack = [{ node, prefix }];
563
+ while (stack.length > 0) {
564
+ const current = stack.pop();
565
+ if (!current) {
566
+ continue;
567
+ }
568
+ if (current.node.terminal) {
569
+ matches.push(current.prefix);
570
+ }
571
+ const children = [...current.node.children].reverse();
572
+ for (const [character, child] of children) {
573
+ stack.push({ node: child, prefix: `${current.prefix}${character}` });
574
+ }
552
575
  }
553
576
  }
554
577
  async visitFromNode(node, prefix, visitor) {
555
- if (node.terminal) {
556
- await visitor(prefix);
557
- }
558
- for (const [character, child] of node.children) {
559
- await this.visitFromNode(child, `${prefix}${character}`, visitor);
560
- }
561
- }
562
- collectPatternMatches(node, prefix, pattern, patternIndex, matches, visited, depth) {
563
- if (depth > MAX_PATTERN_RECURSION_DEPTH) {
564
- return;
565
- }
566
- const stateKey = `${node.id}:${patternIndex}`;
567
- if (visited.has(stateKey)) {
568
- return;
569
- }
570
- visited.add(stateKey);
571
- if (patternIndex === pattern.length) {
572
- if (node.terminal) {
573
- matches.add(prefix);
578
+ const stack = [{ node, prefix }];
579
+ while (stack.length > 0) {
580
+ const current = stack.pop();
581
+ if (!current) {
582
+ continue;
574
583
  }
575
- return;
576
- }
577
- const patternChar = pattern[patternIndex];
578
- if (patternChar === void 0) {
579
- return;
580
- }
581
- if (patternChar === "*") {
582
- this.collectPatternMatches(node, prefix, pattern, patternIndex + 1, matches, visited, depth + 1);
583
- for (const [character, child2] of node.children) {
584
- this.collectPatternMatches(child2, `${prefix}${character}`, pattern, patternIndex, matches, visited, depth + 1);
584
+ if (current.node.terminal) {
585
+ await visitor(current.prefix);
585
586
  }
586
- return;
587
- }
588
- if (patternChar === "?") {
589
- for (const [character, child2] of node.children) {
590
- this.collectPatternMatches(
591
- child2,
592
- `${prefix}${character}`,
593
- pattern,
594
- patternIndex + 1,
595
- matches,
596
- visited,
597
- depth + 1
598
- );
587
+ const children = [...current.node.children].reverse();
588
+ for (const [character, child] of children) {
589
+ stack.push({ node: child, prefix: `${current.prefix}${character}` });
599
590
  }
600
- return;
601
- }
602
- const child = node.children.get(patternChar);
603
- if (child) {
604
- this.collectPatternMatches(
605
- child,
606
- `${prefix}${patternChar}`,
607
- pattern,
608
- patternIndex + 1,
609
- matches,
610
- visited,
611
- depth + 1
612
- );
613
591
  }
614
592
  }
593
+ literalPrefix(pattern) {
594
+ const wildcardIndex = pattern.search(/[*?]/);
595
+ return wildcardIndex === -1 ? pattern : pattern.slice(0, wildcardIndex);
596
+ }
615
597
  pruneKnownKeysIfNeeded() {
616
598
  if (this.maxKnownKeys === void 0 || this.knownKeys.size <= this.maxKnownKeys) {
617
599
  return;
package/dist/edge.d.cts CHANGED
@@ -1,2 +1,2 @@
1
- export { m as CacheContextOptionsContext, o as CacheEntryWriteKind, p as CacheEntryWriteOptions, e as CacheGetOptions, f as CacheLayer, h as CacheLayerSetManyEntry, y as CacheMetricsSnapshot, B as CacheRateLimitOptions, H as CacheTtlPolicy, J as CacheTtlPolicyContext, O as CacheWriteOptions, P as EvictionPolicy, R as MemoryLayer, S as MemoryLayerOptions, T as MemoryLayerSnapshotEntry, U as PatternMatcher, V as TagIndex, W as createHonoCacheMiddleware } from './edge-BDyuPmIq.cjs';
1
+ export { m as CacheContextOptionsContext, p as CacheEntryWriteKind, q as CacheEntryWriteOptions, e as CacheGetOptions, f as CacheLayer, h as CacheLayerSetManyEntry, z as CacheMetricsSnapshot, D as CacheRateLimitOptions, J as CacheTtlPolicy, K as CacheTtlPolicyContext, P as CacheWriteOptions, Q as EvictionPolicy, S as MemoryLayer, T as MemoryLayerOptions, U as MemoryLayerSnapshotEntry, V as PatternMatcher, W as TagIndex, X as createHonoCacheMiddleware } from './edge-LBUuZAdr.cjs';
2
2
  import 'node:events';
package/dist/edge.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { m as CacheContextOptionsContext, o as CacheEntryWriteKind, p as CacheEntryWriteOptions, e as CacheGetOptions, f as CacheLayer, h as CacheLayerSetManyEntry, y as CacheMetricsSnapshot, B as CacheRateLimitOptions, H as CacheTtlPolicy, J as CacheTtlPolicyContext, O as CacheWriteOptions, P as EvictionPolicy, R as MemoryLayer, S as MemoryLayerOptions, T as MemoryLayerSnapshotEntry, U as PatternMatcher, V as TagIndex, W as createHonoCacheMiddleware } from './edge-BDyuPmIq.js';
1
+ export { m as CacheContextOptionsContext, p as CacheEntryWriteKind, q as CacheEntryWriteOptions, e as CacheGetOptions, f as CacheLayer, h as CacheLayerSetManyEntry, z as CacheMetricsSnapshot, D as CacheRateLimitOptions, J as CacheTtlPolicy, K as CacheTtlPolicyContext, P as CacheWriteOptions, Q as EvictionPolicy, S as MemoryLayer, T as MemoryLayerOptions, U as MemoryLayerSnapshotEntry, V as PatternMatcher, W as TagIndex, X as createHonoCacheMiddleware } from './edge-LBUuZAdr.js';
2
2
  import 'node:events';
package/dist/edge.js CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  MemoryLayer,
3
3
  TagIndex,
4
4
  createHonoCacheMiddleware
5
- } from "./chunk-5CIBABDH.js";
5
+ } from "./chunk-XMUT66SH.js";
6
6
  import {
7
7
  PatternMatcher
8
8
  } from "./chunk-KJDFYE5T.js";