UNPKG

bandcamp-fetch

Version:
105 lines 5.05 kB
"use strict"; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _Cache_instances, _Cache_ttl, _Cache_maxEntries, _Cache_cache, _Cache_getCacheKey; Object.defineProperty(exports, "__esModule", { value: true }); exports.CacheDataType = void 0; const node_cache_1 = __importDefault(require("node-cache")); var CacheDataType; (function (CacheDataType) { CacheDataType["Page"] = "Page"; CacheDataType["Constants"] = "Constants"; })(CacheDataType || (exports.CacheDataType = CacheDataType = {})); class Cache { constructor(ttl, maxEntries) { _Cache_instances.add(this); _Cache_ttl.set(this, void 0); _Cache_maxEntries.set(this, void 0); _Cache_cache.set(this, void 0); __classPrivateFieldSet(this, _Cache_ttl, ttl, "f"); __classPrivateFieldSet(this, _Cache_maxEntries, maxEntries, "f"); __classPrivateFieldSet(this, _Cache_cache, new node_cache_1.default({ checkperiod: 600 }), "f"); } setTTL(type, ttl) { if (__classPrivateFieldGet(this, _Cache_ttl, "f")[type] !== ttl) { this.getKeys(type).forEach((key) => { __classPrivateFieldGet(this, _Cache_cache, "f").ttl(key, ttl); }); __classPrivateFieldGet(this, _Cache_ttl, "f")[type] = ttl; } } setMaxEntries(type, maxEntries) { this.reduceEntries(type, maxEntries); __classPrivateFieldGet(this, _Cache_maxEntries, "f")[type] = maxEntries; } getMaxEntries(type) { return __classPrivateFieldGet(this, _Cache_maxEntries, "f")[type] !== undefined ? __classPrivateFieldGet(this, _Cache_maxEntries, "f")[type] : -1; } // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters get(type, key) { return __classPrivateFieldGet(this, _Cache_cache, "f").get(__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_getCacheKey).call(this, type, key)); } // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters put(type, key, value) { const maxEntries = this.getMaxEntries(type); if (maxEntries === 0) { return false; } else if (maxEntries > 0) { this.reduceEntries(type, maxEntries - 1); } return __classPrivateFieldGet(this, _Cache_cache, "f").set(__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_getCacheKey).call(this, type, key), value, __classPrivateFieldGet(this, _Cache_ttl, "f")[type]); } reduceEntries(type, reduceTo) { if (reduceTo === undefined) { reduceTo = this.getMaxEntries(type); } const keys = this.getKeys(type); if (keys.length > reduceTo) { for (let i = 0; i < keys.length - reduceTo; i++) { __classPrivateFieldGet(this, _Cache_cache, "f").del(keys[i]); } } } getKeys(type) { return __classPrivateFieldGet(this, _Cache_cache, "f").keys().filter((key) => key.startsWith(`${type}.`)); } clear(type) { if (!type) { __classPrivateFieldGet(this, _Cache_cache, "f").flushAll(); } else { this.getKeys(type).forEach((key) => { __classPrivateFieldGet(this, _Cache_cache, "f").del(key); }); } } async getOrSet(type, key, promiseCallback) { const cachedValue = this.get(type, key); if (cachedValue !== undefined) { return cachedValue; } const value = await promiseCallback(); this.put(type, key, value); return value; } } _Cache_ttl = new WeakMap(), _Cache_maxEntries = new WeakMap(), _Cache_cache = new WeakMap(), _Cache_instances = new WeakSet(), _Cache_getCacheKey = function _Cache_getCacheKey(type, key) { return `${type}.${key}`; }; exports.default = Cache; //# sourceMappingURL=Cache.js.map