想看看大佬们在 nestjs 中是如何使用 redis 的,要是有代码截图那就更棒了😋
1
musi 2023-08-04 08:07:25 +08:00 via iPhone
新建一个 service ,初始化的时候 new 一个 ioredis
|
2
mybro 2023-08-04 09:09:57 +08:00 1
import { InjectRedis } from '@liaoliaots/nestjs-redis';
@Injectable() export class AppService { constructor(@InjectRedis() private readonly redis: Redis) {} getHello(): string { return 'Hello World!'; } async getRedisValue(key: string): Promise<string> { const value = await this.redis.get(key); return value; } async setRedis(key: string, value: string | Buffer | number) { this.redis.set(key, value); } } import { RedisModule } from '@liaoliaots/nestjs-redis'; RedisModule.forRootAsync({ imports: [ConfigModule], inject: [ConfigService], useFactory: (configService: ConfigService) => ({ config: configService.get('redisConfig'), }), }), |
3
wusheng0 2023-08-04 12:36:15 +08:00 via Android
|
6
zhangky 2023-08-04 17:13:12 +08:00
我之前学习 nestjs 的时候,敲过一个小项目。有用到 redis,可以参考看看。
https://github.com/zkeyoung/salted-fish-service-client/blob/master/src/app.module.ts |