UNPKG

@mastra/core

Version:
77 lines (76 loc) 2.11 kB
import { t as MastraBase } from "./base-BeUQ6mLP.js"; import { TTLCache } from "@isaacs/ttlcache"; //#region src/cache/base.ts var MastraServerCache = class extends 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 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 export { MastraServerCache as n, InMemoryServerCache as t }; //# sourceMappingURL=inmemory-DHuA6Z20.js.map