@@ -19,15 +19,15 @@ var zlib = require('zlib');
1919 * Node zlib documentation for a list of valid options for gzip:
2020 * https://nodejs.org/dist/latest-v4.x/docs/api/zlib.html#zlib_class_options
2121 */
22- function redisStore ( args ) {
22+ function redisStore ( args = { } ) {
2323 var self = {
2424 name : 'redis' ,
2525 events : new EventEmitter ( )
2626 } ;
2727
2828 // cache-manager should always pass in args
2929 /* istanbul ignore next */
30- var redisOptions = getFromUrl ( args ) || args || { } ;
30+ var redisOptions = getFromUrl ( args ) || args ;
3131 var poolSettings = redisOptions ;
3232 var Promise = args . promiseDependency || global . Promise ;
3333
@@ -216,20 +216,18 @@ function redisStore(args) {
216216 * @returns {Promise }
217217 */
218218 self . set = function ( key , value , options , cb ) {
219+ options = options || { } ;
220+ if ( typeof options === 'function' ) {
221+ cb = options ;
222+ options = { } ;
223+ }
219224 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 ) ) ;
226226
227227 if ( ! self . isCacheableValue ( value ) ) {
228228 return cb ( new Error ( 'value cannot be ' + value ) ) ;
229229 }
230230
231- options = options || { } ;
232-
233231 var ttl = ( options . ttl || options . ttl === 0 ) ? options . ttl : redisOptions . ttl ;
234232 var compress = ( options . compress || options . compress === false ) ? options . compress : redisOptions . compress ;
235233 if ( compress === true ) {
@@ -270,42 +268,51 @@ function redisStore(args) {
270268 * @param {String|Array } key - The cache key or array of keys to delete
271269 * @param {Object } [options] - The options (optional)
272270 * @param {Function } [cb] - A callback that returns a potential error, otherwise null
271+ * @returns {Promise }
273272 */
274273 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' ) ) ;
279276
280- connect ( function ( err , conn ) {
281- if ( err ) {
282- return cb && cb ( err ) ;
277+ if ( typeof options === 'function' ) {
278+ cb = options ;
279+ options = { } ;
283280 }
284281
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 ) ) ;
289293 }
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+ } ) ;
295298 } ) ;
296299 } ;
297300
298301 /**
299302 * Delete all the keys of the currently selected DB
300303 * @method reset
301304 * @param {Function } [cb] - A callback that returns a potential error, otherwise null
305+ * @returns {Promise }
302306 */
303307 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+ } ) ;
309316 } ) ;
310317 } ;
311318
@@ -314,13 +321,17 @@ function redisStore(args) {
314321 * @method ttl
315322 * @param {String } key - The cache key
316323 * @param {Function } cb - A callback that returns a potential error and the response
324+ * @returns {Promise }
317325 */
318326 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+ } ) ;
324335 } ) ;
325336 } ;
326337
@@ -331,8 +342,10 @@ function redisStore(args) {
331342 * @param {Object } [options] - The options (default: {})
332343 * @param {number } [options.scanCount] - The number of keys to traverse with each call to SCAN (default: 100)
333344 * @param {Function } cb - A callback that returns a potential error and the response
345+ * @returns {Promise }
334346 */
335347 self . keys = function ( pattern , options , cb ) {
348+ options = options || { } ;
336349
337350 // Account for all argument permutations.
338351 // Only cb supplied.
@@ -353,35 +366,38 @@ function redisStore(args) {
353366 options = { } ;
354367 }
355368
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+ }
360375
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 ;
364379
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+ }
370385
371- var nextCursorId = result [ 0 ] ;
372- var keys = result [ 1 ] ;
386+ var nextCursorId = result [ 0 ] ;
387+ var keys = result [ 1 ] ;
373388
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+ }
377392
378- if ( nextCursorId !== '0' ) {
379- return nextBatch ( nextCursorId ) ;
380- }
393+ if ( nextCursorId !== '0' ) {
394+ return nextBatch ( nextCursorId ) ;
395+ }
381396
382- handleResponse ( conn , cb ) ( null , Object . keys ( keysObj ) ) ;
383- } ) ;
384- } ) ( 0 ) ;
397+ handleResponse ( conn , cb ) ( null , Object . keys ( keysObj ) ) ;
398+ } ) ;
399+ } ) ( 0 ) ;
400+ } ) ;
385401 } ) ;
386402 } ;
387403
@@ -402,22 +418,26 @@ function redisStore(args) {
402418 * Returns the underlying redis client connection
403419 * @method getClient
404420 * @param {Function } cb - A callback that returns a potential error and an object containing the Redis client and a done method
421+ * @returns {Promise }
405422 */
406423 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 ) ;
420429 }
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+ } ) ;
421441 } ) ;
422442 } ) ;
423443 } ;
0 commit comments