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

fix(cache): log cache errors as trace #10449

Merged
merged 2 commits into from
Jun 17, 2021
Merged
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
58 changes: 26 additions & 32 deletions lib/util/cache/package/decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import * as packageCache from '.';

type Handler<T> = (parameters: DecoratorParameters<T>) => Promise<unknown>;
Expand Down Expand Up @@ -90,37 +89,32 @@ export function cache<T>({
ttlMinutes = 30,
}: CacheParameters): Decorator<T> {
return decorate(async ({ args, instance, callback }) => {
try {
let finalNamespace: string;
if (is.string(namespace)) {
finalNamespace = namespace;
} else if (is.function_(namespace)) {
finalNamespace = namespace.apply(instance, args);
}

let finalKey: string;
if (is.string(key)) {
finalKey = key;
} else if (is.function_(key)) {
finalKey = key.apply(instance, args);
}

const cachedResult = await packageCache.get<unknown>(
finalNamespace,
finalKey
);

if (cachedResult !== undefined) {
return cachedResult;
}

const result = await callback();

await packageCache.set(finalNamespace, finalKey, result, ttlMinutes);
return result;
} catch (err) /* istanbul ignore next */ {
logger.error({ err }, 'cache decorate error');
throw err;
let finalNamespace: string;
if (is.string(namespace)) {
finalNamespace = namespace;
} else if (is.function_(namespace)) {
finalNamespace = namespace.apply(instance, args);
}

let finalKey: string;
if (is.string(key)) {
finalKey = key;
} else if (is.function_(key)) {
finalKey = key.apply(instance, args);
}

const cachedResult = await packageCache.get<unknown>(
finalNamespace,
finalKey
);

if (cachedResult !== undefined) {
return cachedResult;
}

const result = await callback();

await packageCache.set(finalNamespace, finalKey, result, ttlMinutes);
return result;
});
}