File size: 1,395 Bytes
a5d1c1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { FileInfo } from '../types/contract.js';

export interface CachedTreeEntry {
  expiresAt: number;
  files: FileInfo[];
  pathMap: Map<string, string>; // lowercase normalized path -> exact case path
}

export class TreeCache {
  private cache = new Map<string, CachedTreeEntry>();

  private makeKey(owner: string, repo: string, branch: string): string {
    return `${owner.toLowerCase()}/${repo.toLowerCase()}:${branch.toLowerCase()}`;
  }

  get(owner: string, repo: string, branch: string): CachedTreeEntry | undefined {
    const key = this.makeKey(owner, repo, branch);
    const entry = this.cache.get(key);
    if (!entry) {
      return undefined;
    }

    if (Date.now() > entry.expiresAt) {
      this.cache.delete(key);
      return undefined;
    }

    return entry;
  }

  set(
    owner: string,
    repo: string,
    branch: string,
    files: FileInfo[],
    pathMap: Map<string, string>,
    ttlMs: number
  ): void {
    const key = this.makeKey(owner, repo, branch);
    this.cache.set(key, {
      expiresAt: Date.now() + ttlMs,
      files,
      pathMap,
    });
  }

  invalidate(owner: string, repo: string, branch: string): void {
    const key = this.makeKey(owner, repo, branch);
    this.cache.delete(key);
  }

  clear(): void {
    this.cache.clear();
  }

  size(): number {
    return this.cache.size;
  }
}

export const treeCache = new TreeCache();