redis 在 nestjs 中的使用

284 天前
 NorthA

想看看大佬们在 nestjs 中是如何使用 redis 的,要是有代码截图那就更棒了😋

1617 次点击
所在节点    Node.js
6 条回复
musi
284 天前
新建一个 service ,初始化的时候 new 一个 ioredis
mybro
284 天前
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'),
}),
}),
wusheng0
284 天前
NorthA
284 天前
@wusheng0 我看过官网,就是想看看除了官网的例子,大佬们都怎么做
NorthA
284 天前
@mybro ok ,我去试试
zhangky
284 天前
我之前学习 nestjs 的时候,敲过一个小项目。有用到 redis,可以参考看看。
https://github.com/zkeyoung/salted-fish-service-client/blob/master/src/app.module.ts

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/962244

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX