File size: 1,377 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// DRY: shared between /api/cli-tools/status and /api/cli-tools/all-statuses (plan 14 F2)
// In-memory mtime-based cache for batch CLI tool status results.
// Cache invalidated when mtime changes. Lives until server restart (no TTL).

import type { ToolBatchStatus } from "@/shared/types/cliBatchStatus";

export interface CacheEntry {
  mtimeMs: number;
  result: ToolBatchStatus;
}

/** Singleton in-memory cache: toolId → { mtimeMs, result } */
const _cache = new Map<string, CacheEntry>();

/**
 * Get cached result for a toolId if mtime matches.
 * Returns null if:
 *  - entry doesn't exist
 *  - stored mtimeMs !== provided mtimeMs (config file changed)
 */
export function getCached(toolId: string, mtimeMs: number): ToolBatchStatus | null {
  const entry = _cache.get(toolId);
  if (!entry) return null;
  if (entry.mtimeMs !== mtimeMs) return null;
  return entry.result;
}

/**
 * Store a result in the cache for a toolId with its mtime.
 */
export function setCached(toolId: string, mtimeMs: number, result: ToolBatchStatus): void {
  _cache.set(toolId, { mtimeMs, result });
}

/**
 * Remove a specific toolId from the cache (e.g. after config write).
 */
export function invalidate(toolId: string): void {
  _cache.delete(toolId);
}

/**
 * Clear all cached entries. Primarily for testing isolation.
 */
export function clearCache(): void {
  _cache.clear();
}