typescript-cacheable
Version:
An in-memory caching (memoization) decorator for Typescript
51 lines • 1.81 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacheRegistry = exports.cacheProperty = void 0;
const ExpiringMap_1 = require("../ExpiringMap");
exports.cacheProperty = '__typescript_cacheable_cache__';
class CacheRegistry {
getOrInit(target, methodName, options) {
this.processOptions(target, methodName, options);
const cache = this.getOrInitCache(target, methodName);
if (!cache.has(methodName)) {
cache.set(methodName, new ExpiringMap_1.ExpiringMap());
}
return cache.get(methodName);
}
getOrInitCache(target, methodName) {
const cacheHost = this.getCacheHost(target);
if (!Object.prototype.hasOwnProperty.call(cacheHost, exports.cacheProperty)) {
this.initCache(cacheHost);
}
return cacheHost[exports.cacheProperty];
}
initCache(cacheHost) {
if (cacheHost === undefined) {
throw 'cacheHost must be defined';
}
Object.defineProperty(cacheHost, exports.cacheProperty, {
configurable: true,
enumerable: false,
writable: false,
value: this.newCache(),
});
}
initialiseStore(storeFunction) { }
getCacheHost(target, methodName) {
return target;
}
getMethodMap(cache, target, isLocal) {
if (isLocal) {
const localCache = cache;
const className = target.constructor.name;
if (!localCache.has(className)) {
localCache.set(className, new Map());
}
return localCache.get(className);
}
return cache;
}
processOptions(target, methodName, _options) { }
}
exports.CacheRegistry = CacheRegistry;
//# sourceMappingURL=CacheRegistry.js.map
;