Skip to content

Core Module

The foundation of NestJS RedisX providing Redis client management with driver abstraction.

What is Core?

@nestjs-redisx/core is the foundation package that provides:

  • RedisModule — NestJS module for Redis integration
  • RedisService — High-level Redis operations wrapper
  • Driver Abstraction — Switch between ioredis and node-redis
  • Multi-Client Support — Manage multiple Redis connections
  • Connection Types — Single, Cluster, and Sentinel configurations
  • Health Monitoring — Automatic reconnection and health checks
  • Plugin System — Extensible architecture for plugins

Installation

bash
npm install @nestjs-redisx/core ioredis
bash
npm install @nestjs-redisx/core redis

Quick Start

typescript
import { Module } from '@nestjs/common';
import { RedisModule } from '@nestjs-redisx/core';

@Module({
  imports: [
    RedisModule.forRoot({
      clients: {
        host: 'localhost',
        port: 6379,
      },
    }),
  ],
})
export class AppModule {}

Use in services:

typescript
import { Injectable } from '@nestjs/common';
import { RedisService } from '@nestjs-redisx/core';
import { User } from './types';

@Injectable()
export class UserService {
  constructor(private readonly redis: RedisService) {}

  async cacheUser(id: string, user: User): Promise<void> {
    await this.redis.set(`user:${id}`, JSON.stringify(user), { ex: 3600 });
  }

  async getUser(id: string): Promise<User | null> {
    const data = await this.redis.get(`user:${id}`);
    return data ? JSON.parse(data) : null;
  }
}

Architecture

Key Features

FeatureDescription
Global ModuleImport once, use everywhere
Lazy ConnectionConnects on first use
Auto ReconnectionExponential backoff with jitter
Health ChecksMonitor connection status
Multiple ClientsNamed connections for different purposes
Driver AgnosticSwitch drivers without code changes
Type SafeFull TypeScript support

Module Configuration

Synchronous

typescript
RedisModule.forRoot({
  clients: {
    host: 'localhost',
    port: 6379,
    password: 'secret',
  },
})

Asynchronous

typescript
RedisModule.forRootAsync({
  useFactory: (config: ConfigService) => ({
    clients: {
      host: config.get('REDIS_HOST'),
      port: config.get('REDIS_PORT'),
    },
  }),
  inject: [ConfigService],
})

With Plugins

typescript
import { CachePlugin } from '@nestjs-redisx/cache';
import { LocksPlugin } from '@nestjs-redisx/locks';

RedisModule.forRoot({
  clients: { host: 'localhost', port: 6379 },
  plugins: [
    new CachePlugin(),
    new LocksPlugin(),
  ],
})

Connection Types

Single Instance

typescript
{
  clients: {
    type: 'single',  // Optional, default
    host: 'localhost',
    port: 6379,
  }
}

Redis Cluster

typescript
{
  clients: {
    type: 'cluster',
    nodes: [
      { host: 'node1', port: 6379 },
      { host: 'node2', port: 6379 },
      { host: 'node3', port: 6379 },
    ],
  }
}

Redis Sentinel

typescript
{
  clients: {
    type: 'sentinel',
    sentinels: [
      { host: 'sentinel1', port: 26379 },
      { host: 'sentinel2', port: 26379 },
    ],
    name: 'mymaster',
  }
}

Documentation

Released under the MIT License.