Skip to content

Commit 81a2565

Browse files
authored
Support Redis Cluster connection (#47)
* Cluster setup * Added cluster support. Exported proxy and abort signal helper * Lint and packages * Updated readme * Rename and cleanup * Minor adjustments and readme update
1 parent 44f336e commit 81a2565

16 files changed

+5653
-1692
lines changed

README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,48 @@ A Redis-based handler for key- and tag-based caching. Compared to the original i
116116
import createRedisHandler from "@fortedigital/nextjs-cache-handler/redis-strings";
117117

118118
const redisHandler = await createRedisHandler({
119-
client,
119+
client: createClient({
120+
url: process.env.REDIS_URL,
121+
}),
120122
keyPrefix: "myApp:",
121123
sharedTagsKey: "myTags",
122124
sharedTagsTtlKey: "myTagTtls",
123125
});
124126
```
125127

128+
#### Redis Cluster (Experimental)
129+
130+
```js
131+
import { createCluster } from "@redis/client";
132+
import createRedisHandler from "@fortedigital/nextjs-cache-handler/redis-strings";
133+
import { withAdapter } from "@fortedigital/nextjs-cache-handler/cluster/adapter";
134+
135+
const { hostname: redisHostName } = new URL(process.env.REDIS_URL);
136+
redis = withAdapter(
137+
createCluster({
138+
rootNodes: [{ url: process.env.REDIS_URL }],
139+
140+
// optional if you use TLS and need to resolve shards' ip to proper hostname
141+
nodeAddressMap(address) {
142+
const [_, port] = address.split(":");
143+
144+
return {
145+
host: redisHostName,
146+
port: Number(port),
147+
};
148+
},
149+
})
150+
);
151+
152+
// after using withAdapter you can use redis cluster instance as parameter for createRedisHandler
153+
const redisCacheHandler = createRedisHandler({
154+
client: redis,
155+
keyPrefix: CACHE_PREFIX,
156+
});
157+
```
158+
159+
**Note:** Redis Cluster support is currently experimental and may have limitations or unexpected bugs. Use it with caution.
160+
126161
---
127162

128163
### `local-lru`
@@ -186,7 +221,8 @@ Next 15 decided to change types of some properties from String to Buffer which c
186221
```js
187222
import createBufferStringDecoratorHandler from "@fortedigital/nextjs-cache-handler/buffer-string-decorator";
188223

189-
const bufferStringDecorator = createBufferStringDecoratorHandler(redisCacheHandler);
224+
const bufferStringDecorator =
225+
createBufferStringDecoratorHandler(redisCacheHandler);
190226
```
191227
192228
## Examples

examples/redis-minimal/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"@types/node": "^24",
2626
"@types/react": "^19",
2727
"@types/react-dom": "^19",
28-
"eslint": "^9",
28+
"eslint": "^9",
2929
"eslint-config-next": "15.4.3",
3030
"tailwindcss": "^4",
3131
"typescript": "^5"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { createDefaultPreset } from "ts-jest";
2+
3+
const tsJestTransformCfg = createDefaultPreset().transform;
4+
5+
/** @type {import("jest").Config} **/
6+
export default {
7+
testEnvironment: "node",
8+
transform: {
9+
...tsJestTransformCfg,
10+
},
11+
};

0 commit comments

Comments
 (0)