Skip to content

Commit 4a2144a

Browse files
authored
Merge pull request #25 from trieb-work/allow-redis-client-settings
feat: add TLS/SSL support with Redis client configuration options
2 parents 827864b + 90db505 commit 4a2144a

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,33 @@ A working example of above can be found in the `test/integration/next-app-custom
132132
| inMemoryCachingTime | Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely. | `10000` |
133133
| defaultStaleAge | Default stale age in seconds for cached items | `1209600` (14 days) |
134134
| estimateExpireAge | Function to calculate expire age (redis TTL value) from stale age | Production: `staleAge * 2`<br> Other: `staleAge * 1.2` |
135+
| socketOptions | Redis client socket options for TLS/SSL configuration (e.g., `{ tls: true, rejectUnauthorized: false }`) | `undefined` |
136+
| clientOptions | Additional Redis client options (e.g., username, password) | `undefined` |
137+
138+
## TLS Configuration
139+
140+
To connect to Redis using TLS/SSL (e.g., when using Redis over `rediss://` URLs), you can configure the socket options. Here's an example:
141+
142+
```javascript
143+
const { RedisStringsHandler } = require('@trieb.work/nextjs-turbo-redis-cache');
144+
145+
let cachedHandler;
146+
147+
module.exports = class CustomizedCacheHandler {
148+
constructor() {
149+
if (!cachedHandler) {
150+
cachedHandler = new RedisStringsHandler({
151+
redisUrl: 'rediss://your-redis-host:6380', // Note the rediss:// protocol
152+
socketOptions: {
153+
tls: true,
154+
rejectUnauthorized: false, // Only use this if you want to skip certificate validation
155+
},
156+
});
157+
}
158+
}
159+
// ... rest of the handler implementation
160+
};
161+
```
135162

136163
## Consistency of Redis and this caching implementation
137164

src/RedisStringsHandler.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { commandOptions, createClient } from 'redis';
1+
import { commandOptions, createClient, RedisClientOptions } from 'redis';
22
import { SyncedMap } from './SyncedMap';
33
import { DeduplicatedRequestHandler } from './DeduplicatedRequestHandler';
44
import { debug } from './utils/debug';
@@ -60,6 +60,14 @@ export type CreateRedisStringsHandlerOptions = {
6060
* @default Production: staleAge * 2, Other: staleAge * 1.2
6161
*/
6262
estimateExpireAge?: (staleAge: number) => number;
63+
/** Additional Redis client socket options
64+
* @example { tls: true, rejectUnauthorized: false }
65+
*/
66+
socketOptions?: RedisClientOptions['socket'];
67+
/** Additional Redis client options to be passed directly to createClient
68+
* @example { username: 'user', password: 'pass' }
69+
*/
70+
clientOptions?: Omit<RedisClientOptions, 'url' | 'database' | 'socket'>;
6371
};
6472

6573
// Identifier prefix used by Next.js to mark automatically generated cache tags
@@ -113,6 +121,8 @@ export default class RedisStringsHandler {
113121
defaultStaleAge = 60 * 60 * 24 * 14,
114122
estimateExpireAge = (staleAge) =>
115123
process.env.VERCEL_ENV === 'production' ? staleAge * 2 : staleAge * 1.2,
124+
socketOptions,
125+
clientOptions,
116126
}: CreateRedisStringsHandlerOptions) {
117127
this.keyPrefix = keyPrefix;
118128
this.timeoutMs = timeoutMs;
@@ -122,9 +132,12 @@ export default class RedisStringsHandler {
122132
this.estimateExpireAge = estimateExpireAge;
123133

124134
try {
135+
// Create Redis client with properly typed configuration
125136
this.client = createClient({
126-
...(database !== 0 ? { database } : {}),
127137
url: redisUrl,
138+
...(database !== 0 ? { database } : {}),
139+
...(socketOptions ? { socket: socketOptions } : {}),
140+
...(clientOptions || {}),
128141
});
129142

130143
this.client.on('error', (error) => {

0 commit comments

Comments
 (0)