import { CacheEntry } from './types.js';
/**
* Simple in-memory cache with TTL support
*/
export class Cache<T> {
private cache = new Map<string, CacheEntry<T>>();
private defaultTTL: number;
constructor(defaultTTL: number = 300000) { // Default 5 minutes
this.defaultTTL = defaultTTL;
// Clean up expired entries every minute
setInterval(() => this.cleanup(), 60000);
}
/**
* Set a value in the cache
*/
set(key: string, value: T, ttl?: number): void {
const entry: CacheEntry<T> = {
data: value,
timestamp: Date.now(),
ttl: ttl ?? this.defaultTTL,
};
this.cache.set(key, entry);
}
/**
* Get a value from the cache
*/
get(key: string): T | undefined {
const entry = this.cache.get(key);
if (!entry) {
return undefined;
}
// Check if entry has expired
if (Date.now() - entry.timestamp > entry.ttl) {
this.cache.delete(key);
return undefined;
}
return entry.data;
}
/**
* Check if a key exists and is not expired
*/
has(key: string): boolean {
return this.get(key) !== undefined;
}
/**
* Clear the entire cache
*/
clear(): void {
this.cache.clear();
}
/**
* Remove expired entries
*/
private cleanup(): void {
const now = Date.now();
for (const [key, entry] of this.cache.entries()) {
if (now - entry.timestamp > entry.ttl) {
this.cache.delete(key);
}
}
}
/**
* Get cache statistics
*/
getStats(): { size: number; keys: string[] } {
return {
size: this.cache.size,
keys: Array.from(this.cache.keys()),
};
}
}