Skip to content

Commit

Permalink
Merge pull request typeorm#3364 from ignacioblit/enchancement/ioredis…
Browse files Browse the repository at this point in the history
…-cluster-support

Enchancement - Add support for ioredis Cluster object initialization
  • Loading branch information
pleerock committed Jan 7, 2019
2 parents 5a651c0 + ca5a833 commit 3bd211c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/cache/QueryResultCacheFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ export class QueryResultCacheFactory {
if ((this.connection.options.cache as any).type === "ioredis")
return new RedisQueryResultCache(this.connection, "ioredis");

if ((this.connection.options.cache as any).type === "ioredis/cluster")
return new RedisQueryResultCache(this.connection, "ioredis/cluster");

return new DbQueryResultCache(this.connection);
}


}
11 changes: 9 additions & 2 deletions src/cache/RedisQueryResultCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ export class RedisQueryResultCache implements QueryResultCache {
/**
* Type of the Redis Client (redis or ioredis).
*/
protected clientType: "redis" | "ioredis";
protected clientType: "redis" | "ioredis" | "ioredis/cluster";

// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------

constructor(protected connection: Connection, clientType: "redis" | "ioredis") {
constructor(protected connection: Connection, clientType: "redis" | "ioredis" | "ioredis/cluster") {
this.clientType = clientType;
this.redis = this.loadRedis();
}
Expand Down Expand Up @@ -61,6 +61,13 @@ export class RedisQueryResultCache implements QueryResultCache {
this.client = new this.redis();
}

} else if (this.clientType === "ioredis/cluster") {
if (cacheOptions && cacheOptions.options) {
this.client = new this.redis.Cluster(cacheOptions.options);
} else {
throw new Error(`Options required for ${this.clientType}.`);
}

}
}

Expand Down
2 changes: 1 addition & 1 deletion src/connection/BaseConnectionOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export interface BaseConnectionOptions {
* - "database" means cached values will be stored in the separate table in database. This is default value.
* - "redis" means cached values will be stored inside redis. You must provide redis connection options.
*/
readonly type?: "database"|"redis"; // todo: add mongodb and other cache providers as well in the future
readonly type?: "database" | "redis" | "ioredis" | "ioredis/cluster"; // todo: add mongodb and other cache providers as well in the future

/**
* Used to provide redis connection options.
Expand Down
1 change: 1 addition & 0 deletions src/platform/PlatformTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class PlatformTools {
* ioredis
*/
case "ioredis":
case "ioredis/cluster":
return require("ioredis");

/**
Expand Down

0 comments on commit 3bd211c

Please sign in to comment.