@@ -2,68 +2,93 @@ const RedisPool = require("simple-redis-pool");
22
33const debug = require ( "debug" ) ( "simpleRedisStore" ) ;
44
5- var RedisStore = module . exports = function ( name , redisOptions , poolOptions ) {
6-
7- // set default pool options
8- poolOptions = Object . assign ( {
9- acquireTimeoutMillis : 50
10- } , poolOptions || { } ) ;
11-
12- this . name = name ;
13- this . redisOptions = redisOptions ;
14- this . poolOptions = poolOptions ;
15- this . pool = new RedisPool ( name , redisOptions , poolOptions ) ;
16- debug ( "Redis store created." , this . pool . status ( ) ) ;
17-
18- this . getName = this . pool . getName ;
19- this . getRedisDB = this . pool . getRedisDB ;
20- this . getPoolStatus = this . pool . status ;
21- } ;
225
23- RedisStore . prototype . executeCmd = function ( cmd ) {
24- return this . pool . acquire ( )
25- . then ( conn => cmd ( conn )
26- . then ( result => {
27- this . pool . release ( conn ) ;
28- return result ;
29- } )
30- . catch ( err => {
31- this . pool . release ( conn ) ;
6+ const RedisStore = module . exports = function ( options ) {
7+
8+ this . name = options . name || `redisStore-${ Math . random ( ) . toString ( 36 ) . substr ( 2 , 10 ) } ` ;
9+ this . redisOptions = options . redisOptions ;
10+ this . poolOptions = Object . assign ( {
11+ acquireTimeoutMillis : 200
12+ } , options . poolOptions || { } ) ;
13+ this . logger = require ( "./logger" ) ( options . logger ) ;
14+
15+ this . pool = null ;
16+ try {
17+ this . pool = new RedisPool ( {
18+ name : this . name ,
19+ redisOptions : this . redisOptions ,
20+ poolOptions : this . poolOptions ,
21+ logger : this . logger
22+ } ) ;
23+
24+ this . ping ( )
25+ . then ( resp => {
26+ if ( resp === "PONG" ) {
27+ debug ( "Redis store created." , this . pool . status ( ) ) ;
28+ } else {
29+ debug ( "expected PONG but got" , resp ) ;
30+ const err = new Error ( "UNKNOWN_PING_RESPONSE" ) ;
31+ err . message = "expected PONG but got : " + resp ;
3232 throw err ;
33- } ) ) ;
33+ }
34+ } ) ;
35+
36+ } catch ( e ) {
37+ debug ( "Failed to create" , e ) ;
38+ this . pool = null ;
39+ throw e ;
40+ }
41+ } ;
42+
43+
44+ RedisStore . prototype . getName = function ( ) {
45+ return this . pool . getName ( ) ;
46+ } ;
47+
48+ RedisStore . prototype . getRedisOptions = function ( ) {
49+ return this . pool . getRedisOptions ( ) ;
50+ } ;
51+
52+ RedisStore . prototype . getPoolOptions = function ( ) {
53+ return this . pool . getPoolOptions ( ) ;
54+ } ;
55+
56+ // verify if the connection is successful or not
57+ RedisStore . prototype . ping = function ( ) {
58+ return this . pool . sendCommand ( "ping" ) ;
3459} ;
3560
3661RedisStore . prototype . get = function ( key ) {
37- return this . executeCmd ( conn => conn . getAsync ( key ) ) ;
62+ return this . pool . sendCommand ( "get" , key ) ;
3863} ;
3964
4065RedisStore . prototype . set = function ( key , value , ttlInSeconds ) {
4166
4267 if ( ttlInSeconds ) {
43- return this . executeCmd ( conn => conn . setexAsync ( key , ttlInSeconds , value ) ) ;
68+ return this . pool . sendCommand ( "setex" , [ key , ttlInSeconds , value ] ) ;
4469 } else {
45- return this . executeCmd ( conn => conn . setAsync ( key , value ) ) ;
70+ return this . pool . sendCommand ( "set" , [ key , value ] ) ;
4671 }
4772} ;
4873
4974RedisStore . prototype . del = function ( keys ) {
50- return this . executeCmd ( conn => conn . delAsync ( keys ) ) ;
75+ return this . pool . sendCommand ( "del" , keys ) ;
5176} ;
5277
5378RedisStore . prototype . expire = function ( key , ttlInSeconds ) {
54- return this . executeCmd ( conn => conn . expireAsync ( key , ttlInSeconds ) ) ;
79+ return this . pool . sendCommand ( "expire" , [ key , ttlInSeconds ] ) ;
5580} ;
5681
5782RedisStore . prototype . ttlInSeconds = function ( key ) {
58- return this . executeCmd ( conn => conn . ttlAsync ( key ) ) ;
83+ return this . pool . sendCommand ( "ttl" , key ) ;
5984} ;
6085
6186RedisStore . prototype . keys = function ( pattern ) {
6287 if ( ! pattern || pattern === "" ) {
6388 pattern = "*" ;
6489 }
6590
66- return this . executeCmd ( conn => conn . keysAsync ( pattern ) ) ;
91+ return this . pool . sendCommand ( "keys" , pattern ) ;
6792} ;
6893
6994RedisStore . prototype . deleteAll = function ( pattern ) {
0 commit comments