@@ -5,7 +5,11 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
55var Redis = _interopDefault ( require ( 'redis' ) ) ;
66
77var redisStore = function redisStore ( ) {
8- var redisCache = Redis . createClient . apply ( Redis , arguments ) ;
8+ for ( var _len = arguments . length , args = Array ( _len ) , _key = 0 ; _key < _len ; _key ++ ) {
9+ args [ _key ] = arguments [ _key ] ;
10+ }
11+
12+ var redisCache = Redis . createClient . apply ( Redis , args ) ;
913 var storeArgs = redisCache . options ;
1014
1115 return {
@@ -37,6 +41,68 @@ var redisStore = function redisStore() {
3741 }
3842 } ) ;
3943 } ,
44+ mset : function mset ( ) {
45+ for ( var _len2 = arguments . length , args = Array ( _len2 ) , _key2 = 0 ; _key2 < _len2 ; _key2 ++ ) {
46+ args [ _key2 ] = arguments [ _key2 ] ;
47+ }
48+
49+ var _this = this ;
50+
51+ return new Promise ( function ( resolve , reject ) {
52+ var cb = void 0 ;
53+ var options = { } ;
54+
55+ if ( typeof args [ args . length - 1 ] === 'function' ) {
56+ cb = args . pop ( ) ;
57+ }
58+
59+ if ( args [ args . length - 1 ] instanceof Object && args [ args . length - 1 ] . constructor === Object ) {
60+ options = args . pop ( ) ;
61+ }
62+
63+ if ( ! cb ) {
64+ cb = function cb ( err , result ) {
65+ return err ? reject ( err ) : resolve ( result ) ;
66+ } ;
67+ }
68+
69+ var ttl = options . ttl || options . ttl === 0 ? options . ttl : storeArgs . ttl ;
70+
71+ var multi = void 0 ;
72+ if ( ttl ) {
73+ multi = redisCache . multi ( ) ;
74+ }
75+
76+ var length = args . length ;
77+ var key = void 0 ;
78+ var value = void 0 ;
79+ var parsed = [ ] ;
80+ for ( var i = 0 ; i < length ; i += 2 ) {
81+ key = args [ i ] ;
82+ value = args [ i + 1 ] ;
83+
84+ /**
85+ * Make sure the value is cacheable
86+ */
87+ if ( ! _this . isCacheableValue ( value ) ) {
88+ return cb ( new Error ( 'value cannot be ' + value ) ) ;
89+ }
90+
91+ value = JSON . stringify ( value ) || '"undefined"' ;
92+ parsed . push . apply ( parsed , [ key , value ] ) ;
93+
94+ if ( ttl ) {
95+ multi . setex ( key , ttl , value ) ;
96+ }
97+ }
98+
99+ if ( ttl ) {
100+ multi . exec ( handleResponse ( cb ) ) ;
101+ } else {
102+ redisCache . mset . apply ( redisCache , [ ] . concat ( parsed , [ handleResponse ( cb ) ] ) ) ;
103+ }
104+ } ) ;
105+ } ,
40106 get : function get ( key , options , cb ) {
41107 return new Promise ( function ( resolve , reject ) {
42108 if ( typeof options === 'function' ) {
@@ -52,10 +118,47 @@ var redisStore = function redisStore() {
52118 redisCache . get ( key , handleResponse ( cb , { parse : true } ) ) ;
53119 } ) ;
54120 } ,
55- del : function del ( key , options , cb ) {
121+ mget : function mget ( ) {
122+ for ( var _len3 = arguments . length , args = Array ( _len3 ) , _key3 = 0 ; _key3 < _len3 ; _key3 ++ ) {
123+ args [ _key3 ] = arguments [ _key3 ] ;
124+ }
125+
56126 return new Promise ( function ( resolve , reject ) {
57- if ( typeof options === 'function' ) {
58- cb = options ;
127+ var cb = void 0 ;
128+ var options = { } ;
129+
130+ if ( typeof args [ args . length - 1 ] === 'function' ) {
131+ cb = args . pop ( ) ;
132+ }
133+
134+ if ( args [ args . length - 1 ] instanceof Object && args [ args . length - 1 ] . constructor === Object ) {
135+ options = args . pop ( ) ;
136+ }
137+
138+ if ( ! cb ) {
139+ cb = function cb ( err , result ) {
140+ return err ? reject ( err ) : resolve ( result ) ;
141+ } ;
142+ }
143+
144+ redisCache . mget . apply ( redisCache , [ ] . concat ( args , [ handleResponse ( cb , { parse : true } ) ] ) ) ;
145+ } ) ;
146+ } ,
147+ del : function del ( ) {
148+ for ( var _len4 = arguments . length , args = Array ( _len4 ) , _key4 = 0 ; _key4 < _len4 ; _key4 ++ ) {
149+ args [ _key4 ] = arguments [ _key4 ] ;
150+ }
151+
152+ return new Promise ( function ( resolve , reject ) {
153+ var cb = void 0 ;
154+ var options = { } ;
155+
156+ if ( typeof args [ args . length - 1 ] === 'function' ) {
157+ cb = args . pop ( ) ;
158+ }
159+
160+ if ( args [ args . length - 1 ] instanceof Object && args [ args . length - 1 ] . constructor === Object ) {
161+ options = args . pop ( ) ;
59162 }
60163
61164 if ( ! cb ) {
@@ -64,7 +167,7 @@ var redisStore = function redisStore() {
64167 } ;
65168 }
66169
67- redisCache . del ( key , handleResponse ( cb ) ) ;
170+ redisCache . del . apply ( redisCache , [ ] . concat ( args , [ handleResponse ( cb ) ] ) ) ;
68171 } ) ;
69172 } ,
70173 reset : function reset ( cb ) {
@@ -120,11 +223,21 @@ function handleResponse(cb) {
120223 }
121224
122225 if ( opts . parse ) {
123- try {
124- result = JSON . parse ( result ) ;
125- } catch ( e ) {
126- return cb && cb ( e ) ;
226+ var isMultiple = Array . isArray ( result ) ;
227+ if ( ! isMultiple ) {
228+ result = [ result ] ;
127229 }
230+
231+ result = result . map ( function ( _result ) {
232+ try {
233+ _result = JSON . parse ( _result ) ;
234+ } catch ( e ) {
235+ return cb && cb ( e ) ;
236+ }
237+ return _result ;
238+ } ) ;
239+
240+ result = isMultiple ? result : result [ 0 ] ;
128241 }
129242
130243 return cb && cb ( null , result ) ;
0 commit comments