(expiryDurationMs: number)
| 13 | type PromiseFunctionWithAnyArgs = (...any: any) => Promise<any>; |
| 14 | const cacheStoreForMethods = getGlobalCacheStore(); |
| 15 | export function cache(expiryDurationMs: number) { |
| 16 | return function ( |
| 17 | target: Object, |
| 18 | propertyName: string, |
| 19 | descriptor: TypedPropertyDescriptor<PromiseFunctionWithAnyArgs> |
| 20 | ) { |
| 21 | const originalMethod = descriptor.value!; |
| 22 | const className = 'constructor' in target && target.constructor.name ? target.constructor.name : ''; |
| 23 | const keyPrefix = `Cache_Method_Output_${className}.${propertyName}`; |
| 24 | descriptor.value = async function (...args: any) { |
| 25 | if (isTestExecution()) { |
| 26 | return originalMethod.apply(this, args) as Promise<any>; |
| 27 | } |
| 28 | const key = getCacheKeyFromFunctionArgs(keyPrefix, args); |
| 29 | const cachedItem = cacheStoreForMethods.get(key); |
| 30 | if (cachedItem && !cachedItem.expired) { |
| 31 | logger.debug(`Cached data exists ${key}`); |
| 32 | return Promise.resolve(cachedItem.data); |
| 33 | } |
| 34 | const promise = originalMethod.apply(this, args) as Promise<any>; |
| 35 | promise |
| 36 | .then((result) => cacheStoreForMethods.set(key, new DataWithExpiry(expiryDurationMs, result))) |
| 37 | .catch(noop); |
| 38 | return promise; |
| 39 | }; |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Swallows exceptions thrown by a function. Function must return either a void or a promise that resolves to a void. |
no test coverage detected