@@ -9,43 +9,27 @@ if (!process.env.REDIS_URL) {
9
9
const PREFIX = 'string:' ;
10
10
const CONNECT_TIMEOUT_MS = 5 * 50 * 1000 ;
11
11
12
- function createRedisClient ( url ) {
13
- const client = createClient ( {
14
- url,
15
- name : `cache-handler:${ PREFIX } ${ process . env . PORT ?? process . pid } ` ,
16
- socket : {
17
- connectTimeout : CONNECT_TIMEOUT_MS ,
18
- } ,
12
+ const client = createClient ( {
13
+ url : process . env . REDIS_URL ,
14
+ name : `cache-handler:${ PREFIX } ${ process . env . PORT ?? process . pid } ` ,
15
+ socket : {
16
+ connectTimeout : CONNECT_TIMEOUT_MS ,
17
+ } ,
18
+ } ) ;
19
+
20
+ client . on ( 'error' , ( error ) => {
21
+ console . error ( 'Redis error:' , error . message ) ;
22
+ } ) ;
23
+
24
+ IncrementalCache . onCreation ( async ( options ) => {
25
+ await client . connect ( ) ;
26
+
27
+ const handler = createHandler ( {
28
+ client,
29
+ keyPrefix : PREFIX ,
19
30
} ) ;
20
31
21
- client . on ( 'error' , ( error ) => {
22
- console . error ( 'Redis error:' , error . message ) ;
23
- } ) ;
24
-
25
- return client ;
26
- }
27
-
28
- async function connect ( client ) {
29
- try {
30
- await client . connect ( ) ;
31
- } catch ( error ) {
32
- console . error ( 'Redis connection error:' , error . message ) ;
33
- }
34
- }
35
-
36
- if ( process . env . SERVER_STARTED ) {
37
- const client = createRedisClient ( process . env . REDIS_URL ) ;
38
-
39
- connect ( client ) . then ( ( ) => {
40
- console . log ( 'Redis connected' ) ;
41
- } ) ;
42
-
43
- IncrementalCache . onCreation (
44
- createHandler ( {
45
- client,
46
- keyPrefix : PREFIX ,
47
- } ) ,
48
- ) ;
49
- }
32
+ return handler ( options ) ;
33
+ } ) ;
50
34
51
35
module . exports = IncrementalCache ;
0 commit comments