Skip to content

Commit

Permalink
Add support for node-redis v4.0.0 and newer
Browse files Browse the repository at this point in the history
- Fixes #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
  • Loading branch information
aardvarkk committed Dec 7, 2021
1 parent 54cf660 commit 09122aa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
3 changes: 1 addition & 2 deletions docs/caching.md
Expand Up @@ -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:

Expand Down
11 changes: 7 additions & 4 deletions src/cache/RedisQueryResultCache.ts
Expand Up @@ -50,10 +50,13 @@ export class RedisQueryResultCache implements QueryResultCache {
async connect(): Promise<void> {
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();
const isV4 = "v4" in this.client;
this.client = this.redis.createClient({
...cacheOptions?.options,
...(isV4 && { legacyMode: true })
});
if (isV4) {
await this.client.connect();
}
} else if (this.clientType === "ioredis") {
if (cacheOptions && cacheOptions.port) {
Expand Down

0 comments on commit 09122aa

Please sign in to comment.