@mastra/core
Version:
133 lines (132 loc) • 4.41 kB
JavaScript
const require_base = require("./base-BIzR1D3_.cjs");
let _isaacs_ttlcache = require("@isaacs/ttlcache");
// src/cache/base.ts
var MastraServerCache = class extends require_base.MastraBase {
constructor({ name }) {
super({
component: "SERVER_CACHE",
name
});
}
/**
* Atomically allocate the next index from `counterKey` and append `value`
* (with `index` set to the 0-based allocated index) to the list at `listKey`.
* Returns the assigned index.
*
* The list and its counter must expire together: a counter that outlives its
* list restarts at 0 while subscribers still expect the old numbering, and a
* counter that expires first would start reusing indices already in the list.
* Every append refreshes both keys to the same deadline.
*
* @param ttlMs - Minimum lifetime for both keys, in milliseconds. A cache
* whose own TTL is longer, or that never expires, keeps that instead. When
* omitted, the cache's default TTL applies.
*
* The default implementation composes `increment()` + `listPush()` (two
* round-trips) and is therefore NOT atomic, and ignores `ttlMs`. Overrides
* should perform both steps in one server-side operation.
*/
async listPushIndexed(listKey, counterKey, value, _ttlMs) {
const index = await this.increment(counterKey) - 1;
await this.listPush(listKey, {
...value,
index
});
return index;
}
};
// src/cache/inmemory.ts
var InMemoryServerCache = class extends MastraServerCache {
cache;
ttlMs;
constructor(options = {}) {
super({ name: "InMemoryServerCache" });
this.ttlMs = options.ttlMs ?? 3e5;
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;
}
/**
* Allocation and append happen in one synchronous critical section. JS runs
* one task at a time and nothing here awaits, so no other publish can observe
* the counter after it is bumped but before the entry is appended.
*/
async listPushIndexed(listKey, counterKey, value, ttlMs) {
const existingCounter = this.cache.get(counterKey);
if (existingCounter !== void 0 && typeof existingCounter !== "number") throw new Error(`${counterKey} exists but is not a number`);
const index = typeof existingCounter === "number" ? existingCounter : 0;
const existingList = this.cache.get(listKey);
if (existingList !== void 0 && !Array.isArray(existingList)) throw new Error(`${listKey} exists but is not an array`);
const list = Array.isArray(existingList) ? existingList : [];
const ttl = this.ttlMs > 0 ? Math.max(this.ttlMs, ttlMs ?? 0) : Infinity;
this.cache.set(counterKey, index + 1, { ttl });
list.push({
...value,
index
});
this.cache.set(listKey, list, { ttl });
return index;
}
};
Object.defineProperty(exports, "InMemoryServerCache", {
enumerable: true,
get: function() {
return InMemoryServerCache;
}
});
Object.defineProperty(exports, "MastraServerCache", {
enumerable: true,
get: function() {
return MastraServerCache;
}
});
//# sourceMappingURL=inmemory-fgR8sq-U.cjs.map