Files
klz-cables.com/lib/services/cache/memory-cache-service.ts
Marc Mintel b05a21350c
Some checks failed
Build & Deploy / deploy (push) Failing after 3m45s
umami, glitchtip, redis
2026-01-18 15:37:51 +01:00

31 lines
780 B
TypeScript

import type { CacheService, CacheSetOptions } from './cache-service';
type Entry = {
value: unknown;
expiresAt?: number;
};
export class MemoryCacheService implements CacheService {
private readonly store = new Map<string, Entry>();
async get<T>(key: string) {
const entry = this.store.get(key);
if (!entry) return undefined;
if (entry.expiresAt && Date.now() > entry.expiresAt) {
this.store.delete(key);
return undefined;
}
return entry.value as T;
}
async set<T>(key: string, value: T, options?: CacheSetOptions) {
const ttl = options?.ttlSeconds;
const expiresAt = ttl ? Date.now() + ttl * 1000 : undefined;
this.store.set(key, { value, expiresAt });
}
async del(key: string) {
this.store.delete(key);
}
}