* Retrieves cached search results for a given query, or provides a base set * of files to search from. * @param query The search query pattern. * @returns An object containing the files to search and a boolean indicating * if the result is an exact cache hit.
(
query: string,
)
| 26 | * if the result is an exact cache hit. |
| 27 | */ |
| 28 | async get( |
| 29 | query: string, |
| 30 | ): Promise<{ files: string[]; isExactMatch: boolean }> { |
| 31 | const isCacheHit = this.cache.has(query); |
| 32 | |
| 33 | if (isCacheHit) { |
| 34 | this.hits++; |
| 35 | return { files: this.cache.get(query)!, isExactMatch: true }; |
| 36 | } |
| 37 | |
| 38 | this.misses++; |
| 39 | |
| 40 | // This is the core optimization of the memory cache. |
| 41 | // If a user first searches for "foo", and then for "foobar", |
| 42 | // we don't need to search through all files again. We can start |
| 43 | // from the results of the "foo" search. |
| 44 | // This finds the most specific, already-cached query that is a prefix |
| 45 | // of the current query. |
| 46 | let bestBaseQuery = ''; |
| 47 | for (const key of this.cache?.keys?.() ?? []) { |
| 48 | if (query.startsWith(key) && key.length > bestBaseQuery.length) { |
| 49 | bestBaseQuery = key; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | const filesToSearch = bestBaseQuery |
| 54 | ? this.cache.get(bestBaseQuery)! |
| 55 | : this.allFiles; |
| 56 | |
| 57 | return { files: filesToSearch, isExactMatch: false }; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Stores search results in the cache. |
no outgoing calls
no test coverage detected