From 89e75b2956c716bf10aafdc410de781e306db8f9 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Mon, 2 Oct 2023 15:50:54 -0700 Subject: [PATCH] Fixes #11 - Cache logic and FetchOptions improved --- sonar-project.properties | 2 +- src/lib/cache.ts | 54 ++++++++++++++++++++-------------------- src/skimming.ts | 8 +++--- test/cache_test.ts | 44 +++++++++++++++++++++++++++++--- 4 files changed, 73 insertions(+), 35 deletions(-) diff --git a/sonar-project.properties b/sonar-project.properties index 9554fae..ccecaa0 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,7 +1,7 @@ sonar.projectKey=petruki_skimming sonar.projectName=skimming sonar.organization=petruki -sonar.projectVersion=1.0.10 +sonar.projectVersion=1.0.11 sonar.javascript.lcov.reportPaths=coverage/report.lcov diff --git a/src/lib/cache.ts b/src/lib/cache.ts index 98c4434..e9ed001 100644 --- a/src/lib/cache.ts +++ b/src/lib/cache.ts @@ -25,9 +25,11 @@ export default class CacheHandler { const result = this.fetchCache( query, - ignoreCase, - previewLength, - trimContent, + { + ignoreCase, + previewLength, + trimContent, + }, ); if (result.length) { @@ -59,9 +61,7 @@ export default class CacheHandler { store( query: string, output: Output, - previewLength: number = DEFAULT_PREVIEW_LENGTH, - ignoreCase: boolean = DEFAULT_IGNORE_CASE, - trimContent: boolean = DEFAULT_TRIM, + fetchOptions?: FetchOptions, ): void { const cachedData = this.cache.filter((cache) => cache.query === query); @@ -69,16 +69,16 @@ export default class CacheHandler { cachedData[0].output = cachedData[0].output.filter((cachedOutput) => cachedOutput.file != output.file); cachedData[0].output.push(output); cachedData[0].exp = Date.now() + (1000 * this.cacheExpireDuration); - cachedData[0].previewLength = previewLength; - cachedData[0].trimContent = trimContent; - cachedData[0].ignoreCase = ignoreCase; + cachedData[0].previewLength = fetchOptions?.previewLength ?? DEFAULT_PREVIEW_LENGTH; + cachedData[0].trimContent = fetchOptions?.trimContent ?? DEFAULT_TRIM; + cachedData[0].ignoreCase = fetchOptions?.ignoreCase ?? DEFAULT_IGNORE_CASE; } else { const toBeCached = { query, output: [output], - previewLength, - ignoreCase, - trimContent, + previewLength: fetchOptions?.previewLength ?? DEFAULT_PREVIEW_LENGTH, + ignoreCase: fetchOptions?.ignoreCase ?? DEFAULT_IGNORE_CASE, + trimContent: fetchOptions?.trimContent ?? DEFAULT_TRIM, exp: Date.now() + (1000 * this.cacheExpireDuration), }; @@ -97,29 +97,29 @@ export default class CacheHandler { * Fetches cache based on query input and options provided * * @param query - * @param ignoreCase - * @param previewLength - * @param trimContent + * @param fetchOptions * @returns */ private fetchCache( query: string, - ignoreCase: boolean | undefined, - previewLength: number | undefined, - trimContent: boolean | undefined, - ) { + fetchOptions: FetchOptions, + ): Cache[] { + const { ignoreCase, previewLength, trimContent } = fetchOptions; + return this.cache.filter((storedData) => { - if (storedData.query.length <= query.length) { + if (storedData.query.length <= query.length && storedData.exp > Date.now()) { + const hasOptionsChanged = this.checkOptions(storedData, { + previewLength, + ignoreCase, + trimContent, + }); + if (ignoreCase) { - return query.toLowerCase().startsWith(storedData.query.toLowerCase()); + return query.toLowerCase().startsWith(storedData.query.toLowerCase()) && !hasOptionsChanged; } - if (query.startsWith(storedData.query) && storedData.exp > Date.now()) { - return !this.checkOptions(storedData, { - previewLength, - ignoreCase, - trimContent, - }); + if (query.startsWith(storedData.query)) { + return !hasOptionsChanged; } } return false; diff --git a/src/skimming.ts b/src/skimming.ts index 1a54164..3a7d60a 100644 --- a/src/skimming.ts +++ b/src/skimming.ts @@ -75,9 +75,11 @@ export class Skimming { this.cacheHandler.store( query, output, - previewLength, - ignoreCase, - trimContent, + { + previewLength, + ignoreCase, + trimContent, + }, ); } } diff --git a/test/cache_test.ts b/test/cache_test.ts index 927c623..57989e4 100644 --- a/test/cache_test.ts +++ b/test/cache_test.ts @@ -120,6 +120,9 @@ test({ found: 1, cache: true, }, + { + ignoreCase: true, + }, ); // test @@ -129,6 +132,35 @@ test({ }, }); +test({ + name: 'CACHE - Should fetch from remote when ignore case enabled', + async fn(): Promise { + // given + const cacheHandler = new CacheHandler({ size: 2, expireDuration: 1 }); + + cacheHandler.store( + 'My Search', + { + file: 'filename.md', + segment: ['My Search begins somewhere here'], + found: 1, + cache: true, + }, + { + ignoreCase: true, + }, + ); + + // test + let output = cacheHandler.fetch('my search', { ignoreCase: true }); + assertEquals(output.length, 1); + await sleep(1500); + + output = cacheHandler.fetch('my search', { ignoreCase: true }); + assertEquals(output.length, 0); + }, +}); + test({ name: 'CACHE - Should fetch from source once FetchOptions (previewLength) has changed', fn(): void { @@ -144,7 +176,9 @@ test({ found: 1, cache: true, }, - previewLength, + { + previewLength, + }, ); // test @@ -168,9 +202,11 @@ test({ found: 1, cache: true, }, - DEFAULT_PREVIEW_LENGTH, - DEFAULT_IGNORE_CASE, - trimContent, + { + trimContent, + previewLength: DEFAULT_PREVIEW_LENGTH, + ignoreCase: DEFAULT_IGNORE_CASE, + }, ); // test