From 887776aaf07d4e3cdb75867e8c6fc13131610c37 Mon Sep 17 00:00:00 2001 From: Ian Clarkson Date: Fri, 3 Dec 2021 12:32:40 -0800 Subject: [PATCH] Add support for node-redis v4.0.0 and newer - Fixes https://github.com/typeorm/typeorm/issues/8420 - v4.0.0 and newer of node-redis will no longer auto-connect, so we need to add that step - For backwards compatibility, we check for the existence of a "v4" property so we don't try to call `connect` on older versions of the client - A `legacyMode` flag exists in the new v4.0.0 of node-redis to keep a similar calling style without having to change any code - Note that the legacyMode flag is **required** to be passed if the calling code isn't going to be changed --- docs/caching.md | 3 +-- src/cache/RedisQueryResultCache.ts | 10 ++++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/docs/caching.md b/docs/caching.md index 2a193b1290..9d7e87d4e7 100644 --- a/docs/caching.md +++ b/docs/caching.md @@ -149,8 +149,7 @@ Example: } ``` -"options" can be [node_redis specific options](https://github.com/NodeRedis/node_redis#options-object-properties) or [ioredis specific options](https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options) depending on what type you're using. - +"options" can be [node_redis specific options](https://github.com/redis/node-redis/blob/master/docs/client-configuration.md) or [ioredis specific options](https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options) depending on what type you're using. In case you want to connect to a redis-cluster using IORedis's cluster functionality, you can do that as well by doing the following: diff --git a/src/cache/RedisQueryResultCache.ts b/src/cache/RedisQueryResultCache.ts index 8c60e084fc..8c0d5addca 100644 --- a/src/cache/RedisQueryResultCache.ts +++ b/src/cache/RedisQueryResultCache.ts @@ -50,10 +50,12 @@ export class RedisQueryResultCache implements QueryResultCache { async connect(): Promise { const cacheOptions: any = this.connection.options.cache; if (this.clientType === "redis") { - if (cacheOptions && cacheOptions.options) { - this.client = this.redis.createClient(cacheOptions.options); - } else { - this.client = this.redis.createClient(); + this.client = this.redis.createClient({ + ...cacheOptions?.options, + legacyMode: true + }); + if ("connect" in this.client) { + await this.client.connect(); } } else if (this.clientType === "ioredis") { if (cacheOptions && cacheOptions.port) {