Skip to content

Commit 69c91fc

Browse files
authored
Merge pull request #22 from trieb-work/iosifnicolae2-add-redis-url
Iosifnicolae2 add redis url
2 parents 0f14a8e + 59b7e59 commit 69c91fc

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
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: 21 additions & 19 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:
@@ -29,21 +26,6 @@ Tested versions are:
2926

3027
Currently PPR, 'use cache', cacheLife and cacheTag are not tested. Use these operations with caution and your own risk.
3128

32-
## Available Options (needs Option B of getting started)
33-
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`|
46-
4729
## Getting started
4830

4931
### Enable redis key-space notifications for Expire and Evict events
@@ -76,6 +58,8 @@ const nextConfig = {
7658
}
7759
```
7860

61+
Make sure to set either REDIS_URL or REDISHOST and REDISPORT environment variables.
62+
7963
### Option B: create a wrapper file to change options
8064

8165
create new file `customized-cache-handler.js` in your project root and add the following code:
@@ -129,6 +113,22 @@ const nextConfig = {
129113

130114
A working example of above can be found in the `test/integration/next-app-customized` folder.
131115

116+
## Available Options
117+
118+
| Option | Description | Default Value |
119+
| ---------------------- | ----------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
120+
| 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'` |
121+
| database | Redis database number to use. Uses DB 0 for production, DB 1 otherwise | `process.env.VERCEL_ENV === 'production' ? 0 : 1` |
122+
| keyPrefix | Prefix added to all Redis keys | `process.env.VERCEL_URL \|\| 'UNDEFINED_URL_'` |
123+
| sharedTagsKey | Key used to store shared tags hash map in Redis | `'__sharedTags__'` |
124+
| timeoutMs | Timeout in milliseconds for Redis operations | `5000` |
125+
| revalidateTagQuerySize | Number of entries to query in one batch during full sync of shared tags hash map | `250` |
126+
| avgResyncIntervalMs | Average interval in milliseconds between tag map full re-syncs | `3600000` (1 hour) |
127+
| redisGetDeduplication | Enable deduplication of Redis get requests via internal in-memory cache. | `true` |
128+
| inMemoryCachingTime | Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely. | `10000` |
129+
| defaultStaleAge | Default stale age in seconds for cached items | `1209600` (14 days) |
130+
| estimateExpireAge | Function to calculate expire age (redis TTL value) from stale age | Production: `staleAge * 2`<br> Other: `staleAge * 1.2` |
131+
132132
## Consistency of Redis and this caching implementation
133133

134134
To understand consistency levels of this caching implementation we first have to understand the consistency of redis itself:
@@ -170,7 +170,9 @@ By accepting and tolerating this eventual consistency, the performance of the ca
170170

171171
## Testing
172172

173-
To run all tests you can use the following command:
173+
Run `pnpm run-dev-server` to start the nextjs integration test project.
174+
175+
To run all tests you can use the following command in a second terminal:
174176

175177
```bash
176178
pnpm test

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)