This package uses ioredis for Redis connections. Install it as a peer dependency:
npm install ioredis// data-cache-handler.mjs
import Redis from "ioredis";
import { createRedisDataCacheHandler } from "@mrjasonroy/cache-components-cache-handler";
const redis = new Redis(process.env.REDIS_URL || "redis://localhost:6379");
export default createRedisDataCacheHandler({
redis,
keyPrefix: "myapp:cache:",
tagPrefix: "myapp:tags:",
});// next.config.js
export default {
cacheComponents: true,
cacheHandlers: {
default: "./data-cache-handler.mjs",
},
};const redis = new Redis(process.env.REDIS_URL);
// Examples:
// redis://localhost:6379
// redis://:password@host:6379/0
// rediss://host:6380 (TLS)const redis = new Redis({
host: "localhost",
port: 6379,
password: "your-password",
db: 0,
});const redis = new Redis({
host: "your-host.com",
port: 6380,
tls: {
rejectUnauthorized: false, // For self-signed certs
},
});Or use rediss:// protocol:
const redis = new Redis("rediss://your-host.com:6380");const redis = new Redis(process.env.REDIS_URL);
redis.on("error", (err) => {
console.error("Redis Error:", err);
});
redis.on("connect", () => {
console.log("Redis Connected");
});
redis.on("ready", () => {
console.log("Redis Ready");
});export default createRedisDataCacheHandler({
redis, // ioredis client instance
keyPrefix: "myapp:cache:", // Namespace for cache keys
tagPrefix: "myapp:tags:", // Namespace for cache tags
defaultTTL: 86400, // Default TTL in seconds (24 hours)
debug: false, // Enable debug logging
});docker run -d -p 6379:6379 redis:7-alpineOr use docker-compose.yml:
version: "3"
services:
redis:
image: redis:7-alpine
ports:
- "6379:6379"Start:
docker compose up -dioredis handles connection pooling automatically. No additional configuration needed.
For automatic failover:
import Redis from "ioredis";
const redis = new Redis({
sentinels: [
{ host: "sentinel-1", port: 26379 },
{ host: "sentinel-2", port: 26379 },
{ host: "sentinel-3", port: 26379 },
],
name: "mymaster",
});For distributed deployments:
import { Cluster } from "ioredis";
const redis = new Cluster([
{ host: "node1", port: 6379 },
{ host: "node2", port: 6379 },
{ host: "node3", port: 6379 },
]);Keys are automatically expired based on cache lifetime settings. Monitor Redis memory usage.
# Redis CLI
redis-cli info memory
redis-cli info stats
# Check cache keys
redis-cli --scan --pattern "myapp:cache:*" | wc -l
# Check tag keys
redis-cli --scan --pattern "myapp:tags:*" | wc -lFor ElastiCache you typically get a hostname + token instead of a URI. Use the zero-config factory and let it wire TLS/passwords for you:
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:",
});Valkey is a Redis-compatible open source fork. Switch the type to "valkey" (or set CACHE_BACKEND=valkey) and point at your cluster URL:
import { createCacheHandler } from "@mrjasonroy/cache-components-cache-handler";
export default createCacheHandler({
type: "valkey",
url: process.env.VALKEY_URL ?? "redis://valkey.internal:6379",
password: process.env.REDIS_PASSWORD,
});const redis = createClient({
url: process.env.UPSTASH_REDIS_REST_URL,
password: process.env.UPSTASH_REDIS_REST_TOKEN,
socket: {
tls: true,
},
});const redis = createClient({
url: process.env.KV_URL,
token: process.env.KV_REST_API_TOKEN,
socket: {
tls: true,
},
});