NorthA
V2EX  ›  Node.js

redis 在 nestjs 中的使用

  •  
  •   NorthA · Aug 4, 2023 · 2893 views
    This topic created in 1031 days ago, the information mentioned may be changed or developed.

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

    6 replies    2023-08-04 17:13:12 +08:00
    musi
        1
    musi  
       Aug 4, 2023 via iPhone
    新建一个 service ,初始化的时候 new 一个 ioredis
    mybro
        2
    mybro  
       Aug 4, 2023   ❤️ 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'),
    }),
    }),
    wusheng0
        3
    wusheng0  
       Aug 4, 2023 via Android
    NorthA
        4
    NorthA  
    OP
       Aug 4, 2023
    @wusheng0 我看过官网,就是想看看除了官网的例子,大佬们都怎么做
    NorthA
        5
    NorthA  
    OP
       Aug 4, 2023
    @mybro ok ,我去试试
    zhangky
        6
    zhangky  
       Aug 4, 2023
    我之前学习 nestjs 的时候,敲过一个小项目。有用到 redis,可以参考看看。
    https://github.com/zkeyoung/salted-fish-service-client/blob/master/src/app.module.ts
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2648 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 10:21 · PVG 18:21 · LAX 03:21 · JFK 06:21
    ♥ Do have faith in what you're doing.