Skip to content

Commit 9f80dd6

Browse files
committed
feat: add TLS/SSL support with Redis client configuration options
1 parent b022e6e commit 9f80dd6

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

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

132159
## Consistency of Redis and this caching implementation
133160

src/RedisStringsHandler.ts

Lines changed: 14 additions & 1 deletion
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({
126137
...(database !== 0 ? { database } : {}),
127138
url: redis_url,
139+
...(socketOptions ? { socket: socketOptions } : {}),
140+
...(clientOptions || {}),
128141
});
129142

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

0 commit comments

Comments
 (0)