Skip to content

Commit bf97d25

Browse files
committed
refactor: 🔨 simplify the redis cache factory method
1 parent 976abe1 commit bf97d25

3 files changed

Lines changed: 29 additions & 18 deletions

File tree

src/component/connector/mongoose.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module.exports = class MongooseStore {
7474
try {
7575
debug("Connecting to MongoDB...");
7676

77-
this.#db = await mongoose.createConnection(dsn, {}).asPromise();
77+
this.#db = mongoose.createConnection(dsn, {});
7878

7979
debug("MongoDB connection established");
8080

@@ -113,16 +113,14 @@ module.exports = class MongooseStore {
113113
}
114114

115115
connected() {
116-
return this.#db?.readyState === MongooseStore.readyStates.connected;
117-
//return mongoose.connection.readyState === MongooseStore.readyStates.connected;
116+
return mongoose.connection.readyState === MongooseStore.readyStates.connected;
118117

119118
// Ready states:
120119
// eady states being: 0: disconnected 1: connected 2: connecting 3: disconnecting
121120
}
122121

123122
connecting() {
124-
return this.#db?.readyState === MongooseStore.readyStates.connecting;
125-
//return mongoose.connection.readyState === MongooseStore.readyStates.connecting;
123+
return mongoose.connection.readyState === MongooseStore.readyStates.connecting;
126124
}
127125

128126
getClient() {

src/factory/cache/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ module.exports = class CacheFactory {
3636
);
3737
}
3838

39-
if(driver === "redis") {
39+
// We no longer test for redis config because we no longer use the
40+
// config.credentials for redis. Instead, we have flattened everything in
41+
// config.credentials into the config object itself.
42+
// We don't test for host and/or port because redis tries to connect to
43+
// localhost and default port (6379) when these are not specified.
44+
// Also, since the default max attempts is 5,
45+
// the connection will stop attempting to connect to the localhost redis server
46+
// after 5 attempts if the user has not passed any config for the "redis" driver.
47+
/*if(driver === "redis") {
4048
if(!is.object(config.connection) && !is.object(config.credentials)) {
4149
const expectedProps = [
4250
"connection",
@@ -49,7 +57,9 @@ module.exports = class CacheFactory {
4957
`one of either properties: ${expectedProps.join(", ")}.`
5058
);
5159
}
52-
} else if(driver === "file" && !config.storagePath) {
60+
} else*/
61+
62+
if(driver === "file" && !config.storagePath) {
5363
throw new Error(
5464
errorPrefix +
5565
"The `config` parameter for the 'file' driver expects an object with a " +

src/factory/cache/redis-cache.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,33 @@ const RedisStore = require("../../component/connector/redis");
99
* An existing connection to a redis instance.
1010
* If this passed, it is used to connect to the Redis server.
1111
* Otherwise, we try to connect to a Redis server using the
12-
* options.credentials.
12+
* other options.
1313
* @param {Object} [options.credentials] (optional):
1414
* Credentials for establishing a connection to a Redis server.
15-
* @param {String} [options.credentials.host]: the server host
16-
* @param {Number} [options.credentials.port]: the server port
17-
* @param {String} [options.credentials.username]: the server username
18-
* @param {String} [options.credentials.password]: the server user password
19-
* @param {String} [options.credentials.db]: the database to connect to
20-
* @param {String} [options.credentials.url]: full DSN of the Redis server
21-
* If the [options.credentials.url] is set, it is used instead
15+
* @param {String} [options.host]: the server host
16+
* @param {Number} [options.port]: the server port
17+
* @param {String} [options.username]: the server username
18+
* @param {String} [options.password]: the server user password
19+
* @param {String} [options.db]: the database to connect to
20+
* @param {String} [options.url]: full DSN of the Redis server
21+
* If the [options.url] is set, it is used instead
2222
* and the other credential options are ignored.
23+
* @param {Boolean} [options.autoConnect]
24+
* @param {Boolean} [options.legacyMode]
25+
* @param {Boolean} [options.exitOnConnectionFailure]
2326
* @param {Number} [options.maxConnectionAttempts]: The maximum number of times
2427
* to attempt connecting before exiting.
2528
* @return {Object} with methods: set(), get(), unset(), contains(), and client().
2629
*/
2730
module.exports = function createRedisStore(options) {
28-
const { connection, credentials } = options || {};
31+
const { connection, ...rest } = options || {};
2932

3033
let store;
3134

3235
if(connection && typeof connection === "object") {
3336
store = connection;
34-
} else if(credentials && typeof credentials === "object") {
35-
const redisStore = new RedisStore(credentials);
37+
} else {
38+
const redisStore = new RedisStore({ ...rest });
3639
store = redisStore.getClient();
3740

3841
setTimeout(async function() {

0 commit comments

Comments
 (0)