@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
89 lines (87 loc) • 2.29 kB
JavaScript
import { MastraBase } from './chunk-WENZPAHS.js';
import { TTLCache } from '@isaacs/ttlcache';
// src/cache/base.ts
var MastraServerCache = class extends MastraBase {
constructor({ name }) {
super({
component: "SERVER_CACHE",
name
});
}
};
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;
}
};
export { InMemoryServerCache, MastraServerCache };
//# sourceMappingURL=chunk-JZ7Q75IW.js.map
//# sourceMappingURL=chunk-JZ7Q75IW.js.map