Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #11 - Cache logic and FetchOptions improved #12

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -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

Expand Down
54 changes: 27 additions & 27 deletions src/lib/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ export default class CacheHandler {

const result = this.fetchCache(
query,
ignoreCase,
previewLength,
trimContent,
{
ignoreCase,
previewLength,
trimContent,
},
);

if (result.length) {
Expand Down Expand Up @@ -59,26 +61,24 @@ 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);

if (cachedData.length) {
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),
};

Expand All @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions src/skimming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ export class Skimming {
this.cacheHandler.store(
query,
output,
previewLength,
ignoreCase,
trimContent,
{
previewLength,
ignoreCase,
trimContent,
},
);
}
}
Expand Down
44 changes: 40 additions & 4 deletions test/cache_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ test({
found: 1,
cache: true,
},
{
ignoreCase: true,
},
);

// test
Expand All @@ -129,6 +132,35 @@ test({
},
});

test({
name: 'CACHE - Should fetch from remote when ignore case enabled',
async fn(): Promise<void> {
// 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 {
Expand All @@ -144,7 +176,9 @@ test({
found: 1,
cache: true,
},
previewLength,
{
previewLength,
},
);

// test
Expand All @@ -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
Expand Down