Skip to content

Commit

Permalink
remove cache decorator error handling entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMagee committed Jun 17, 2021
1 parent 3851f96 commit b45926a
Showing 1 changed file with 26 additions and 31 deletions.
57 changes: 26 additions & 31 deletions lib/util/cache/package/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,37 +90,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.trace({ 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;
});
}

0 comments on commit b45926a

Please sign in to comment.