diff options
| author | Bryant Chandler <[email protected]> | 2025-08-05 16:18:03 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-08-05 23:18:03 +0000 |
| commit | 12a9bc3ed94fab3071529b5304d46bcc5b4fe756 (patch) | |
| tree | 90967b6670668c6c476719ac04422e1744cbabd6 /packages/core/src/utils/filesearch/crawlCache.ts | |
| parent | 2141b39c3d713a19f2dd8012a76c2ff8b7c30a5e (diff) | |
feat(core, cli): Introduce high-performance FileSearch engine (#5136)
Co-authored-by: Jacob Richman <[email protected]>
Diffstat (limited to 'packages/core/src/utils/filesearch/crawlCache.ts')
| -rw-r--r-- | packages/core/src/utils/filesearch/crawlCache.ts | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/packages/core/src/utils/filesearch/crawlCache.ts b/packages/core/src/utils/filesearch/crawlCache.ts new file mode 100644 index 00000000..3cc948c6 --- /dev/null +++ b/packages/core/src/utils/filesearch/crawlCache.ts @@ -0,0 +1,65 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import crypto from 'node:crypto'; + +const crawlCache = new Map<string, string[]>(); +const cacheTimers = new Map<string, NodeJS.Timeout>(); + +/** + * Generates a unique cache key based on the project directory and the content + * of ignore files. This ensures that the cache is invalidated if the project + * or ignore rules change. + */ +export const getCacheKey = ( + directory: string, + ignoreContent: string, +): string => { + const hash = crypto.createHash('sha256'); + hash.update(directory); + hash.update(ignoreContent); + return hash.digest('hex'); +}; + +/** + * Reads cached data from the in-memory cache. + * Returns undefined if the key is not found. + */ +export const read = (key: string): string[] | undefined => crawlCache.get(key); + +/** + * Writes data to the in-memory cache and sets a timer to evict it after the TTL. + */ +export const write = (key: string, results: string[], ttlMs: number): void => { + // Clear any existing timer for this key to prevent premature deletion + if (cacheTimers.has(key)) { + clearTimeout(cacheTimers.get(key)!); + } + + // Store the new data + crawlCache.set(key, results); + + // Set a timer to automatically delete the cache entry after the TTL + const timerId = setTimeout(() => { + crawlCache.delete(key); + cacheTimers.delete(key); + }, ttlMs); + + // Store the timer handle so we can clear it if the entry is updated + cacheTimers.set(key, timerId); +}; + +/** + * Clears the entire cache and all active timers. + * Primarily used for testing. + */ +export const clear = (): void => { + for (const timerId of cacheTimers.values()) { + clearTimeout(timerId); + } + crawlCache.clear(); + cacheTimers.clear(); +}; |
