@@ -29,6 +29,7 @@ function redisStore(args) {
2929 /* istanbul ignore next */
3030 var redisOptions = getFromUrl ( args ) || args || { } ;
3131 var poolSettings = redisOptions ;
32+ var Promise = args . promiseDependency || global . Promise ;
3233
3334 redisOptions . host = redisOptions . host || '127.0.0.1' ;
3435 redisOptions . port = redisOptions . port || 6379 ;
@@ -174,27 +175,32 @@ function redisStore(args) {
174175 * @param {Object } [options] - The options (optional)
175176 * @param {boolean|Object } options.compress - compression configuration
176177 * @param {Function } cb - A callback that returns a potential error and the response
178+ * @returns {Promise }
177179 */
178180 self . get = function ( key , options , cb ) {
179- if ( typeof options === 'function' ) {
180- cb = options ;
181- options = { } ;
182- }
183- options = options || { } ;
184- options . parse = true ;
181+ return new Promise ( function ( resolve , reject ) {
182+ if ( typeof options === 'function' ) {
183+ cb = options ;
184+ options = { } ;
185+ }
186+ options = options || { } ;
187+ options . parse = true ;
185188
186- var compress = ( options . compress || options . compress === false ) ? options . compress : redisOptions . compress ;
187- if ( compress ) {
188- options . compress = ( compress === true ) ? compressDefault : compress ;
189- key = new Buffer ( key ) ;
190- }
189+ cb = cb ? cb : ( err , result ) => err ? reject ( err ) : resolve ( result )
191190
192- connect ( function ( err , conn ) {
193- if ( err ) {
194- return cb && cb ( err ) ;
191+ var compress = ( options . compress || options . compress === false ) ? options . compress : redisOptions . compress ;
192+ if ( compress ) {
193+ options . compress = ( compress === true ) ? compressDefault : compress ;
194+ key = new Buffer ( key ) ;
195195 }
196196
197- conn . get ( key , handleResponse ( conn , cb , options ) ) ;
197+ connect ( function ( err , conn ) {
198+ if ( err ) {
199+ return cb ( err ) ;
200+ }
201+
202+ conn . get ( key , handleResponse ( conn , cb , options ) ) ;
203+ } ) ;
198204 } ) ;
199205 } ;
200206
@@ -207,50 +213,54 @@ function redisStore(args) {
207213 * @param {Object } options.ttl - The ttl value
208214 * @param {boolean|Object } options.compress - compression configuration
209215 * @param {Function } [cb] - A callback that returns a potential error, otherwise null
216+ * @returns {Promise }
210217 */
211218 self . set = function ( key , value , options , cb ) {
219+ return new Promise ( function ( resolve , reject ) {
220+ if ( typeof options === 'function' ) {
221+ cb = options ;
222+ options = { } ;
223+ }
212224
213- if ( typeof options === 'function' ) {
214- cb = options ;
215- options = { } ;
216- }
217-
218- if ( ! self . isCacheableValue ( value ) ) {
219- return cb ( new Error ( 'value cannot be ' + value ) ) ;
220- }
225+ cb = cb ? cb : ( err , result ) => err ? reject ( err ) : resolve ( result )
221226
222- options = options || { } ;
227+ if ( ! self . isCacheableValue ( value ) ) {
228+ return cb ( new Error ( 'value cannot be ' + value ) ) ;
229+ }
223230
224- var ttl = ( options . ttl || options . ttl === 0 ) ? options . ttl : redisOptions . ttl ;
225- var compress = ( options . compress || options . compress === false ) ? options . compress : redisOptions . compress ;
226- if ( compress === true ) {
227- compress = compressDefault ;
228- }
231+ options = options || { } ;
229232
230- connect ( function ( err , conn ) {
231- if ( err ) {
232- return cb && cb ( err ) ;
233+ var ttl = ( options . ttl || options . ttl === 0 ) ? options . ttl : redisOptions . ttl ;
234+ var compress = ( options . compress || options . compress === false ) ? options . compress : redisOptions . compress ;
235+ if ( compress === true ) {
236+ compress = compressDefault ;
233237 }
234- var val = JSON . stringify ( value ) || '"undefined"' ;
235238
236- // Refactored to remove duplicate code.
237- function persist ( pErr , pVal ) {
238- if ( pErr ) {
239- return cb && cb ( pErr ) ;
239+ connect ( function ( err , conn ) {
240+ if ( err ) {
241+ return cb ( err ) ;
240242 }
243+ var val = JSON . stringify ( value ) || '"undefined"' ;
241244
242- if ( ttl ) {
243- conn . setex ( key , ttl , pVal , handleResponse ( conn , cb ) ) ;
244- } else {
245- conn . set ( key , pVal , handleResponse ( conn , cb ) ) ;
245+ // Refactored to remove duplicate code.
246+ function persist ( pErr , pVal ) {
247+ if ( pErr ) {
248+ return cb ( pErr ) ;
249+ }
250+
251+ if ( ttl ) {
252+ conn . setex ( key , ttl , pVal , handleResponse ( conn , cb ) ) ;
253+ } else {
254+ conn . set ( key , pVal , handleResponse ( conn , cb ) ) ;
255+ }
246256 }
247- }
248257
249- if ( compress ) {
250- zlib . gzip ( val , compress . params || { } , persist ) ;
251- } else {
252- persist ( null , val ) ;
253- }
258+ if ( compress ) {
259+ zlib . gzip ( val , compress . params || { } , persist ) ;
260+ } else {
261+ persist ( null , val ) ;
262+ }
263+ } ) ;
254264 } ) ;
255265 } ;
256266
0 commit comments