UNPKG

@mastra/core

Version:
88 lines (87 loc) 2.34 kB
const require_base = require("./base-B6soWsYg.cjs"); let _isaacs_ttlcache = require("@isaacs/ttlcache"); //#region src/cache/base.ts var MastraServerCache = class extends require_base.MastraBase { constructor({ name }) { super({ component: "SERVER_CACHE", name }); } }; //#endregion //#region src/cache/inmemory.ts var InMemoryServerCache = class extends MastraServerCache { cache; ttlMs; constructor(options = {}) { super({ name: "InMemoryServerCache" }); this.ttlMs = options.ttlMs ?? 1e3 * 60 * 5; const ttl = this.ttlMs > 0 ? this.ttlMs : Infinity; this.cache = new _isaacs_ttlcache.TTLCache({ max: options.maxSize ?? 1e3, ttl }); } async get(key) { return this.cache.get(key); } async set(key, value, ttlMs) { if (ttlMs === void 0) { this.cache.set(key, value); return; } this.cache.set(key, value, { ttl: ttlMs > 0 ? ttlMs : Infinity }); } async listLength(key) { const value = this.cache.get(key); if (value === void 0) return 0; if (!Array.isArray(value)) throw new Error(`${key} exists but is not an array`); return value.length; } async listPush(key, value) { const existing = this.cache.get(key); if (Array.isArray(existing)) { existing.push(value); if (this.ttlMs > 0) this.cache.set(key, existing, { ttl: this.ttlMs }); } else if (existing !== void 0) throw new Error(`${key} exists but is not an array`); else this.cache.set(key, [value]); } async listFromTo(key, from, to = -1) { const list = this.cache.get(key); if (Array.isArray(list)) { const endIndex = to === -1 ? void 0 : to + 1; return list.slice(from, endIndex); } return []; } async delete(key) { this.cache.delete(key); } async clear() { this.cache.clear(); } async increment(key) { const value = this.cache.get(key); let counter; if (value === void 0) counter = 1; else if (typeof value === "number") counter = value + 1; else throw new Error(`${key} exists but is not a number`); this.cache.set(key, counter); return counter; } }; //#endregion Object.defineProperty(exports, "InMemoryServerCache", { enumerable: true, get: function() { return InMemoryServerCache; } }); Object.defineProperty(exports, "MastraServerCache", { enumerable: true, get: function() { return MastraServerCache; } }); //# sourceMappingURL=inmemory-CVHnncRp.cjs.map