@@ -19,15 +19,15 @@ var zlib = require('zlib');
19
19
* Node zlib documentation for a list of valid options for gzip:
20
20
* https://nodejs.org/dist/latest-v4.x/docs/api/zlib.html#zlib_class_options
21
21
*/
22
- function redisStore ( args ) {
22
+ function redisStore ( args = { } ) {
23
23
var self = {
24
24
name : 'redis' ,
25
25
events : new EventEmitter ( )
26
26
} ;
27
27
28
28
// cache-manager should always pass in args
29
29
/* istanbul ignore next */
30
- var redisOptions = getFromUrl ( args ) || args || { } ;
30
+ var redisOptions = getFromUrl ( args ) || args ;
31
31
var poolSettings = redisOptions ;
32
32
var Promise = args . promiseDependency || global . Promise ;
33
33
@@ -216,20 +216,18 @@ function redisStore(args) {
216
216
* @returns {Promise }
217
217
*/
218
218
self . set = function ( key , value , options , cb ) {
219
+ options = options || { } ;
220
+ if ( typeof options === 'function' ) {
221
+ cb = options ;
222
+ options = { } ;
223
+ }
219
224
return new Promise ( function ( resolve , reject ) {
220
- if ( typeof options === 'function' ) {
221
- cb = options ;
222
- options = { } ;
223
- }
224
-
225
- cb = cb ? cb : ( err , result ) => err ? reject ( err ) : resolve ( result ) ;
225
+ cb = cb || ( ( err , result ) => err ? reject ( err ) : resolve ( result ) ) ;
226
226
227
227
if ( ! self . isCacheableValue ( value ) ) {
228
228
return cb ( new Error ( 'value cannot be ' + value ) ) ;
229
229
}
230
230
231
- options = options || { } ;
232
-
233
231
var ttl = ( options . ttl || options . ttl === 0 ) ? options . ttl : redisOptions . ttl ;
234
232
var compress = ( options . compress || options . compress === false ) ? options . compress : redisOptions . compress ;
235
233
if ( compress === true ) {
@@ -270,42 +268,51 @@ function redisStore(args) {
270
268
* @param {String|Array } key - The cache key or array of keys to delete
271
269
* @param {Object } [options] - The options (optional)
272
270
* @param {Function } [cb] - A callback that returns a potential error, otherwise null
271
+ * @returns {Promise }
273
272
*/
274
273
self . del = function ( key , options , cb ) {
275
- if ( typeof options === 'function' ) {
276
- cb = options ;
277
- options = { } ;
278
- }
274
+ return new Promise ( ( resolve , reject ) => {
275
+ cb = cb || ( ( err ) => err ? reject ( err ) : resolve ( 'OK' ) ) ;
279
276
280
- connect ( function ( err , conn ) {
281
- if ( err ) {
282
- return cb && cb ( err ) ;
277
+ if ( typeof options === 'function' ) {
278
+ cb = options ;
279
+ options = { } ;
283
280
}
284
281
285
- if ( Array . isArray ( key ) ) {
286
- var multi = conn . multi ( ) ;
287
- for ( var i = 0 , l = key . length ; i < l ; ++ i ) {
288
- multi . del ( key [ i ] ) ;
282
+ connect ( function ( err , conn ) {
283
+ if ( err ) {
284
+ return cb ( err ) ;
285
+ }
286
+
287
+ if ( Array . isArray ( key ) ) {
288
+ var multi = conn . multi ( ) ;
289
+ for ( var i = 0 , l = key . length ; i < l ; ++ i ) {
290
+ multi . del ( key [ i ] ) ;
291
+ }
292
+ multi . exec ( handleResponse ( conn , cb ) ) ;
289
293
}
290
- multi . exec ( handleResponse ( conn , cb ) ) ;
291
- }
292
- else {
293
- conn . del ( key , handleResponse ( conn , cb ) ) ;
294
- }
294
+ else {
295
+ conn . del ( key , handleResponse ( conn , cb ) ) ;
296
+ }
297
+ } ) ;
295
298
} ) ;
296
299
} ;
297
300
298
301
/**
299
302
* Delete all the keys of the currently selected DB
300
303
* @method reset
301
304
* @param {Function } [cb] - A callback that returns a potential error, otherwise null
305
+ * @returns {Promise }
302
306
*/
303
307
self . reset = function ( cb ) {
304
- connect ( function ( err , conn ) {
305
- if ( err ) {
306
- return cb && cb ( err ) ;
307
- }
308
- conn . flushdb ( handleResponse ( conn , cb ) ) ;
308
+ return new Promise ( ( resolve , reject ) => {
309
+ cb = cb || ( err => err ? reject ( err ) : resolve ( 'OK' ) ) ;
310
+ connect ( function ( err , conn ) {
311
+ if ( err ) {
312
+ return cb ( err ) ;
313
+ }
314
+ conn . flushdb ( handleResponse ( conn , cb ) ) ;
315
+ } ) ;
309
316
} ) ;
310
317
} ;
311
318
@@ -314,13 +321,17 @@ function redisStore(args) {
314
321
* @method ttl
315
322
* @param {String } key - The cache key
316
323
* @param {Function } cb - A callback that returns a potential error and the response
324
+ * @returns {Promise }
317
325
*/
318
326
self . ttl = function ( key , cb ) {
319
- connect ( function ( err , conn ) {
320
- if ( err ) {
321
- return cb && cb ( err ) ;
322
- }
323
- conn . ttl ( key , handleResponse ( conn , cb ) ) ;
327
+ return new Promise ( ( resolve , reject ) => {
328
+ cb = cb || ( ( err , res ) => err ? reject ( err ) : resolve ( res ) ) ;
329
+ connect ( function ( err , conn ) {
330
+ if ( err ) {
331
+ return cb ( err ) ;
332
+ }
333
+ conn . ttl ( key , handleResponse ( conn , cb ) ) ;
334
+ } ) ;
324
335
} ) ;
325
336
} ;
326
337
@@ -331,8 +342,10 @@ function redisStore(args) {
331
342
* @param {Object } [options] - The options (default: {})
332
343
* @param {number } [options.scanCount] - The number of keys to traverse with each call to SCAN (default: 100)
333
344
* @param {Function } cb - A callback that returns a potential error and the response
345
+ * @returns {Promise }
334
346
*/
335
347
self . keys = function ( pattern , options , cb ) {
348
+ options = options || { } ;
336
349
337
350
// Account for all argument permutations.
338
351
// Only cb supplied.
@@ -353,35 +366,38 @@ function redisStore(args) {
353
366
options = { } ;
354
367
}
355
368
356
- connect ( function ( err , conn ) {
357
- if ( err ) {
358
- return cb && cb ( err ) ;
359
- }
369
+ return new Promise ( ( resolve , reject ) => {
370
+ cb = cb || ( ( err , res ) => err ? reject ( err ) : resolve ( res ) ) ;
371
+ connect ( function ( err , conn ) {
372
+ if ( err ) {
373
+ return cb ( err ) ;
374
+ }
360
375
361
- // Use an object to dedupe as scan can return duplicates
362
- var keysObj = { } ;
363
- var scanCount = Number ( options . scanCount ) || 100 ;
376
+ // Use an object to dedupe as scan can return duplicates
377
+ var keysObj = { } ;
378
+ var scanCount = Number ( options . scanCount ) || 100 ;
364
379
365
- ( function nextBatch ( cursorId ) {
366
- conn . scan ( cursorId , 'match' , pattern , 'count' , scanCount , function ( err , result ) {
367
- if ( err ) {
368
- handleResponse ( conn , cb ) ( err ) ;
369
- }
380
+ ( function nextBatch ( cursorId ) {
381
+ conn . scan ( cursorId , 'match' , pattern , 'count' , scanCount , function ( err , result ) {
382
+ if ( err ) {
383
+ handleResponse ( conn , cb ) ( err ) ;
384
+ }
370
385
371
- var nextCursorId = result [ 0 ] ;
372
- var keys = result [ 1 ] ;
386
+ var nextCursorId = result [ 0 ] ;
387
+ var keys = result [ 1 ] ;
373
388
374
- for ( var i = 0 , l = keys . length ; i < l ; ++ i ) {
375
- keysObj [ keys [ i ] ] = 1 ;
376
- }
389
+ for ( var i = 0 , l = keys . length ; i < l ; ++ i ) {
390
+ keysObj [ keys [ i ] ] = 1 ;
391
+ }
377
392
378
- if ( nextCursorId !== '0' ) {
379
- return nextBatch ( nextCursorId ) ;
380
- }
393
+ if ( nextCursorId !== '0' ) {
394
+ return nextBatch ( nextCursorId ) ;
395
+ }
381
396
382
- handleResponse ( conn , cb ) ( null , Object . keys ( keysObj ) ) ;
383
- } ) ;
384
- } ) ( 0 ) ;
397
+ handleResponse ( conn , cb ) ( null , Object . keys ( keysObj ) ) ;
398
+ } ) ;
399
+ } ) ( 0 ) ;
400
+ } ) ;
385
401
} ) ;
386
402
} ;
387
403
@@ -402,22 +418,26 @@ function redisStore(args) {
402
418
* Returns the underlying redis client connection
403
419
* @method getClient
404
420
* @param {Function } cb - A callback that returns a potential error and an object containing the Redis client and a done method
421
+ * @returns {Promise }
405
422
*/
406
423
self . getClient = function ( cb ) {
407
- connect ( function ( err , conn ) {
408
- if ( err ) {
409
- return cb && cb ( err ) ;
410
- }
411
- cb ( null , {
412
- client : conn ,
413
- done : function ( done ) {
414
- var args = Array . prototype . slice . call ( arguments , 1 ) ;
415
- pool . release ( conn ) ;
416
-
417
- if ( done && typeof done === 'function' ) {
418
- done . apply ( null , args ) ;
419
- }
424
+ return new Promise ( ( resolve , reject ) => {
425
+ cb = cb || ( ( err , res ) => err ? reject ( err ) : resolve ( res ) ) ;
426
+ connect ( function ( err , conn ) {
427
+ if ( err ) {
428
+ return cb ( err ) ;
420
429
}
430
+ cb ( null , {
431
+ client : conn ,
432
+ done : function ( done ) {
433
+ var args = Array . prototype . slice . call ( arguments , 1 ) ;
434
+ pool . release ( conn ) ;
435
+
436
+ if ( done && typeof done === 'function' ) {
437
+ done . apply ( null , args ) ;
438
+ }
439
+ }
440
+ } ) ;
421
441
} ) ;
422
442
} ) ;
423
443
} ;
0 commit comments