From 3da8f4710310196a00012ef11f77f709e8158ae3 Mon Sep 17 00:00:00 2001 From: LiaoLiao Date: Thu, 26 Sep 2024 02:33:40 +0800 Subject: [PATCH] docs: update --- docs/latest/redis.md | 51 +++++++++----------------------------------- 1 file changed, 10 insertions(+), 41 deletions(-) diff --git a/docs/latest/redis.md b/docs/latest/redis.md index 9db5898c..50044ff3 100644 --- a/docs/latest/redis.md +++ b/docs/latest/redis.md @@ -22,40 +22,24 @@ export class AppModule {} **Now**, we can use redis in two ways. -via decorator: - ```ts import { Injectable } from '@nestjs/common'; -import { InjectRedis, DEFAULT_REDIS_NAMESPACE } from '@liaoliaots/nestjs-redis'; +import { RedisService, DEFAULT_REDIS } from '@liaoliaots/nestjs-redis'; import Redis from 'ioredis'; @Injectable() export class AppService { - constructor( - @InjectRedis() private readonly redis: Redis // or // @InjectRedis(DEFAULT_REDIS_NAMESPACE) private readonly redis: Redis - ) {} - - async set() { - return await this.redis.set('key', 'value', 'EX', 10); - } -} -``` - -via service: - -```ts -import { Injectable } from '@nestjs/common'; -import { RedisService, DEFAULT_REDIS_NAMESPACE } from '@liaoliaots/nestjs-redis'; -import Redis from 'ioredis'; - -@Injectable() -export class AppService { - private readonly redis: Redis; + private readonly redis: Redis | null; constructor(private readonly redisService: RedisService) { - this.redis = this.redisService.getClient(); + this.redis = this.redisService.getOrThrow(); + // same as + // this.redis = this.redisService.getOrThrow(DEFAULT_REDIS); + // or - // this.redis = this.redisService.getClient(DEFAULT_REDIS_NAMESPACE); + // this.redis = this.redisService.getOrNil(); + // same as + // this.redis = this.redisService.getOrNil(DEFAULT_REDIS); } async set() { @@ -106,7 +90,7 @@ export class AppModule {} | ------------- | ---------------------------------------------------------------------- | ----------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | closeClient | `boolean` | `true` | `false` | 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.](https://docs.nestjs.com/fundamentals/lifecycle-events#application-shutdown) | | commonOptions | [RedisOptions](https://luin.github.io/ioredis/index.html#RedisOptions) | `undefined` | `false` | Common options to be passed to each client. | -| readyLog | `boolean` | `false` | `false` | If set to `true`, then ready logging will be displayed when the client is ready. | +| readyLog | `boolean` | `true` | `false` | If set to `true`, then ready logging will be displayed when the client is ready. | | errorLog | `boolean` | `true` | `false` | If set to `true`, then errors that occurred while connecting will be displayed by the built-in logger. | | config | `RedisClientOptions` \| `RedisClientOptions`[] | `undefined` | `false` | Used to specify single or multiple clients. | @@ -497,18 +481,3 @@ export class AppModule {} ``` And there we go. - -### Testing - -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`. - -```ts -import { Test, TestingModule } from '@nestjs/testing'; -import { getRedisToken } from '@liaoliaots/nestjs-redis'; - -const module: TestingModule = await Test.createTestingModule({ - providers: [{ provide: getRedisToken('namespace'), useValue: mockedInstance }, YourService] -}).compile(); -``` - -A working example is available [here](/sample).