1- const Redis = require ( 'ioredis' )
2- const InMemoryRedis = require ( 'ioredis-mock' )
1+ const createRedisClient = require ( './redis/create-client' )
2+ const InMemoryRedis = require ( 'redis-mock' )
3+ const { promisify } = require ( 'util' )
34
4- const { CI , NODE_ENV , REDIS_URL , REDIS_MAX_DB } = process . env
5+ const { CI , NODE_ENV , REDIS_URL } = process . env
56
67// Do not use real a Redis client for CI, tests, or if the REDIS_URL is not provided
78const useRealRedis = ! CI && NODE_ENV !== 'test' && ! ! REDIS_URL
89
9- // By default, every Redis instance supports database numbers 0 - 15
10- const redisMaxDb = REDIS_MAX_DB || 15
11-
12- // Enable better stack traces in non-production environments
13- const redisBaseOptions = {
14- showFriendlyErrorStack : NODE_ENV !== 'production'
15- }
16-
1710class RedisAccessor {
18- constructor ( { databaseNumber = 0 , prefix = null , allowSetFailures = false } = { } ) {
19- if ( ! Number . isInteger ( databaseNumber ) || databaseNumber < 0 || databaseNumber > redisMaxDb ) {
20- throw new TypeError (
21- `Redis database number must be an integer between 0 and ${ redisMaxDb } but was: ${ JSON . stringify ( databaseNumber ) } `
22- )
23- }
24-
11+ constructor ( { databaseNumber = 0 , prefix = null , allowSetFailures = false , name = null } = { } ) {
2512 const redisClient = useRealRedis
26- ? new Redis ( REDIS_URL , {
27- ... redisBaseOptions ,
13+ ? createRedisClient ( {
14+ url : REDIS_URL ,
2815 db : databaseNumber ,
29-
30- // Only add this configuration for TLS-enabled REDIS_URL values.
31- // Otherwise, it breaks for local Redis instances without TLS enabled.
32- ...REDIS_URL . startsWith ( 'rediss://' ) && {
33- tls : {
34- // Required for production Heroku Redis
35- rejectUnauthorized : false
36- }
37- }
16+ name : name || 'redis-accessor'
3817 } )
39- : new InMemoryRedis ( )
18+ : InMemoryRedis . createClient ( )
4019
4120 this . _client = redisClient
4221
@@ -96,6 +75,7 @@ class RedisAccessor {
9675 }
9776
9877 async set ( key , value , options = { } ) {
78+ const setAsync = promisify ( this . _client . set ) . bind ( this . _client )
9979 const fullKey = this . prefix ( key )
10080
10181 if ( typeof value !== 'string' || ! value ) {
@@ -106,7 +86,7 @@ class RedisAccessor {
10686 const setArgs = this . constructor . translateSetArguments ( options )
10787
10888 try {
109- const result = await this . _client . set ( fullKey , value , ...setArgs )
89+ const result = await setAsync ( fullKey , value , ...setArgs )
11090 return result === 'OK'
11191 } catch ( err ) {
11292 const errorText = `Failed to set value in Redis.
@@ -124,14 +104,10 @@ Error: ${err.message}`
124104 }
125105
126106 async get ( key ) {
127- const value = await this . _client . get ( this . prefix ( key ) )
107+ const getAsync = promisify ( this . _client . get ) . bind ( this . _client )
108+ const value = await getAsync ( this . prefix ( key ) )
128109 return value
129110 }
130-
131- async exists ( key ) {
132- const result = await this . _client . exists ( this . prefix ( key ) )
133- return result === 1
134- }
135111}
136112
137113module . exports = RedisAccessor
0 commit comments