1
- import redis from 'redis' ;
1
+ import { createClient } from 'redis' ;
2
2
import logger from '../../logger' ;
3
3
import { KeyPromiseQueue } from '../../KeyPromiseQueue' ;
4
4
@@ -15,114 +15,76 @@ const isValidTTL = ttl => typeof ttl === 'number' && ttl > 0;
15
15
export class RedisCacheAdapter {
16
16
constructor ( redisCtx , ttl = DEFAULT_REDIS_TTL ) {
17
17
this . ttl = isValidTTL ( ttl ) ? ttl : DEFAULT_REDIS_TTL ;
18
- this . client = redis . createClient ( redisCtx ) ;
18
+ this . client = createClient ( redisCtx ) ;
19
19
this . queue = new KeyPromiseQueue ( ) ;
20
20
}
21
21
22
- handleShutdown ( ) {
22
+ async connect ( ) {
23
+ if ( this . client . isOpen ) {
24
+ return ;
25
+ }
26
+ return this . client . connect ( ) ;
27
+ }
28
+
29
+ async handleShutdown ( ) {
23
30
if ( ! this . client ) {
24
- return Promise . resolve ( ) ;
31
+ return ;
32
+ }
33
+ try {
34
+ await this . client . quit ( ) ;
35
+ } catch ( err ) {
36
+ logger . error ( 'RedisCacheAdapter error on shutdown' , { error : err } ) ;
25
37
}
26
- return new Promise ( resolve => {
27
- this . client . quit ( err => {
28
- if ( err ) {
29
- logger . error ( 'RedisCacheAdapter error on shutdown' , { error : err } ) ;
30
- }
31
- resolve ( ) ;
32
- } ) ;
33
- } ) ;
34
38
}
35
39
36
- get ( key ) {
40
+ async get ( key ) {
37
41
debug ( 'get' , { key } ) ;
38
- return this . queue . enqueue (
39
- key ,
40
- ( ) =>
41
- new Promise ( resolve => {
42
- this . client . get ( key , function ( err , res ) {
43
- debug ( '-> get' , { key, res } ) ;
44
- if ( ! res ) {
45
- return resolve ( null ) ;
46
- }
47
- resolve ( JSON . parse ( res ) ) ;
48
- } ) ;
49
- } )
50
- ) ;
42
+ try {
43
+ await this . queue . enqueue ( key ) ;
44
+ const res = await this . client . get ( key ) ;
45
+ if ( ! res ) {
46
+ return null ;
47
+ }
48
+ return JSON . parse ( res ) ;
49
+ } catch ( err ) {
50
+ logger . error ( 'RedisCacheAdapter error on get' , { error : err } ) ;
51
+ }
51
52
}
52
53
53
- put ( key , value , ttl = this . ttl ) {
54
+ async put ( key , value , ttl = this . ttl ) {
54
55
value = JSON . stringify ( value ) ;
55
56
debug ( 'put' , { key, value, ttl } ) ;
56
-
57
+ await this . queue . enqueue ( key ) ;
57
58
if ( ttl === 0 ) {
58
59
// ttl of zero is a logical no-op, but redis cannot set expire time of zero
59
- return this . queue . enqueue ( key , ( ) => Promise . resolve ( ) ) ;
60
+ return ;
60
61
}
61
62
62
63
if ( ttl === Infinity ) {
63
- return this . queue . enqueue (
64
- key ,
65
- ( ) =>
66
- new Promise ( resolve => {
67
- this . client . set ( key , value , function ( ) {
68
- resolve ( ) ;
69
- } ) ;
70
- } )
71
- ) ;
64
+ return this . client . set ( key , value ) ;
72
65
}
73
66
74
67
if ( ! isValidTTL ( ttl ) ) {
75
68
ttl = this . ttl ;
76
69
}
77
-
78
- return this . queue . enqueue (
79
- key ,
80
- ( ) =>
81
- new Promise ( resolve => {
82
- this . client . psetex ( key , ttl , value , function ( ) {
83
- resolve ( ) ;
84
- } ) ;
85
- } )
86
- ) ;
70
+ return this . client . set ( key , value , { PX : ttl } ) ;
87
71
}
88
72
89
- del ( key ) {
73
+ async del ( key ) {
90
74
debug ( 'del' , { key } ) ;
91
- return this . queue . enqueue (
92
- key ,
93
- ( ) =>
94
- new Promise ( resolve => {
95
- this . client . del ( key , function ( ) {
96
- resolve ( ) ;
97
- } ) ;
98
- } )
99
- ) ;
75
+ await this . queue . enqueue ( key ) ;
76
+ return this . client . del ( key ) ;
100
77
}
101
78
102
- clear ( ) {
79
+ async clear ( ) {
103
80
debug ( 'clear' ) ;
104
- return this . queue . enqueue (
105
- FLUSH_DB_KEY ,
106
- ( ) =>
107
- new Promise ( resolve => {
108
- this . client . flushdb ( function ( ) {
109
- resolve ( ) ;
110
- } ) ;
111
- } )
112
- ) ;
81
+ await this . queue . enqueue ( FLUSH_DB_KEY ) ;
82
+ return this . client . sendCommand ( [ 'FLUSHDB' ] ) ;
113
83
}
114
84
115
85
// Used for testing
116
- async getAllKeys ( ) {
117
- return new Promise ( ( resolve , reject ) => {
118
- this . client . keys ( '*' , ( err , keys ) => {
119
- if ( err ) {
120
- reject ( err ) ;
121
- } else {
122
- resolve ( keys ) ;
123
- }
124
- } ) ;
125
- } ) ;
86
+ getAllKeys ( ) {
87
+ return this . client . keys ( '*' ) ;
126
88
}
127
89
}
128
90
0 commit comments