|
| 1 | +/** |
| 2 | + * File-based session cache for the CLI. |
| 3 | + * Stores parsed SessionData on disk so subsequent runs skip unchanged files. |
| 4 | + * |
| 5 | + * Cache file: ~/.copilot-token-tracker/cli-cache.json |
| 6 | + */ |
| 7 | +import * as fs from 'fs'; |
| 8 | +import * as path from 'path'; |
| 9 | +import * as os from 'os'; |
| 10 | +import type { SessionData } from './helpers'; |
| 11 | + |
| 12 | +/** Bump this when the SessionData shape changes to force a full re-parse. */ |
| 13 | +const CACHE_VERSION = 1; |
| 14 | + |
| 15 | +/** Maximum number of entries to keep in the cache file. */ |
| 16 | +const MAX_CACHE_ENTRIES = 2000; |
| 17 | + |
| 18 | +interface CacheEntry { |
| 19 | + /** File modification time (ms since epoch) */ |
| 20 | + mtime: number; |
| 21 | + /** File size in bytes */ |
| 22 | + size: number; |
| 23 | + /** Parsed session data (lastModified stored as ISO string) */ |
| 24 | + data: Omit<SessionData, 'lastModified'> & { lastModified: string }; |
| 25 | +} |
| 26 | + |
| 27 | +interface CacheFile { |
| 28 | + version: number; |
| 29 | + entries: Record<string, CacheEntry>; |
| 30 | +} |
| 31 | + |
| 32 | +const CACHE_DIR = path.join(os.homedir(), '.copilot-token-tracker'); |
| 33 | +const CACHE_PATH = path.join(CACHE_DIR, 'cli-cache.json'); |
| 34 | + |
| 35 | +let cache: Map<string, CacheEntry> = new Map(); |
| 36 | +let cacheEnabled = true; |
| 37 | +let dirty = false; |
| 38 | + |
| 39 | +/** Disable caching (e.g. when --no-cache is passed). */ |
| 40 | +export function disableCache(): void { |
| 41 | + cacheEnabled = false; |
| 42 | + cache.clear(); |
| 43 | +} |
| 44 | + |
| 45 | +/** Load cache from disk. Safe to call multiple times — only loads once. */ |
| 46 | +export function loadCache(): void { |
| 47 | + if (!cacheEnabled) { return; } |
| 48 | + try { |
| 49 | + if (!fs.existsSync(CACHE_PATH)) { return; } |
| 50 | + const raw = fs.readFileSync(CACHE_PATH, 'utf-8'); |
| 51 | + const parsed: CacheFile = JSON.parse(raw); |
| 52 | + if (parsed.version !== CACHE_VERSION) { |
| 53 | + // Version mismatch — discard stale cache |
| 54 | + cache.clear(); |
| 55 | + return; |
| 56 | + } |
| 57 | + cache = new Map(Object.entries(parsed.entries)); |
| 58 | + } catch { |
| 59 | + // Corrupt / unreadable — start fresh |
| 60 | + cache.clear(); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** Save cache to disk (only if entries were added/updated). */ |
| 65 | +export function saveCache(): void { |
| 66 | + if (!cacheEnabled || !dirty) { return; } |
| 67 | + try { |
| 68 | + // Prune to MAX_CACHE_ENTRIES, keeping the most recently modified files |
| 69 | + if (cache.size > MAX_CACHE_ENTRIES) { |
| 70 | + const sorted = [...cache.entries()].sort((a, b) => b[1].mtime - a[1].mtime); |
| 71 | + cache = new Map(sorted.slice(0, MAX_CACHE_ENTRIES)); |
| 72 | + } |
| 73 | + |
| 74 | + fs.mkdirSync(CACHE_DIR, { recursive: true }); |
| 75 | + const payload: CacheFile = { |
| 76 | + version: CACHE_VERSION, |
| 77 | + entries: Object.fromEntries(cache), |
| 78 | + }; |
| 79 | + fs.writeFileSync(CACHE_PATH, JSON.stringify(payload), 'utf-8'); |
| 80 | + } catch { |
| 81 | + // Best-effort — don't crash the CLI if cache write fails |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Look up a cached result for a session file. |
| 87 | + * Returns the SessionData if the cache entry matches the file's current mtime and size, |
| 88 | + * or null if the file needs to be re-parsed. |
| 89 | + */ |
| 90 | +export function getCached(filePath: string, mtime: number, size: number): SessionData | null { |
| 91 | + if (!cacheEnabled) { return null; } |
| 92 | + const entry = cache.get(filePath); |
| 93 | + if (!entry) { return null; } |
| 94 | + if (entry.mtime !== mtime || entry.size !== size) { return null; } |
| 95 | + // Rehydrate the Date |
| 96 | + return { |
| 97 | + ...entry.data, |
| 98 | + lastModified: new Date(entry.data.lastModified), |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +/** Store a parsed result in the cache. */ |
| 103 | +export function setCached(filePath: string, mtime: number, size: number, data: SessionData): void { |
| 104 | + if (!cacheEnabled) { return; } |
| 105 | + dirty = true; |
| 106 | + cache.set(filePath, { |
| 107 | + mtime, |
| 108 | + size, |
| 109 | + data: { |
| 110 | + ...data, |
| 111 | + lastModified: data.lastModified.toISOString(), |
| 112 | + }, |
| 113 | + }); |
| 114 | +} |
| 115 | + |
| 116 | +/** Return cache hit/miss stats for display. */ |
| 117 | +export function getCacheStats(): { entries: number; enabled: boolean } { |
| 118 | + return { entries: cache.size, enabled: cacheEnabled }; |
| 119 | +} |
0 commit comments