Redis Cache Adapter (ioredis) | @express-route-cache
Set up distributed Express.js route caching with Redis using ioredis. Supports URL connection, existing client reuse, and key prefixing. Recommended for production.
Redis Adapter
The Redis adapter is the recommended choice for production environments. It allows multiple server instances to share the same cache and persist data across restarts.
Installation
npm install @express-route-cache/redis ioredisSetup
import { createCache } from "@express-route-cache/core";
import { createRedisAdapter } from "@express-route-cache/redis";
// Option 1: Connect via URL
const adapter = createRedisAdapter({ url: "redis://localhost:6379" });
// Option 2: Use full ioredis options
const adapter = createRedisAdapter({
options: { host: "127.0.0.1", port: 6379, password: "secret" },
});
// Option 3: Reuse an existing ioredis client
const adapter = createRedisAdapter({ client: myExistingClient });
const cache = createCache({ adapter, keyPrefix: "my-app:" });Adapter Options (createRedisAdapter)
| Option | Type | Description |
|---|---|---|
url | string | Redis connection URL (e.g. redis://localhost:6379). |
options | RedisOptions | Raw ioredis options object (alternative to url). |
client | Redis | An existing ioredis instance to reuse. |
[!NOTE] The cache key prefix (default
"erc:") is configured oncreateCache({ keyPrefix }), not on the adapter.
Redis Cluster Adapter
If you are running in a distributed environment with a Redis Cluster topology (such as AWS ElastiCache Cluster or custom sharded setups), use createRedisClusterAdapter.
It automatically manages keys across multiple nodes, pipelines DEL calls to handle cross-slot limitations safely, and queries master nodes individually during keys() lookups.
Setup
import { createCache } from "@express-route-cache/core";
import { createRedisClusterAdapter } from "@express-route-cache/redis";
const adapter = createRedisClusterAdapter({
nodes: [
{ host: "10.0.0.1", port: 6379 },
{ host: "10.0.0.2", port: 6379 },
],
options: {
scaleReads: "slave", // scale reads to replicas
},
});
const cache = createCache({ adapter });Adapter Options (createRedisClusterAdapter)
| Option | Type | Description |
|---|---|---|
nodes | ClusterNode[] | Required. List of seed cluster nodes. |
options | ClusterOptions | Raw ioredis ClusterOptions configuration. |
client | Cluster | An existing ioredis.Cluster instance to reuse (alternative to seeding nodes). |
Performance
This adapter uses native Redis MGET and INCR commands for O(1) performance, and SCAN (cursor-based) for safe key enumeration in Cache Studio. It is highly optimized for high-throughput Express applications.
In-Memory Cache Adapter | @express-route-cache
High-performance in-memory caching for single-process Express.js applications and local development. Built into @express-route-cache/core, zero dependencies.
Memcached Cache Adapter (memjs) | @express-route-cache
Set up Express.js route caching with Memcached using memjs. Fast, simple key-value caching for production environments that already use Memcached.