Integrates IORedis with Nest
npm install @svtslv/nestjs-ioredis ioredis
npm install -D @types/ioredis
You can also use the interactive CLI
npx nestjs-modules
docker run -p 6379:6379 redis
import { Module } from '@nestjs/common';
import { RedisModule } from '@svtslv/nestjs-ioredis';
import { AppController } from './app.controller';
@Module({
imports: [
RedisModule.forRoot({
config: {
// host: 'localhost',
// port: 6379,
url: 'redis://localhost:6379',
},
}),
],
controllers: [AppController],
})
export class AppModule {}
import { Module } from '@nestjs/common';
import { RedisModule } from '@svtslv/nestjs-ioredis';
import { AppController } from './app.controller';
@Module({
imports: [
RedisModule.forRootAsync({
useFactory: () => ({
config: {
// host: 'localhost',
// port: 6379,
url: 'redis://localhost:6379',
},
}),
}),
],
controllers: [AppController],
})
export class AppModule {}
import { Controller, Get, } from '@nestjs/common';
import { InjectRedis, Redis } from '@svtslv/nestjs-ioredis';
@Controller()
export class AppController {
constructor(
@InjectRedis() private readonly redis: Redis,
) {}
@Get()
async getHello() {
await this.redis.set('key', 'Redis data!');
const redisData = await this.redis.get("key");
return { redisData };
}
}
MIT