Project Files
src / cache.ts
// Tiny in-process LRU + TTL cache. No deps, no disk. Resets when the
// LM Studio plugin worker (or the MCP server process) restarts.
interface Entry<V> {
value: V;
expiresAt: number;
}
export class TtlLruCache<V> {
private map = new Map<string, Entry<V>>();
constructor(private readonly maxEntries: number, private readonly ttlMs: number) {}
get(key: string): V | undefined {
const e = this.map.get(key);
if (!e) return undefined;
if (Date.now() > e.expiresAt) {
this.map.delete(key);
return undefined;
}
// Refresh LRU position.
this.map.delete(key);
this.map.set(key, e);
return e.value;
}
set(key: string, value: V): void {
if (this.ttlMs <= 0) return; // caching disabled
if (this.map.has(key)) this.map.delete(key);
this.map.set(key, { value, expiresAt: Date.now() + this.ttlMs });
while (this.map.size > this.maxEntries) {
const oldest = this.map.keys().next().value;
if (oldest === undefined) break;
this.map.delete(oldest);
}
}
clear(): void {
this.map.clear();
}
get size(): number {
return this.map.size;
}
}