Skip to content

Commit 3da8f47

Browse files
committed
docs: update
1 parent 13e056f commit 3da8f47

File tree

1 file changed

+10
-41
lines changed

1 file changed

+10
-41
lines changed

docs/latest/redis.md

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,24 @@ export class AppModule {}
2222

2323
**Now**, we can use redis in two ways.
2424

25-
via decorator:
26-
2725
```ts
2826
import { Injectable } from '@nestjs/common';
29-
import { InjectRedis, DEFAULT_REDIS_NAMESPACE } from '@liaoliaots/nestjs-redis';
27+
import { RedisService, DEFAULT_REDIS } from '@liaoliaots/nestjs-redis';
3028
import Redis from 'ioredis';
3129

3230
@Injectable()
3331
export class AppService {
34-
constructor(
35-
@InjectRedis() private readonly redis: Redis // or // @InjectRedis(DEFAULT_REDIS_NAMESPACE) private readonly redis: Redis
36-
) {}
37-
38-
async set() {
39-
return await this.redis.set('key', 'value', 'EX', 10);
40-
}
41-
}
42-
```
43-
44-
via service:
45-
46-
```ts
47-
import { Injectable } from '@nestjs/common';
48-
import { RedisService, DEFAULT_REDIS_NAMESPACE } from '@liaoliaots/nestjs-redis';
49-
import Redis from 'ioredis';
50-
51-
@Injectable()
52-
export class AppService {
53-
private readonly redis: Redis;
32+
private readonly redis: Redis | null;
5433

5534
constructor(private readonly redisService: RedisService) {
56-
this.redis = this.redisService.getClient();
35+
this.redis = this.redisService.getOrThrow();
36+
// same as
37+
// this.redis = this.redisService.getOrThrow(DEFAULT_REDIS);
38+
5739
// or
58-
// this.redis = this.redisService.getClient(DEFAULT_REDIS_NAMESPACE);
40+
// this.redis = this.redisService.getOrNil();
41+
// same as
42+
// this.redis = this.redisService.getOrNil(DEFAULT_REDIS);
5943
}
6044

6145
async set() {
@@ -106,7 +90,7 @@ export class AppModule {}
10690
| ------------- | ---------------------------------------------------------------------- | ----------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
10791
| 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) |
10892
| commonOptions | [RedisOptions](https://luin.github.io/ioredis/index.html#RedisOptions) | `undefined` | `false` | Common options to be passed to each client. |
109-
| readyLog | `boolean` | `false` | `false` | If set to `true`, then ready logging will be displayed when the client is ready. |
93+
| readyLog | `boolean` | `true` | `false` | If set to `true`, then ready logging will be displayed when the client is ready. |
11094
| errorLog | `boolean` | `true` | `false` | If set to `true`, then errors that occurred while connecting will be displayed by the built-in logger. |
11195
| config | `RedisClientOptions` \| `RedisClientOptions`[] | `undefined` | `false` | Used to specify single or multiple clients. |
11296

@@ -497,18 +481,3 @@ export class AppModule {}
497481
```
498482

499483
And there we go.
500-
501-
### Testing
502-
503-
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`.
504-
505-
```ts
506-
import { Test, TestingModule } from '@nestjs/testing';
507-
import { getRedisToken } from '@liaoliaots/nestjs-redis';
508-
509-
const module: TestingModule = await Test.createTestingModule({
510-
providers: [{ provide: getRedisToken('namespace'), useValue: mockedInstance }, YourService]
511-
}).compile();
512-
```
513-
514-
A working example is available [here](/sample).

0 commit comments

Comments
 (0)