@@ -29,6 +29,7 @@ function redisStore(args) {
29
29
/* istanbul ignore next */
30
30
var redisOptions = getFromUrl ( args ) || args || { } ;
31
31
var poolSettings = redisOptions ;
32
+ var Promise = args . promiseDependency || global . Promise ;
32
33
33
34
redisOptions . host = redisOptions . host || '127.0.0.1' ;
34
35
redisOptions . port = redisOptions . port || 6379 ;
@@ -174,27 +175,32 @@ function redisStore(args) {
174
175
* @param {Object } [options] - The options (optional)
175
176
* @param {boolean|Object } options.compress - compression configuration
176
177
* @param {Function } cb - A callback that returns a potential error and the response
178
+ * @returns {Promise }
177
179
*/
178
180
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 ;
185
188
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 )
191
190
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 ) ;
195
195
}
196
196
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
+ } ) ;
198
204
} ) ;
199
205
} ;
200
206
@@ -207,50 +213,54 @@ function redisStore(args) {
207
213
* @param {Object } options.ttl - The ttl value
208
214
* @param {boolean|Object } options.compress - compression configuration
209
215
* @param {Function } [cb] - A callback that returns a potential error, otherwise null
216
+ * @returns {Promise }
210
217
*/
211
218
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
+ }
212
224
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 )
221
226
222
- options = options || { } ;
227
+ if ( ! self . isCacheableValue ( value ) ) {
228
+ return cb ( new Error ( 'value cannot be ' + value ) ) ;
229
+ }
223
230
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 || { } ;
229
232
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 ;
233
237
}
234
- var val = JSON . stringify ( value ) || '"undefined"' ;
235
238
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 ) ;
240
242
}
243
+ var val = JSON . stringify ( value ) || '"undefined"' ;
241
244
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
+ }
246
256
}
247
- }
248
257
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
+ } ) ;
254
264
} ) ;
255
265
} ;
256
266
0 commit comments