Skip to content

Commit

Permalink
Updated the redis client to allow for cluster.
Browse files Browse the repository at this point in the history
  • Loading branch information
rizen committed Oct 24, 2024
1 parent 0f2c244 commit e996d23
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"email-templates": "^11.1.1",
"ioredis": "^5.3.2",
"keyv": "^4.5.4",
"keyv-anyredis": "^3.3.0",
"lodash": "^4.17.21",
"md-editor-v3": "^4.13.2",
"mysql2": "^3.9.7",
Expand Down
1 change: 1 addition & 0 deletions ving/docs/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ outline: deep
* Updated the drizzle client to trap bad connection strings with an error message.
* Fixed a potential bug where someone enters a negative page number into the paginator.
* Fixed a bug in the Pager component introduced with the upgrade to PrimeVue 4 where the default page was 0 rather than 1.
* Updated the redis client to allow for cluster.

### 2024-10-01
* Added colin's patch for mysql pagination.
Expand Down
1 change: 1 addition & 0 deletions ving/docs/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Can also include username and password like this:
```bash
VING_REDIS="redis://user:pass@localhost:6379"
```
If you're using Redis Cluster, you can add `?cluster=yes` to the end of the URL.

Note that if you're hosting on AWS, all Redis instances there have TLS enabled by default, so the URL prefix will have 2 's' at the end like this: `rediss://`.

Expand Down
23 changes: 22 additions & 1 deletion ving/redis.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,28 @@ export const spawnRedis = () => {
if (!redisUrl) {
throw new Error('VING_REDIS environment variable is not set');
}
return new Redis(redisUrl, { maxRetriesPerRequest: null });
const redisConfig = new URL(redisUrl);
const redisNode = {
port: parseInt(redisConfig.port || 6379),
host: redisConfig.hostname,
};
const redisOptions = { maxRetriesPerRequest: null, keyPrefix: process.env.NODE_ENV };
if (redisConfig.password) {
redisOptions.password = redisConfig.password;
}
if (redisConfig.username) {
redisOptions.username = redisConfig.username;
}
if (redisConfig.protocol === 'rediss:') {
redisOptions.tls = {};
}
if (redisConfig.searchParams.get('cluster') === 'yes') {
return new Redis.Cluster([redisNode], {
slotsRefreshTimeout: 20000,
redisOptions,
});
}
return new Redis(redisNode.port, redisNode.host, redisOptions);
}

let redis = undefined;
Expand Down

0 comments on commit e996d23

Please sign in to comment.