HINT: By default, the RedisModule is a Global module.
HINT: If you don't set the namespace for a client, its namespace is set to "default". Please note that you shouldn't have multiple client without a namespace, or with the same namespace, otherwise they will get overridden.
// an exampleimport{Module}from'@nestjs/common';import{RedisModule,RedisService}from'@liaoliaots/nestjs-redis';import{ThrottlerModule}from'@nestjs/throttler';import{ThrottlerStorageRedisService}from'nestjs-throttler-storage-redis';@Module({imports:[RedisModule.forRoot({config:{host:'localhost',port:6379,password:'authpassword',},}),ThrottlerModule.forRootAsync({useFactory(redisService:RedisService){constredis=redisService.getClient();return{ttl:60,limit:600,storage:newThrottlerStorageRedisService(redis,1000),};},inject:[RedisService],}),],})exportclassAppModule{}
If set to true, all clients will be closed automatically on nestjs application shutdown. To use closeClient, you must enable listeners by calling app.enableShutdownHooks(). Read more about the application shutdown.
Client name. If client name is not given then it will be called "default". Different clients must have different names. You can import DEFAULT_REDIS_NAMESPACE to use it.
url
string
undefined
false
URI scheme to be used to specify connection options as a redis:// URL or rediss:// URL.
path
string
undefined
false
Path to be used for Unix domain sockets.
onClientCreated
function
undefined
false
Function to be executed as soon as the client is created.
// an exampleimport{Module,ValueProvider}from'@nestjs/common';import{RedisModule,RedisModuleOptions}from'@liaoliaots/nestjs-redis';constMyOptionsSymbol=Symbol('options');constMyOptionsProvider:ValueProvider<RedisModuleOptions>={provide:MyOptionsSymbol,useValue:{config:{host:'localhost',port:6379,password:'authpassword',},},};@Module({imports:[RedisModule.forRootAsync({useFactory(options:RedisModuleOptions){returnoptions;},inject:[MyOptionsSymbol],extraProviders:[MyOptionsProvider],}),],})exportclassAppModule{}
... or via useExisting, if you wish to use an existing configuration provider imported from a different module.
import{Module}from'@nestjs/common';import{RedisModule}from'@liaoliaots/nestjs-redis';@Module({imports:[RedisModule.forRoot({config:{host:'localhost',port:6379,password:'authpassword',// or with URL// url: 'redis://:authpassword@localhost:6379/0'},}),],})exportclassAppModule{}
By default, the RedisModule is a Global module, RedisService and all redis instances are registered in the global scope. Once defined, they're available everywhere.
You can change this behavior by isGlobal parameter:
// cats.module.tsimport{Module}from'@nestjs/common';import{RedisModule}from'@liaoliaots/nestjs-redis';import{CatsService}from'./cats.service';import{CatsController}from'./cats.controller';@Module({imports:[RedisModule.forRoot({config:{host:'localhost',port:6379,password:'authpassword',},},false,// <-- providers are registered in the module scope),],providers:[CatsService],controllers:[CatsController],})exportclassCatsModule{}
# Unix socket.
#
# Specify the path for the Unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
# unixsocket /run/redis.sock
# unixsocketperm 700
This package exposes getRedisToken() function that returns an internal injection token based on the provided context. Using this token, you can provide a mock implementation of the redis instance using any of the standard custom provider techniques, including useClass, useValue, and useFactory.