@exromany/lido-csm-sdk
Version:
[](https://github.com/lidofinance/lido-csm-sdk/blob/main/LICENSE.txt) [](h
103 lines • 4.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Cache = void 0;
const index_js_1 = require("../utils/index.js");
const utils_js_1 = require("./utils.js");
const serializeArgs = (args) => args
.map((arg) => JSON.stringify(arg, (_key, value) => {
return (0, index_js_1.isBigint)(value) ? value.toString() : value;
}))
.join(':');
const getDecoratorArgsString = function (args) {
if (!args)
return '';
const argsStringArr = args.map((arg) => {
const field = arg
.split('.')
.reduce((a, b) => a[b], this);
return arg && typeof field === 'function' ? field.call(this) : field;
});
return serializeArgs(argsStringArr);
};
const Cache = function (timeMs = 0, cacheArgs) {
return function CacheDecorator(target, context) {
const methodName = String(context.name);
if (context.kind === 'getter') {
const replacementGetter = function () {
const cache = this.cache;
const decoratorArgsKey = getDecoratorArgsString.call(this, cacheArgs);
const cacheKey = `${methodName}:${decoratorArgsKey}:`;
if (cache.has(cacheKey)) {
const cachedEntry = cache.get(cacheKey);
const currentTime = Date.now();
if (cachedEntry && (cachedEntry.isPromise || currentTime - cachedEntry.timestamp <= timeMs)) {
utils_js_1.callConsoleMessage.call(this, 'Cache:', `Using cache for getter '${methodName}'.`);
return cachedEntry.data;
}
else {
utils_js_1.callConsoleMessage.call(this, 'Cache:', `Cache for getter '${methodName}' has expired.`);
cache.delete(cacheKey);
}
}
utils_js_1.callConsoleMessage.call(this, 'Cache:', `Cache for getter '${methodName}' set.`);
const result = target.call(this);
if (result instanceof Promise) {
cache.set(cacheKey, { data: result, timestamp: Date.now(), isPromise: true });
return result
.then((resolvedResult) => {
cache.set(cacheKey, { data: resolvedResult, timestamp: Date.now(), isPromise: false });
return resolvedResult;
})
.catch((error) => {
cache.delete(cacheKey);
throw error;
});
}
else {
cache.set(cacheKey, { data: result, timestamp: Date.now(), isPromise: false });
}
return result;
};
return replacementGetter;
}
const replacementMethod = function (...args) {
const cache = this.cache;
const decoratorArgsKey = getDecoratorArgsString.call(this, cacheArgs);
const argsKey = serializeArgs(args);
const cacheKey = `${methodName}:${decoratorArgsKey}:${argsKey}`;
if (cache.has(cacheKey)) {
const cachedEntry = cache.get(cacheKey);
const currentTime = Date.now();
if (cachedEntry && (cachedEntry.isPromise || currentTime - cachedEntry.timestamp <= timeMs)) {
utils_js_1.callConsoleMessage.call(this, 'Cache:', `Using cache for method '${methodName}'.`);
return cachedEntry.data;
}
else {
utils_js_1.callConsoleMessage.call(this, 'Cache:', `Cache for method '${methodName}' has expired.`);
cache.delete(cacheKey);
}
}
utils_js_1.callConsoleMessage.call(this, 'Cache:', `Cache for method '${methodName}' set.`);
const result = target.call(this, ...args);
if (result instanceof Promise) {
cache.set(cacheKey, { data: result, timestamp: Date.now(), isPromise: true });
return result
.then((resolvedResult) => {
cache.set(cacheKey, { data: resolvedResult, timestamp: Date.now(), isPromise: false });
return resolvedResult;
})
.catch((error) => {
cache.delete(cacheKey);
throw error;
});
}
else {
cache.set(cacheKey, { data: result, timestamp: Date.now(), isPromise: false });
}
return result;
};
return replacementMethod;
};
};
exports.Cache = Cache;
//# sourceMappingURL=cache.js.map