Skip to content

Commit 1a65ca3

Browse files
committed
Version bump and readme update
1 parent 53e05a0 commit 1a65ca3

File tree

2 files changed

+138
-4
lines changed

2 files changed

+138
-4
lines changed

README.md

Lines changed: 137 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,23 @@ Read more about this in Handlers section below.
4444

4545
Instead of:
4646

47-
```
47+
```js
4848
const { CacheHandler } = require("@neshca/cache-handler");
4949
module.exports = CacheHandler;
5050
```
5151

5252
Use this:
5353

54-
```
55-
const { Next15CacheHandler } = require("@fortedigital/next-15-cache-handler");
54+
```js
55+
const { CacheHandler } = require("@neshca/cache-handler");
56+
const {
57+
Next15CacheHandler,
58+
} = require("@fortedigital/nextjs-cache-handler/next-15-cache-handler");
59+
60+
CacheHandler.onCreation(() => {
61+
// your usual setup
62+
});
63+
5664
module.exports = new Next15CacheHandler();
5765
```
5866

@@ -108,6 +116,132 @@ Next 15 decided to change type of data.value.body property from String to Buffer
108116
109117
It is recommended to use this handler with `redis-strings` in Next 15.
110118
119+
## Full example
120+
121+
```js
122+
// @neshca/cache-handler dependencies
123+
const { CacheHandler } = require("@neshca/cache-handler");
124+
const createLruHandler = require("@neshca/cache-handler/local-lru").default;
125+
126+
// Next/Redis dependencies
127+
const { createClient } = require("redis");
128+
const { PHASE_PRODUCTION_BUILD } = require("next/constants");
129+
130+
// @fortedigital/nextjs-cache-handler dependencies
131+
const createCompositeHandler =
132+
require("@fortedigital/nextjs-cache-handler/composite").default;
133+
const createRedisHandler =
134+
require("@fortedigital/nextjs-cache-handler/redis-strings").default;
135+
const createBufferStringHandler =
136+
require("@fortedigital/nextjs-cache-handler/buffer-string-decorator").default;
137+
const {
138+
Next15CacheHandler,
139+
} = require("@fortedigital/nextjs-cache-handler/next-15-cache-handler");
140+
141+
// Usual onCreation from @neshca/cache-handler
142+
CacheHandler.onCreation(() => {
143+
// Important - It's recommended to use global scope to ensure only one Redis connection is made
144+
// This ensures only one instance get created
145+
if (global.cacheHandlerConfig) {
146+
return global.cacheHandlerConfig;
147+
}
148+
149+
// Important - It's recommended to use global scope to ensure only one Redis connection is made
150+
// This ensures new instances are not created in a race condition
151+
if (global.cacheHandlerConfigPromise) {
152+
return global.cacheHandlerConfigPromise;
153+
}
154+
155+
// You may need to ignore Redis locally, remove this block otherwise
156+
if (process.env.NODE_ENV === "development") {
157+
const lruCache = createLruHandler();
158+
return { handlers: [lruCache] };
159+
}
160+
161+
// Main promise initializing the handler
162+
global.cacheHandlerConfigPromise = (async () => {
163+
/** @type {import("redis").RedisClientType | null} */
164+
let redisClient = null;
165+
if (PHASE_PRODUCTION_BUILD !== process.env.NEXT_PHASE) {
166+
const settings = {
167+
url: process.env.REDIS_URL, // Make sure you configure this variable
168+
pingInterval: 10000,
169+
};
170+
171+
// This is optional and needed only if you use access keys
172+
if (process.env.REDIS_ACCESS_KEY) {
173+
settings.password = process.env.REDIS_ACCESS_KEY;
174+
}
175+
176+
try {
177+
redisClient = createClient(settings);
178+
redisClient.on("error", (e) => {
179+
if (typeof process.env.NEXT_PRIVATE_DEBUG_CACHE !== "undefined") {
180+
console.warn("Redis error", e);
181+
}
182+
global.cacheHandlerConfig = null;
183+
global.cacheHandlerConfigPromise = null;
184+
});
185+
} catch (error) {
186+
console.warn("Failed to create Redis client:", error);
187+
}
188+
}
189+
190+
if (redisClient) {
191+
try {
192+
console.info("Connecting Redis client...");
193+
await redisClient.connect();
194+
console.info("Redis client connected.");
195+
} catch (error) {
196+
console.warn("Failed to connect Redis client:", error);
197+
await redisClient
198+
.disconnect()
199+
.catch(() =>
200+
console.warn(
201+
"Failed to quit the Redis client after failing to connect."
202+
)
203+
);
204+
}
205+
}
206+
const lruCache = createLruHandler();
207+
208+
if (!redisClient?.isReady) {
209+
console.error("Failed to initialize caching layer.");
210+
global.cacheHandlerConfigPromise = null;
211+
global.cacheHandlerConfig = { handlers: [lruCache] };
212+
return global.cacheHandlerConfig;
213+
}
214+
215+
const redisCacheHandler = createRedisHandler({
216+
client: redisClient,
217+
keyPrefix: "nextjs:",
218+
});
219+
220+
global.cacheHandlerConfigPromise = null;
221+
222+
// This example uses composite handler to switch from Redis to LRU cache if tags contains `memory-cache` tag.
223+
// You can skip composite and use Redis or LRU only.
224+
global.cacheHandlerConfig = {
225+
handlers: [
226+
createCompositeHandler({
227+
handlers: [
228+
lruCache,
229+
createBufferStringHandler(redisCacheHandler), // Use `createBufferStringHandler` in Next15 and ignore it in Next14 or below
230+
],
231+
setStrategy: (ctx) => (ctx?.tags.includes("memory-cache") ? 0 : 1), // You can adjust strategy for deciding which cache should the composite use
232+
}),
233+
],
234+
};
235+
236+
return global.cacheHandlerConfig;
237+
})();
238+
239+
return global.cacheHandlerConfigPromise;
240+
});
241+
242+
module.exports = new Next15CacheHandler();
243+
```
244+
111245
## Reference to Original Package
112246
113247
This package builds upon the core functionality provided by [`@neshca/cache-handler`](https://www.npmjs.com/package/@neshca/cache-handler). You can find more information about the core library, including usage examples and API documentation, at the [official documentation page](https://caching-tools.github.io/next-shared-cache).

packages/nextjs-cache-handler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"type": "git",
1111
"url": "git+https://github.com/fortedigital/nextjs-cache-handler.git"
1212
},
13-
"version": "1.1.3",
13+
"version": "1.1.4",
1414
"type": "module",
1515
"license": "MIT",
1616
"description": "Next.js cache handlers",

0 commit comments

Comments
 (0)