Nestjs Redis Packages - v10
    Preparing search index...

    Module @nestjs-labs/nestjs-redis

    @nestjs-labs/nestjs-redis

    Redis(node-redis) module for Nest framework (node.js).

    if you are looking for the ioredis module for nestjs, please use @nestjs-labs/ioredis instead.

    npm install @nestjs-labs/nestjs-redis redis
    
    import { Module } from '@nestjs/common';
    import { RedisModule } from '@nestjs-labs/nestjs-redis';

    @Module({
    imports: [
    RedisModule.forRoot({
    url: 'redis://localhost:6379',
    }),
    ],
    })
    export class AppModule {}
    import { Injectable } from '@nestjs/common';
    import { RedisService } from '@nestjs-labs/nestjs-redis';

    @Injectable()
    export class AppService {
    constructor(private readonly redisService: RedisService) {}

    async setValue(key: string, value: string) {
    const client = this.redisService.getClient();
    await client.set(key, value);
    }

    async getValue(key: string) {
    const client = this.redisService.getClient();
    return await client.get(key);
    }

    // Health check
    async checkHealth() {
    return await this.redisService.healthCheck();
    }
    }

    Get the redis client instance.

    Check if the client is currently connected.

    Wait for the client to be ready and connected.

    Perform a health check on the Redis connection.

    Get the type of Redis client (single instance or cluster).

    Get the cluster client if in cluster mode, null otherwise.

    Get the cluster client or throw an error if not in cluster mode.

    Get cluster information if in cluster mode.

    The module accepts all options from the redis package's RedisClientOptions interface, plus:

    • url?: string - Redis connection URL
    • cluster?: RedisClusterOptions - Redis cluster configuration
    • isGlobal?: boolean - Whether the module should be global
    import { Module } from '@nestjs/common';
    import { ConfigModule, ConfigService } from '@nestjs/config';
    import { RedisModule } from '@nestjs-labs/nestjs-redis';

    @Module({
    imports: [
    ConfigModule.forRoot(),
    RedisModule.forRootAsync({
    imports: [ConfigModule],
    useFactory: (configService: ConfigService) => ({
    // ... other options
    url: configService.get('REDIS_URL'),
    }),
    inject: [ConfigService],
    }),
    ],
    })
    export class AppModule {}
    import { Module } from '@nestjs/common';
    import { RedisModule } from '@nestjs-labs/nestjs-redis';

    @Module({
    imports: [
    RedisModule.forRoot({
    cluster: {
    rootNodes: [
    { url: 'redis://localhost:7000' },
    { url: 'redis://localhost:7001' },
    { url: 'redis://localhost:7002' },
    ],
    },
    }),
    ],
    })
    export class AppModule {}

    The service provides built-in health monitoring capabilities:

    @Injectable()
    export class HealthService {
    constructor(private readonly redisService: RedisService) {}

    async checkRedisHealth() {
    const health = await this.redisService.healthCheck();

    return {
    health,
    timestamp: new Date().toISOString()
    };
    }
    }

    Classes

    RedisModule
    RedisService

    Interfaces

    RedisClusterOptions
    RedisOptions
    RedisOptionsFactory

    Type Aliases

    RedisClientType
    RedisClusterType
    RedisModuleAsyncOptions
    RedisModuleOptions

    Variables

    REDIS_CLIENT

    Functions

    InjectRedis