Redis
Overview
@modularityjs/redis provides a shared ioredis client used by all Redis-backed drivers (cache-redis, lock-redis, queue-redis). It manages the connection lifecycle — connecting during boot and disconnecting on shutdown.
Setup
typescript
import { RedisModule } from '@modularityjs/redis';
const modules = [
RedisModule.forRoot({
host: 'localhost',
port: 6379,
}),
// ... Redis drivers
];Configuration
| Option | Default | Description |
|---|---|---|
host | 'localhost' | Redis server hostname |
port | 6379 | Redis server port |
password | - | Authentication password |
db | 0 | Database index |
keyPrefix | 'modularityjs:' | Prefix for all keys (used by drivers, not ioredis) |
lazyConnect | false | Defer connection until first command |
Key Prefixing
All Redis drivers build keys using the pattern:
{RedisConfig.keyPrefix}{driver namespace}{key}For example, with default config:
- Cache key
user:1becomesmodularityjs:cache:user:1 - Lock key
payment:123becomesmodularityjs:lock:payment:123 - Queue stream
ordersbecomesmodularityjs:queue:stream:orders
The prefix is not passed to ioredis's keyPrefix option (which breaks Lua scripts). Instead, each driver builds the full key manually for consistency across all Redis commands including eval.
Lifecycle
onInit— connects to Redis. If the connection fails, boot fails immediately.onShutdown— gracefully disconnects viaQUIT.