Skip to content

Commit 72ed673

Browse files
authored
Merge pull request #17 from maitamdev/feat/cache-utils
feat(utils): add cache utility
2 parents d69578e + 1820ffd commit 72ed673

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/utils/cacheUtils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Simple in-memory cache with TTL
2+
interface CacheEntry<T> { value: T; expiresAt: number; }
3+
class SimpleCache {
4+
private cache = new Map<string, CacheEntry<unknown>>();
5+
set<T>(key: string, value: T, ttlMs: number = 300000): void {
6+
this.cache.set(key, { value, expiresAt: Date.now() + ttlMs });
7+
}
8+
get<T>(key: string): T | null {
9+
const entry = this.cache.get(key);
10+
if (!entry) return null;
11+
if (Date.now() > entry.expiresAt) { this.cache.delete(key); return null; }
12+
return entry.value as T;
13+
}
14+
has(key: string): boolean { return this.get(key) !== null; }
15+
delete(key: string): void { this.cache.delete(key); }
16+
clear(): void { this.cache.clear(); }
17+
size(): number { return this.cache.size; }
18+
}
19+
export const appCache = new SimpleCache();

0 commit comments

Comments
 (0)