@@ -9,31 +9,57 @@ class RedisCache implements Cache {
99 private redis_ : Redis ;
1010
1111 constructor ( options : RedisOptions ) {
12- this . redis_ = new Redis ( options ) ;
12+ this . redis_ = new Redis ( {
13+ ...options ,
14+ connectTimeout : 500 , // 500ms to connect
15+ commandTimeout : 200 , // 200ms per command
16+ retryStrategy : ( times ) => {
17+ // Stop retrying after 2 attempts
18+ if ( times > 2 ) return null ;
19+ return 50 ; // Wait only 50ms between retries
20+ } ,
21+ maxRetriesPerRequest : 0 , // Don't retry - fail immediately
22+ enableOfflineQueue : false , // Don't queue commands when disconnected
23+ } ) ;
24+
25+ this . redis_ . on ( 'error' , ( err ) => {
26+ console . error ( 'Redis error (app will continue without cache):' , err . message ) ;
27+ } ) ;
1328 }
1429
1530 private static key_ = ( { collection, id } : Selector ) : string => {
1631 return `${ collection } /${ id } ` ;
1732 } ;
1833
1934 async get ( selector : Selector ) : Promise < object | null > {
20- const data = await this . redis_ . get ( RedisCache . key_ ( selector ) ) ;
21- if ( ! data ) return null ;
22-
23- return JSON . parse ( data ) ;
35+ try {
36+ const data = await this . redis_ . get ( RedisCache . key_ ( selector ) ) ;
37+ if ( ! data ) return null ;
38+ return JSON . parse ( data ) ;
39+ } catch ( err ) {
40+ console . error ( 'Redis GET failed, continuing without cache:' , err ) ;
41+ return null ;
42+ }
2443 }
2544
2645 async set ( selector : Selector , value : object | null ) : Promise < void > {
27- if ( ! value ) {
28- await this . redis_ . del ( RedisCache . key_ ( selector ) ) ;
29- return ;
46+ try {
47+ if ( ! value ) {
48+ await this . redis_ . del ( RedisCache . key_ ( selector ) ) ;
49+ return ;
50+ }
51+ await this . redis_ . setex ( RedisCache . key_ ( selector ) , RedisCache . DEFAULT_TTL , JSON . stringify ( value ) ) ;
52+ } catch ( err ) {
53+ console . error ( 'Redis SET failed, continuing without cache:' , err ) ;
3054 }
31-
32- await this . redis_ . setex ( RedisCache . key_ ( selector ) , RedisCache . DEFAULT_TTL , JSON . stringify ( value ) ) ;
3355 }
3456
3557 async remove ( selector : Selector ) : Promise < void > {
36- await this . redis_ . del ( RedisCache . key_ ( selector ) ) ;
58+ try {
59+ await this . redis_ . del ( RedisCache . key_ ( selector ) ) ;
60+ } catch ( err ) {
61+ console . error ( 'Redis DEL failed, continuing without cache:' , err ) ;
62+ }
3763 }
3864}
3965
0 commit comments