export class RelationCache {
    maxSize;
    defaultTTL;
    cache = new Map();
    constructor(maxSize = 1000, defaultTTL = 300000) {
        this.maxSize = maxSize;
        this.defaultTTL = defaultTTL;
    }
    get(key) {
        const entry = this.cache.get(key);
        if (!entry) {
            return null;
        }
        if (Date.now() - entry.timestamp > entry.ttl) {
            this.cache.delete(key);
            return null;
        }
        entry.lastAccessed = Date.now();
        return entry.data;
    }
    set(key, data, ttl = this.defaultTTL) {
        const now = Date.now();
        const existingEntry = this.cache.get(key);
        if (!existingEntry && this.cache.size >= this.maxSize) {
            this.evictLeastRecentlyUsed();
        }
        this.cache.set(key, {
            data,
            timestamp: now,
            ttl,
            lastAccessed: now,
        });
    }
    delete(key) {
        this.cache.delete(key);
    }
    keys() {
        this.pruneExpired();
        return [...this.cache.keys()];
    }
    evictLeastRecentlyUsed() {
        let oldestKey;
        let oldestAccess = Number.POSITIVE_INFINITY;
        for (const [key, entry] of this.cache.entries()) {
            if (entry.lastAccessed < oldestAccess) {
                oldestAccess = entry.lastAccessed;
                oldestKey = key;
            }
        }
        if (oldestKey !== undefined) {
            this.cache.delete(oldestKey);
        }
    }
    pruneExpired() {
        const now = Date.now();
        for (const [key, entry] of this.cache.entries()) {
            if (now - entry.timestamp > entry.ttl) {
                this.cache.delete(key);
            }
        }
    }
}
//# sourceMappingURL=relation-cache.js.map