Skip to content

Commit

Permalink
feat: Use options pattern in constructor (#468) Thanks to @jcw-!
Browse files Browse the repository at this point in the history
  • Loading branch information
jcw- authored Mar 3, 2020
1 parent 2484eb3 commit ff20167
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ class PreAggregations {
this.logger = logger;
this.queryCache = queryCache;
this.cacheDriver = options.cacheAndQueueDriver === 'redis' ?
new RedisCacheDriver(options.redisPool) :
new RedisCacheDriver({ pool: options.redisPool }) :
new LocalCacheDriver();
this.externalDriverFactory = options.externalDriverFactory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class QueryCache {
this.externalDriverFactory = options.externalDriverFactory;
this.logger = logger;
this.cacheDriver = options.cacheAndQueueDriver === 'redis' ?
new RedisCacheDriver(options.redisPool) :
new RedisCacheDriver({ pool: options.redisPool }) :
new LocalCacheDriver();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class RedisCacheDriver {
constructor(pool) {
constructor({ pool }) {
this.redisPool = pool;
}

async getClient() {
return await this.redisPool.getClient();
return this.redisPool.getClient();
}

async get(key) {
Expand Down
11 changes: 6 additions & 5 deletions packages/cubejs-query-orchestrator/orchestrator/RedisPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ const genericPool = require('generic-pool');
const createRedisClient = require('./RedisFactory');

class RedisPool {
constructor(poolMin, poolMax, createClient, destroyClient) {
constructor(options) {
options = options || {};
const defaultMin = process.env.CUBEJS_REDIS_POOL_MIN ? parseInt(process.env.CUBEJS_REDIS_POOL_MIN, 10) : 2;
const defaultMax = process.env.CUBEJS_REDIS_POOL_MAX ? parseInt(process.env.CUBEJS_REDIS_POOL_MAX, 10) : 1000;
const min = (typeof poolMin !== 'undefined') ? poolMin : defaultMin;
const max = (typeof poolMax !== 'undefined') ? poolMax : defaultMax;
const create = createClient || (() => createRedisClient(process.env.REDIS_URL));
const destroy = destroyClient || (client => client.end(true));
const min = (typeof options.poolMin !== 'undefined') ? options.poolMin : defaultMin;
const max = (typeof options.poolMax !== 'undefined') ? options.poolMax : defaultMax;
const create = options.createClient || (() => createRedisClient(process.env.REDIS_URL));
const destroy = options.destroyClient || (client => client.end(true));
const opts = {
min,
max,
Expand Down
2 changes: 1 addition & 1 deletion packages/cubejs-query-orchestrator/test/QueryQueue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ const QueryQueueTest = (name, options) => {

QueryQueueTest('Local');
QueryQueueTest('RedisPool', { cacheAndQueueDriver: 'redis', redisPool: new RedisPool() });
QueryQueueTest('RedisNoPool', { cacheAndQueueDriver: 'redis', redisPool: new RedisPool(0, 0) });
QueryQueueTest('RedisNoPool', { cacheAndQueueDriver: 'redis', redisPool: new RedisPool({ poolMin: 0, poolMax: 0 }) });

0 comments on commit ff20167

Please sign in to comment.