File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed
Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change 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 ( ) ;
You can’t perform that action at this time.
0 commit comments