| 10 | * Manages the cache for code indexing |
| 11 | */ |
| 12 | export class CacheManager implements ICacheManager { |
| 13 | private cachePath: vscode.Uri |
| 14 | private fileHashes: Record<string, string> = {} |
| 15 | private _debouncedSaveCache: () => void |
| 16 | |
| 17 | /** |
| 18 | * Creates a new cache manager |
| 19 | * @param context VS Code extension context |
| 20 | * @param workspacePath Path to the workspace |
| 21 | */ |
| 22 | constructor( |
| 23 | private context: vscode.ExtensionContext, |
| 24 | private workspacePath: string, |
| 25 | ) { |
| 26 | this.cachePath = vscode.Uri.joinPath( |
| 27 | context.globalStorageUri, |
| 28 | `roo-index-cache-${createHash("sha256").update(workspacePath).digest("hex")}.json`, |
| 29 | ) |
| 30 | this._debouncedSaveCache = debounce(async () => { |
| 31 | await this._performSave() |
| 32 | }, 1500) |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Initializes the cache manager by loading the cache file |
| 37 | */ |
| 38 | async initialize(): Promise<void> { |
| 39 | try { |
| 40 | const cacheData = await vscode.workspace.fs.readFile(this.cachePath) |
| 41 | this.fileHashes = JSON.parse(cacheData.toString()) |
| 42 | } catch (error) { |
| 43 | this.fileHashes = {} |
| 44 | TelemetryService.instance.captureEvent(TelemetryEventName.CODE_INDEX_ERROR, { |
| 45 | error: error instanceof Error ? error.message : String(error), |
| 46 | stack: error instanceof Error ? error.stack : undefined, |
| 47 | location: "initialize", |
| 48 | }) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Saves the cache to disk |
| 54 | */ |
| 55 | private async _performSave(): Promise<void> { |
| 56 | try { |
| 57 | await safeWriteJson(this.cachePath.fsPath, this.fileHashes) |
| 58 | } catch (error) { |
| 59 | console.error("Failed to save cache:", error) |
| 60 | TelemetryService.instance.captureEvent(TelemetryEventName.CODE_INDEX_ERROR, { |
| 61 | error: error instanceof Error ? error.message : String(error), |
| 62 | stack: error instanceof Error ? error.stack : undefined, |
| 63 | location: "_performSave", |
| 64 | }) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Clears the cache file by writing an empty object to it |
nothing calls this directly
no outgoing calls
no test coverage detected