Skip to content

Commit cea59a1

Browse files
committed
feat: add redis_url param
1 parent 21f63f9 commit cea59a1

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ node_modules
22
dist
33
.env
44
coverage
5-
.DS_Store
5+
.DS_Store
6+
.idea

README.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
[![npm version](https://img.shields.io/npm/v/@trieb.work/nextjs-turbo-redis-cache.svg)](https://www.npmjs.com/package/@trieb.work/nextjs-turbo-redis-cache)
44
![Turbo redis cache image](https://github.com/user-attachments/assets/98e0dfd9-f38a-42ad-a355-9843740cc2d6)
55

6-
7-
8-
96
The ultimate Redis caching solution for Next.js. Built for production-ready, large-scale projects, it delivers unparalleled performance and efficiency with features tailored for high-traffic applications. This package has been created after extensibly testing the @neshca package and finding several major issues with it.
107

118
Key Features:
@@ -31,18 +28,19 @@ Currently PPR, 'use cache', cacheLife and cacheTag are not tested. Use these ope
3128

3229
## Available Options (needs Option B of getting started)
3330

34-
| Option | Description | Default Value |
35-
| ---------------------- | ----------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------- |
36-
| database | Redis database number to use. Uses DB 0 for production, DB 1 otherwise | `process.env.VERCEL_ENV === 'production' ? 0 : 1` |
37-
| keyPrefix | Prefix added to all Redis keys | `process.env.VERCEL_URL \|\| 'UNDEFINED_URL_'` |
38-
| sharedTagsKey | Key used to store shared tags hash map in Redis | `'__sharedTags__'` |
39-
| timeoutMs | Timeout in milliseconds for Redis operations | `5000` |
40-
| revalidateTagQuerySize | Number of entries to query in one batch during full sync of shared tags hash map | `250` |
41-
| avgResyncIntervalMs | Average interval in milliseconds between tag map full re-syncs | `3600000` (1 hour) |
42-
| redisGetDeduplication | Enable deduplication of Redis get requests via internal in-memory cache. | `true` |
43-
| inMemoryCachingTime | Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely. | `10000` |
44-
| defaultStaleAge | Default stale age in seconds for cached items | `1209600` (14 days) |
45-
| estimateExpireAge | Function to calculate expire age (redis TTL value) from stale age | Production: `staleAge * 2`<br> Other: `staleAge * 1.2`|
31+
| Option | Description | Default Value |
32+
| ---------------------- | ----------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
33+
| redis_url | Redis connection url | `process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}` : 'redis://localhost:6379'` |
34+
| database | Redis database number to use. Uses DB 0 for production, DB 1 otherwise | `process.env.VERCEL_ENV === 'production' ? 0 : 1` |
35+
| keyPrefix | Prefix added to all Redis keys | `process.env.VERCEL_URL \|\| 'UNDEFINED_URL_'` |
36+
| sharedTagsKey | Key used to store shared tags hash map in Redis | `'__sharedTags__'` |
37+
| timeoutMs | Timeout in milliseconds for Redis operations | `5000` |
38+
| revalidateTagQuerySize | Number of entries to query in one batch during full sync of shared tags hash map | `250` |
39+
| avgResyncIntervalMs | Average interval in milliseconds between tag map full re-syncs | `3600000` (1 hour) |
40+
| redisGetDeduplication | Enable deduplication of Redis get requests via internal in-memory cache. | `true` |
41+
| inMemoryCachingTime | Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely. | `10000` |
42+
| defaultStaleAge | Default stale age in seconds for cached items | `1209600` (14 days) |
43+
| estimateExpireAge | Function to calculate expire age (redis TTL value) from stale age | Production: `staleAge * 2`<br> Other: `staleAge * 1.2` |
4644

4745
## Getting started
4846

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"run-dev-server-15-0-3": "cd ./test/integration/next-app-15-0-3 && pnpm dev",
1313
"build": "tsup",
1414
"lint": "eslint -c eslint.config.mjs --fix",
15-
"fmt": "prettier --write 'src/**/*.ts' 'src/*.ts'",
15+
"format": "prettier --write 'src/**/*.ts' 'src/*.ts'",
1616
"test": "vitest --coverage --config vite.config.ts",
1717
"test:ui": "vitest --ui --config vite.config.ts",
1818
"test:unit": "vitest --config vite.config.ts src/**/*.test.ts src/**/*.test.tsx",

src/RedisStringsHandler.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ export type CacheEntry = {
1414
};
1515

1616
export type CreateRedisStringsHandlerOptions = {
17+
/** Redis redis_url to use.
18+
* @default process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST
19+
? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`
20+
: 'redis://localhost:6379'
21+
*/
22+
redis_url?: string;
1723
/** Redis database number to use. Uses DB 0 for production, DB 1 otherwise
1824
* @default process.env.VERCEL_ENV === 'production' ? 0 : 1
1925
*/
@@ -91,6 +97,11 @@ export default class RedisStringsHandler {
9197
private estimateExpireAge: (staleAge: number) => number;
9298

9399
constructor({
100+
redis_url = process.env.REDIS_URL
101+
? process.env.REDIS_URL
102+
: process.env.REDISHOST
103+
? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`
104+
: 'redis://localhost:6379',
94105
database = process.env.VERCEL_ENV === 'production' ? 0 : 1,
95106
keyPrefix = process.env.VERCEL_URL || 'UNDEFINED_URL_',
96107
sharedTagsKey = '__sharedTags__',
@@ -113,9 +124,7 @@ export default class RedisStringsHandler {
113124
try {
114125
this.client = createClient({
115126
...(database !== 0 ? { database } : {}),
116-
url: process.env.REDISHOST
117-
? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`
118-
: 'redis://localhost:6379',
127+
url: redis_url,
119128
});
120129

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

0 commit comments

Comments
 (0)