Skip to content

Commit

Permalink
docs: update
Browse files Browse the repository at this point in the history
  • Loading branch information
liaoliaots committed Sep 25, 2024
1 parent 13e056f commit 3da8f47
Showing 1 changed file with 10 additions and 41 deletions.
51 changes: 10 additions & 41 deletions docs/latest/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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. |

Expand Down Expand Up @@ -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).

0 comments on commit 3da8f47

Please sign in to comment.