@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
92 lines (89 loc) • 2.39 kB
JavaScript
;
var chunkFCQNDFEW_cjs = require('./chunk-FCQNDFEW.cjs');
var ttlcache = require('@isaacs/ttlcache');
// src/cache/base.ts
var MastraServerCache = class extends chunkFCQNDFEW_cjs.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.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;
}
};
exports.InMemoryServerCache = InMemoryServerCache;
exports.MastraServerCache = MastraServerCache;
//# sourceMappingURL=chunk-BRVYVULM.cjs.map
//# sourceMappingURL=chunk-BRVYVULM.cjs.map