Skip to content

Commit

Permalink
Add option to disable caching (#1297)
Browse files Browse the repository at this point in the history
This can be done using the `noCache` context key or `--noCache` CLI flag.

Closes #618
  • Loading branch information
simonvbrae committed Feb 8, 2024
1 parent 1fe8472 commit 53c7686
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/actor-init-query/lib/ActorInitQueryBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ export interface IActorInitQueryBaseArgs<QueryContext extends IQueryContextCommo
* "extensionFunctionCreator": "@comunica/actor-init-query:extensionFunctionCreator",
* "functionArgumentsCache": "@comunica/actor-init-query:functionArgumentsCache",
* "explain": "@comunica/actor-init-query:explain",
* "unionDefaultGraph": "@comunica/bus-query-operation:unionDefaultGraph"
* "unionDefaultGraph": "@comunica/bus-query-operation:unionDefaultGraph",
* "noCache": "@comunica/actor-init-query:noCache"
* }}
*/
contextKeyShortcuts: Record<string, string> | Partial<Record<keyof QueryContext, string>>;
Expand Down
10 changes: 10 additions & 0 deletions packages/actor-init-query/lib/QueryEngineBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ implements IQueryEngine<QueryContext, QueryStringContextInner, QueryAlgebraConte
// Pre-processing the context
actionContext = (await this.actorInitQuery.mediatorContextPreprocess.mediate({ context: actionContext })).context;

// Invalidate caches if cache argument is set to false
if (actionContext.get(KeysInitQuery.noCache)) {
await this.invalidateHttpCache();
}

// Determine explain mode
const explainMode: QueryExplainMode = actionContext.get(KeysInitQuery.explain)!;

Expand Down Expand Up @@ -249,6 +254,11 @@ implements IQueryEngine<QueryContext, QueryStringContextInner, QueryAlgebraConte
};
}

// Invalidate caches if cache argument is set to false
if (actionContext.get(KeysInitQuery.noCache)) {
await this.invalidateHttpCache();
}

return finalOutput;
}

Expand Down
9 changes: 9 additions & 0 deletions packages/actor-init-query/lib/cli/CliArgsHandlerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ export class CliArgsHandlerBase implements ICliArgsHandler {
type: 'boolean',
describe: 'If the default graph should also contain the union of all named graphs',
},
noCache: {
type: 'boolean',
describe: 'If the cache should be disabled',
},
})
.exitProcess(false)
.fail(false)
Expand Down Expand Up @@ -251,6 +255,11 @@ export class CliArgsHandlerBase implements ICliArgsHandler {
if (args.unionDefaultGraph) {
context[KeysQueryOperation.unionDefaultGraph.name] = true;
}

// Define if cache should be disabled
if (args.noCache) {
context[KeysInitQuery.noCache.name] = true;
}
}
}
/* eslint-enable import/no-nodejs-modules */
Expand Down
16 changes: 16 additions & 0 deletions packages/actor-init-query/test/ActorInitQuery-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,22 @@ LIMIT 100
});
});

it('handles the --noCache flag', async() => {
const stdout = await stringifyStream(<any> (await actor.run({
argv: [ sourceHypermedia, '-q', queryString, '--noCache' ],
env: {},
stdin: <Readable><any> new PassThrough(),
context,
})).stdout);
expect(stdout).toContain(`{"a":"triple"}`);
expect(spyQueryOrExplain).toHaveBeenCalledWith(queryString, {
[KeysInitQuery.queryFormat.name]: { language: 'sparql', version: '1.1' },
[KeysRdfResolveQuadPattern.sources.name]: [{ value: sourceHypermedia }],
[KeysCore.log.name]: expect.any(LoggerPretty),
[KeysInitQuery.noCache.name]: true,
});
});

it('handles the destination --to option', async() => {
const stdout = await stringifyStream(<any> (await actor.run({
argv: [ sourceHypermedia, '-q', queryString, '--to', 'http://target.com/' ],
Expand Down
4 changes: 4 additions & 0 deletions packages/context-entries/lib/Keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ export const KeysInitQuery = {
* A JSON-LD context
*/
jsonLdContext: new ActionContextKey<any>('@context'),
/**
* A boolean value denoting whether caching is disabled or not.
*/
noCache: new ActionContextKey<boolean>('@comunica/actor-init-query:noCache'),
};

export const KeysQueryOperation = {
Expand Down

0 comments on commit 53c7686

Please sign in to comment.