MCPcopy Create free account
hub / github.com/deepnote/vscode-deepnote / cache

Function cache

src/platform/common/utils/decorators.ts:15–41  ·  view source on GitHub ↗
(expiryDurationMs: number)

Source from the content-addressed store, hash-verified

13type PromiseFunctionWithAnyArgs = (...any: any) => Promise<any>;
14const cacheStoreForMethods = getGlobalCacheStore();
15export 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.

Callers 2

PoetryClass · 0.90
TestClassClass · 0.90

Calls 6

isTestExecutionFunction · 0.90
getMethod · 0.65
debugMethod · 0.65
resolveMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected