cache on strings instead

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2021-05-26 14:59:17 +02:00
parent 704875e26d
commit 6fd3350a1d
+6 -4
View File
@@ -20,8 +20,9 @@ import entityEnvelopeSchema from '../schema/EntityEnvelope.schema.json';
import entityMetaSchema from '../schema/EntityMeta.schema.json';
import commonSchema from '../schema/shared/common.schema.json';
// A local cache of compiled schemas, to avoid duplicate work
const compiledSchemaCache = new Map<unknown, ValidateFunction<unknown>>();
// A local cache of compiled schemas, to avoid duplicate work.
// The keys are JSON stringified versions of the schema
const compiledSchemaCache = new Map<string, ValidateFunction<unknown>>();
// The core schemas that others can depend on
const refDependencyCandidates = [
@@ -57,9 +58,10 @@ export function compileAjvSchema(
options: { disableCache?: boolean } = {},
): ValidateFunction<unknown> {
const disableCache = options?.disableCache ?? false;
const cacheKey = disableCache ? '' : JSON.stringify(schema);
if (!disableCache) {
const cached = compiledSchemaCache.get(schema);
const cached = compiledSchemaCache.get(cacheKey);
if (cached) {
return cached;
}
@@ -77,7 +79,7 @@ export function compileAjvSchema(
const compiled = ajv.compile(schema);
if (!disableCache) {
compiledSchemaCache.set(schema, compiled);
compiledSchemaCache.set(cacheKey, compiled);
}
return compiled;