homeio
PublikFork dari doctor-io/homeio
lru.ts
import "server-only";
type CacheEntry<T> = {
value: T;
expiresAt: number;
};
export class LruCache<T> {
private readonly entries = new Map<string, CacheEntry<T>>();
constructor(
private readonly maxEntries: number,
private readonly defaultTtlMs: number,
) {}
get(key: string): T | null {
const entry = this.entries.get(key);
if (!entry) return null;
if (entry.expiresAt <= Date.now()) {
this.entries.delete(key);
return null;
}
this.entries.delete(key);
this.entries.set(key, entry);
return entry.value;
}
set(key: string, value: T, ttlMs = this.defaultTtlMs): void {
if (this.entries.has(key)) {
this.entries.delete(key);
}
this.entries.set(key, {
value,
expiresAt: Date.now() + ttlMs,
});
if (this.entries.size > this.maxEntries) {
const oldestKey = this.entries.keys().next().value;
if (oldestKey) {
this.entries.delete(oldestKey);
}
}
}
delete(key: string): void {
this.entries.delete(key);
}
clear(): void {
this.entries.clear();
}
}