31 lines
729 B
TypeScript
31 lines
729 B
TypeScript
import type { RecentProjectsCachePort } from '../../../core/application/ports/RecentProjectsCachePort';
|
|
|
|
interface CacheEntry<T> {
|
|
value: T;
|
|
expiresAt: number;
|
|
}
|
|
|
|
export class InMemoryRecentProjectsCache<T> implements RecentProjectsCachePort<T> {
|
|
readonly #entries = new Map<string, CacheEntry<T>>();
|
|
|
|
async get(key: string): Promise<T | null> {
|
|
const entry = this.#entries.get(key);
|
|
if (!entry) {
|
|
return null;
|
|
}
|
|
|
|
if (entry.expiresAt <= Date.now()) {
|
|
this.#entries.delete(key);
|
|
return null;
|
|
}
|
|
|
|
return entry.value;
|
|
}
|
|
|
|
async set(key: string, value: T, ttlMs: number): Promise<void> {
|
|
this.#entries.set(key, {
|
|
value,
|
|
expiresAt: Date.now() + ttlMs,
|
|
});
|
|
}
|
|
}
|