execution-engine
Version:
A TypeScript library for tracing and visualizing code execution workflows.
33 lines (32 loc) • 1.76 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.cache = cache;
const cache_1 = require("./cache");
const functionMetadata_1 = require("../common/utils/functionMetadata");
/**
* Caches function results to avoid redundant expensive computations
* If the result is already cached, it returns the cached value; otherwise, it executes the function and stores the result.
*
* @param options - Caching configuration specifying TTL, cache key generation, cache management, and optional logging.
* @returns A method decorator that applies caching logic.
*
* @remarks
* - Cache behavior can be customized via `cacheKey`, `ttl`, and `cacheHandler`.
* - Errors are thrown immediately and **not cached** to allow retries.
*/
function cache(options) {
return function (target, propertyKey, descriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (...args) {
const thisMethodMetadata = (0, functionMetadata_1.extractClassMethodMetadata)(target.constructor.name, propertyKey, originalMethod);
return cache_1.executeCache.bind(this)(originalMethod.bind(this), args, {
functionId: thisMethodMetadata.methodSignature,
...options,
cacheKey: functionMetadata_1.attachFunctionMetadata.bind(this)(options.cacheKey, thisMethodMetadata),
bypass: functionMetadata_1.attachFunctionMetadata.bind(this)(options.bypass, thisMethodMetadata),
ttl: functionMetadata_1.attachFunctionMetadata.bind(this)(options.ttl, thisMethodMetadata),
onCacheEvent: functionMetadata_1.attachFunctionMetadata.bind(this)(options.onCacheEvent, thisMethodMetadata)
});
};
};
}
;