Skip to content

Commit

Permalink
Moved cacheOptions to context, added skipCache to fetchOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
petruki committed May 25, 2024
1 parent 3543fc0 commit 38e407d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
export type Context = {
url: string;
files: string[];
options?: CacheOptions;
cacheOptions?: CacheOptions;
};

/**
Expand Down Expand Up @@ -57,4 +57,5 @@ export type FetchOptions = {
trimContent?: boolean;
previewLength?: number;
regex?: boolean;
skipCache?: boolean;
};
14 changes: 9 additions & 5 deletions src/skimming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class Skimming {
validateContext(context);
this.context = context;

if (context.options) {
this.cacheHandler = new CacheHandler(context.options);
if (context.cacheOptions) {
this.cacheHandler = new CacheHandler(context.cacheOptions);
this.useCache = true;
}
}
Expand All @@ -55,10 +55,10 @@ export class Skimming {
validateQuery(query);
validateContext(this.context);

const { ignoreCase, previewLength, trimContent } = options;
const { ignoreCase, previewLength, trimContent, skipCache } = options;
let results: Output[] = [];

if (this.useCache) {
if (this.canUseCache(skipCache)) {
results = this.cacheHandler.fetch(query, options);
}

Expand Down Expand Up @@ -86,7 +86,7 @@ export class Skimming {
};
results.push(output);

if (this.useCache) {
if (this.canUseCache(skipCache)) {
this.cacheHandler.store(
query,
output,
Expand Down Expand Up @@ -173,4 +173,8 @@ export class Skimming {
result.body?.cancel();
throw new NotContentFound(url, doc);
}

private canUseCache(skipCache: boolean = false): boolean {
return this.useCache && !skipCache;
}
}
39 changes: 34 additions & 5 deletions test/mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ test({
const context: Context = {
url: 'https://raw.githubusercontent.com/petruki/skimming/master/test/fixtures/',
files,
options: { size: 10, expireDuration: 10 },
cacheOptions: { size: 10, expireDuration: 10 },
};

const skimmer = Skimming.create(context);
Expand All @@ -206,6 +206,35 @@ test({
},
});

test({
name: 'MOD - Should NOT return value from the cache - when skip cache is enabled',
async fn(): Promise<void> {
// given
const query = 'Skimming';
const files = ['README.md'];
const context: Context = {
url: 'https://raw.githubusercontent.com/petruki/skimming/master/test/fixtures/',
files,
cacheOptions: { size: 10, expireDuration: 10 },
};

const skimmer = Skimming.create(context);

// test
let output = await skimmer.skim(query);
assertEquals(output.length, 1);
output.forEach((data) => {
assertEquals(data.cache, false);
});

output = await skimmer.skim(query, { skipCache: true });
assertEquals(output.length, 1);
output.forEach((data) => {
assertEquals(data.cache, false);
});
},
});

test({
name: 'MOD - Should return value from the cache with new preview length',
async fn(): Promise<void> {
Expand All @@ -215,7 +244,7 @@ test({
const context: Context = {
url: 'https://raw.githubusercontent.com/petruki/skimming/master/test/fixtures/',
files,
options: { size: 10, expireDuration: 10 },
cacheOptions: { size: 10, expireDuration: 10 },
};

const skimmer = Skimming.create(context);
Expand Down Expand Up @@ -249,7 +278,7 @@ test({
const context: Context = {
url: 'https://raw.githubusercontent.com/petruki/skimming/master/test/fixtures/',
files,
options: { size: 10, expireDuration: 10 },
cacheOptions: { size: 10, expireDuration: 10 },
};

const skimmer = Skimming.create(context);
Expand Down Expand Up @@ -283,7 +312,7 @@ test({
const context: Context = {
url: 'https://raw.githubusercontent.com/petruki/skimming/master/test/fixtures/',
files,
options: { size: 10, expireDuration: 10 },
cacheOptions: { size: 10, expireDuration: 10 },
};

const skimmer = Skimming.create(context);
Expand Down Expand Up @@ -323,7 +352,7 @@ test({
const context: Context = {
url: 'https://raw.githubusercontent.com/petruki/skimming/master/test/fixtures/',
files,
options: { size: 10, expireDuration: 10 },
cacheOptions: { size: 10, expireDuration: 10 },
};

const skimmer = Skimming.create(context);
Expand Down

0 comments on commit 38e407d

Please sign in to comment.