"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.commandCache = exports.Cache = void 0;
/**
* Simple in-memory TTL cache.
*/
class Cache {
store = new Map();
defaultTtlMs;
constructor(defaultTtlMs = 60_000) {
this.defaultTtlMs = defaultTtlMs;
}
get(key) {
const entry = this.store.get(key);
if (!entry)
return undefined;
if (Date.now() > entry.expiresAt) {
this.store.delete(key);
return undefined;
}
return entry.value;
}
set(key, value, ttlMs) {
const expiresAt = Date.now() + (ttlMs ?? this.defaultTtlMs);
this.store.set(key, { value, expiresAt });
}
has(key) {
return this.get(key) !== undefined;
}
delete(key) {
return this.store.delete(key);
}
clear() {
this.store.clear();
}
size() {
return this.store.size;
}
/**
* Evict expired entries.
*/
evictExpired() {
const now = Date.now();
let evicted = 0;
for (const [key, entry] of this.store) {
if (now > entry.expiresAt) {
this.store.delete(key);
evicted++;
}
}
return evicted;
}
}
exports.Cache = Cache;
/**
* Command result cache — stores execCommand results keyed by command + cwd.
*/
exports.commandCache = new Cache(5 * 60 * 1000);
//# sourceMappingURL=cache.js.map