- Node.js 22+
- Next.js 16+
- pnpm, npm, or yarn
npm install @mrjasonroy/cache-components-cache-handlerFor development or single-instance deployments:
// data-cache-handler.mjs
import { createMemoryDataCacheHandler } from "@mrjasonroy/cache-components-cache-handler";
export default createMemoryDataCacheHandler({
maxSize: 100 * 1024 * 1024, // 100MB
debug: process.env.NODE_ENV === "development",
});// next.config.js
export default {
cacheComponents: true,
cacheHandler: "./cache-handler.mjs", // Optional: ISR handler
cacheHandlers: {
default: "./data-cache-handler.mjs",
remote: "./data-cache-handler.mjs",
},
cacheMaxMemorySize: 0,
};That's it! This handles all "use cache" directives in your app.
For production or multi-instance deployments:
npm install ioredis// data-cache-handler.mjs
import { createCacheHandler } from "@mrjasonroy/cache-components-cache-handler";
export default createCacheHandler({
type: process.env.CACHE_BACKEND ?? "redis", // "redis" | "valkey"
url: process.env.REDIS_URL ?? process.env.VALKEY_URL ?? "redis://localhost:6379",
password: process.env.REDIS_PASSWORD,
keyPrefix: "myapp:cache:",
tagPrefix: "myapp:tags:",
debug: process.env.NODE_ENV === "development",
});// data-cache-handler.mjs
import { createCacheHandler } from "@mrjasonroy/cache-components-cache-handler";
export default createCacheHandler({
type: "elasticache",
endpoint: process.env.ELASTICACHE_ENDPOINT,
port: Number(process.env.ELASTICACHE_PORT ?? 6379),
tls: process.env.ELASTICACHE_TLS !== "false",
password: process.env.ELASTICACHE_AUTH_TOKEN ?? process.env.REDIS_PASSWORD,
keyPrefix: "prod:cache:",
tagPrefix: "prod:tags:",
});Tip: leave
urlunset for ElastiCache – the factory automatically switches to theendpoint/portsignature whentype: "elasticache"is used.
See Redis Configuration for advanced setup, Sentinel/Cluster instructions, and Valkey notes.
Start your Next.js app:
npm run devYou should see cache operations in the console if debug: true.