1- const redis = require ( 'redis' ) ;
1+ const Redis = require ( 'ioredis' ) . default ;
22const BaseCache = require ( './base' ) ;
33
44const DEQUEUE_SCRIPT = `
@@ -18,7 +18,7 @@ class RedisCache extends BaseCache {
1818 * @return {!Promise }
1919 */
2020 init ( ) {
21- this . _client = redis . createClient ( this . _settings ) ;
21+ this . _client = new Redis ( this . _settings ) ;
2222 return Promise . resolve ( ) ;
2323 }
2424
@@ -28,12 +28,13 @@ class RedisCache extends BaseCache {
2828 */
2929 clear ( ) {
3030 return new Promise ( ( resolve , reject ) => {
31+ if ( ! this . _client ) return reject ( new Error ( "RedisCache: this._client is undefined" ) ) ;
3132 this . _client . flushdb ( error => {
3233 if ( error ) {
3334 reject ( error ) ;
3435 return ;
3536 }
36- resolve ( ) ;
37+ resolve ( undefined ) ;
3738 } ) ;
3839 } ) ;
3940 }
@@ -43,7 +44,7 @@ class RedisCache extends BaseCache {
4344 * @override
4445 */
4546 close ( ) {
46- this . _client . quit ( ) ;
47+ this . _client ? .quit ( ) ;
4748 return Promise . resolve ( ) ;
4849 }
4950
@@ -54,13 +55,14 @@ class RedisCache extends BaseCache {
5455 */
5556 get ( key ) {
5657 return new Promise ( ( resolve , reject ) => {
58+ if ( ! this . _client ) return reject ( new Error ( "RedisCache: this._client is undefined" ) ) ;
5759 this . _client . get ( key , ( error , json ) => {
5860 if ( error ) {
5961 reject ( error ) ;
6062 return ;
6163 }
6264 try {
63- const value = JSON . parse ( json || null ) ;
65+ const value = JSON . parse ( String ( json ) ) ;
6466 resolve ( value ) ;
6567 } catch ( _error ) {
6668 reject ( _error ) ;
@@ -84,21 +86,23 @@ class RedisCache extends BaseCache {
8486 reject ( error ) ;
8587 return ;
8688 }
89+ if ( ! this . _client ) return reject ( new Error ( "RedisCache: this._client is undefined" ) ) ;
8790 this . _client . set ( key , json , error => {
8891 if ( error ) {
8992 reject ( error ) ;
9093 return ;
9194 }
9295 if ( ! this . _settings . expire ) {
93- resolve ( ) ;
96+ resolve ( undefined ) ;
9497 return ;
9598 }
99+ if ( ! this . _client ) return reject ( new Error ( "RedisCache: this._client is undefined" ) ) ;
96100 this . _client . expire ( key , this . _settings . expire , _error => {
97101 if ( _error ) {
98102 reject ( _error ) ;
99103 return ;
100104 }
101- resolve ( ) ;
105+ resolve ( undefined ) ;
102106 } ) ;
103107 } ) ;
104108 } ) ;
@@ -120,21 +124,23 @@ class RedisCache extends BaseCache {
120124 reject ( error ) ;
121125 return ;
122126 }
127+ if ( ! this . _client ) return reject ( new Error ( "RedisCache: this._client is undefined" ) ) ;
123128 this . _client . zadd ( key , priority , json , error => {
124129 if ( error ) {
125130 reject ( error ) ;
126131 return ;
127132 }
128133 if ( ! this . _settings . expire ) {
129- resolve ( ) ;
134+ resolve ( undefined ) ;
130135 return ;
131136 }
137+ if ( ! this . _client ) return reject ( new Error ( "RedisCache: this._client is undefined" ) ) ;
132138 this . _client . expire ( key , this . _settings . expire , _error => {
133139 if ( _error ) {
134140 reject ( _error ) ;
135141 return ;
136142 }
137- resolve ( ) ;
143+ resolve ( undefined ) ;
138144 } ) ;
139145 } ) ;
140146 } ) ;
@@ -147,13 +153,14 @@ class RedisCache extends BaseCache {
147153 */
148154 dequeue ( key ) {
149155 return new Promise ( ( resolve , reject ) => {
156+ if ( ! this . _client ) return reject ( new Error ( "RedisCache: this._client is undefined" ) ) ;
150157 this . _client . eval ( DEQUEUE_SCRIPT , 1 , key , ( error , json ) => {
151158 if ( error ) {
152159 reject ( error ) ;
153160 return ;
154161 }
155162 try {
156- const value = JSON . parse ( json || null ) ;
163+ const value = JSON . parse ( String ( json ) ) ;
157164 resolve ( value ) ;
158165 } catch ( _error ) {
159166 reject ( _error ) ;
@@ -169,6 +176,7 @@ class RedisCache extends BaseCache {
169176 */
170177 size ( key ) {
171178 return new Promise ( ( resolve , reject ) => {
179+ if ( ! this . _client ) return reject ( new Error ( "RedisCache: this._client is undefined" ) ) ;
172180 this . _client . zcount ( key , '-inf' , 'inf' , ( error , size ) => {
173181 if ( error ) {
174182 reject ( error ) ;
@@ -186,12 +194,13 @@ class RedisCache extends BaseCache {
186194 */
187195 remove ( key ) {
188196 return new Promise ( ( resolve , reject ) => {
197+ if ( ! this . _client ) return reject ( new Error ( "RedisCache: this._client is undefined" ) ) ;
189198 this . _client . del ( key , error => {
190199 if ( error ) {
191200 reject ( error ) ;
192201 return ;
193202 }
194- resolve ( ) ;
203+ resolve ( undefined ) ;
195204 } ) ;
196205 } ) ;
197206 }
0 commit comments