AI scaffolded NodeJS version of the App.
This commit is contained in:
29
app-new/backend/src/cache.ts
Normal file
29
app-new/backend/src/cache.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
type CacheEntry<T> = {
|
||||
value: T;
|
||||
expiresAt: number;
|
||||
};
|
||||
|
||||
export class MemoryCache {
|
||||
private readonly store = new Map<string, CacheEntry<unknown>>();
|
||||
|
||||
public get<T>(key: string): T | undefined {
|
||||
const hit = this.store.get(key);
|
||||
if (!hit) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (Date.now() >= hit.expiresAt) {
|
||||
this.store.delete(key);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return hit.value as T;
|
||||
}
|
||||
|
||||
public set<T>(key: string, value: T, ttlMs: number): void {
|
||||
this.store.set(key, {
|
||||
value,
|
||||
expiresAt: Date.now() + ttlMs
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user