Skip to content

Commit

Permalink
feat(redis): make lazyConnect optional in cache-redis plugin (#5963)
Browse files Browse the repository at this point in the history
* feat(redis): make lazyConnect optional in cache-redis plugin

* test: add tests to constructor

* chore: add changeset

* Update thin-pears-beam.md

---------

Co-authored-by: Arda TANRIKULU <[email protected]>
  • Loading branch information
BabakScript and ardatan authored Sep 14, 2023
1 parent b895cb2 commit 625e5d7
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/thin-pears-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-mesh/cache-redis': patch
'@graphql-mesh/types': patch
---

Add lazyConnect to cache-redis
12 changes: 7 additions & 5 deletions packages/cache/redis/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ export default class RedisCache<V = string> implements KeyValueCache<V> {
private client: Redis;

constructor(options: YamlConfig.Cache['redis'] & { pubsub: MeshPubSub; logger: Logger }) {
const lazyConnect = options.lazyConnect !== false;

if (options.url) {
const redisUrl = new URL(interpolateStrWithEnv(options.url));

redisUrl.searchParams.set('lazyConnect', 'true');
redisUrl.searchParams.set('enableAutoPipelining', 'true');
redisUrl.searchParams.set('enableOfflineQueue', 'true');

if (!['redis:', 'rediss:'].includes(redisUrl.protocol)) {
throw new Error('Redis URL must use either redis:// or rediss://');
}

redisUrl.searchParams.set('lazyConnect', lazyConnect.toString());
redisUrl.searchParams.set('enableAutoPipelining', 'true');
redisUrl.searchParams.set('enableOfflineQueue', 'true');

options.logger.debug(`Connecting to Redis at ${redisUrl.toString()}`);
this.client = new Redis(redisUrl?.toString());
} else {
Expand All @@ -41,7 +43,7 @@ export default class RedisCache<V = string> implements KeyValueCache<V> {
host: parsedHost,
port: parseInt(parsedPort),
password: parsedPassword,
lazyConnect: true,
lazyConnect,
enableAutoPipelining: true,
enableOfflineQueue: true,
});
Expand Down
21 changes: 21 additions & 0 deletions packages/cache/redis/test/cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ describe('redis', () => {
);
});

it('passes configuration to redis client with default options, url and lazyConnect case', async () => {
new RedisCache({ url: 'redis://password@localhost:6379', lazyConnect: false, pubsub, logger });

expect(Redis).toHaveBeenCalledWith(
'redis://password@localhost:6379?lazyConnect=false&enableAutoPipelining=true&enableOfflineQueue=true',
);
});

it('passes configuration to redis client with default options, host, port & password case', async () => {
new RedisCache({ port: '6379', password: 'testpassword', host: 'localhost', pubsub, logger });

Expand All @@ -38,6 +46,19 @@ describe('redis', () => {
});
});

it('passes configuration to redis client with default options, host, port, password & lazyConnect case', async () => {
new RedisCache({ port: '6379', password: 'testpassword', host: 'localhost', lazyConnect: false, pubsub, logger });

expect(Redis).toHaveBeenCalledWith({
enableAutoPipelining: true,
enableOfflineQueue: true,
host: 'localhost',
lazyConnect: false,
password: 'testpassword',
port: 6379,
});
});

it('prefers url over specific properties if both given', () => {
new RedisCache({
url: 'redis://localhost:6379',
Expand Down
6 changes: 6 additions & 0 deletions packages/cache/redis/yaml-config.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ type RedisConfig @md {
port: String
password: String
url: String
"""
Flag to indicate lazyConnect value for Redis client.
@default: true
"""
lazyConnect: Boolean
}
3 changes: 3 additions & 0 deletions packages/types/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
},
"url": {
"type": "string"
},
"lazyConnect": {
"type": "boolean"
}
}
},
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,7 @@ export interface RedisConfig {
port?: string;
password?: string;
url?: string;
lazyConnect?: boolean;
}
export interface PubSubConfig {
name: string;
Expand Down
3 changes: 2 additions & 1 deletion website/src/generated-markdown/RedisConfig.generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
* `host` (type: `String`)
* `port` (type: `String`)
* `password` (type: `String`)
* `url` (type: `String`)
* `url` (type: `String`)
* `lazyConnect` (type: `Boolean`)

0 comments on commit 625e5d7

Please sign in to comment.