File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -410,6 +410,15 @@ describe('TTLCache', () => {
410410 cache . destroy ( ) ;
411411 } ) ;
412412
413+ it ( 'rejects an empty string cache key' , ( ) => {
414+ const cache = new TTLCache < string > ( ) ;
415+
416+ expect ( ( ) => cache . set ( '' , 'value' , 60_000 ) ) . toThrow ( 'Cache key cannot be empty' ) ;
417+ expect ( cache . has ( '' ) ) . toBe ( false ) ;
418+
419+ cache . destroy ( ) ;
420+ } ) ;
421+
413422 it ( 'handles rapid get/set/delete cycles' , ( ) => {
414423 const cache = new TTLCache < number > ( ) ;
415424 for ( let i = 0 ; i < 100 ; i ++ ) {
Original file line number Diff line number Diff line change @@ -141,6 +141,7 @@ export class TTLCache<T> {
141141 }
142142
143143 set ( key : string , value : T , ttlMs : number ) : void {
144+ if ( key === '' ) throw new Error ( 'Cache key cannot be empty' ) ;
144145 if ( ttlMs <= 0 ) throw new RangeError ( `ttlMs must be positive, got ${ ttlMs } ` ) ;
145146
146147 const maxSize = this . maxSize ;
You can’t perform that action at this time.
0 commit comments