Skip to content

Commit 0775190

Browse files
committed
feat(examples): add redis integrations
1 parent b45af47 commit 0775190

File tree

9 files changed

+238
-12
lines changed

9 files changed

+238
-12
lines changed

docker-compose.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,16 @@ services:
33
image: redis:8-alpine
44
ports:
55
- '6379:6379'
6+
7+
redis-cluster:
8+
image: redis/redis-stack:latest
9+
command: |
10+
sh -c '
11+
redis-server --port 6380 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes --loadmodule /opt/redis-stack/lib/redisearch.so --loadmodule /opt/redis-stack/lib/rejson.so --protected-mode no --bind 0.0.0.0 &
12+
sleep 5
13+
redis-cli -p 6380 cluster meet 127.0.0.1 6380 || true
14+
redis-cli -p 6380 cluster addslots $(seq 0 16383 | tr "\n" " ") || true
15+
wait
16+
'
17+
ports:
18+
- '6380:6380'

examples/full/package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"targets": {
77
"build": {
88
"executor": "nx:run-commands",
9-
"outputs": ["{projectRoot}/dist"],
9+
"outputs": [
10+
"{projectRoot}/dist"
11+
],
1012
"options": {
1113
"command": "webpack-cli build",
1214
"args": [
@@ -78,5 +80,8 @@
7880
}
7981
}
8082
},
81-
"dependencies": {}
83+
"dependencies": {
84+
"@nestjs-redis/kit": "*",
85+
"redis": "^5.0.0"
86+
}
8287
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import { Controller, Get } from '@nestjs/common';
1+
import { Controller, Get, UseGuards } from '@nestjs/common';
2+
import { ThrottlerGuard } from '@nestjs/throttler';
23
import { AppService } from './app.service';
34

45
@Controller()
56
export class AppController {
67
constructor(private readonly appService: AppService) {}
78

8-
@Get()
9+
@Get('date')
910
getData() {
10-
return this.appService.getData();
11+
return this.appService.getValue('test-date');
12+
}
13+
14+
@Get('date/set')
15+
@UseGuards(ThrottlerGuard)
16+
async setData() {
17+
return this.appService.setValue('test-date', new Date().toISOString());
1118
}
1219
}
Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,54 @@
1+
// app.module.ts
12
import { Module } from '@nestjs/common';
3+
import { TerminusModule } from '@nestjs/terminus';
4+
import { ThrottlerModule, seconds } from '@nestjs/throttler';
5+
import {
6+
RedisHealthIndicator,
7+
RedisModule,
8+
RedisThrottlerStorage,
9+
RedisToken,
10+
RedlockModule,
11+
} from '@nestjs-redis/kit';
12+
import type { RedisClientType } from 'redis';
213
import { AppController } from './app.controller';
314
import { AppService } from './app.service';
15+
import { HealthController } from './health.controller';
416

517
@Module({
6-
imports: [],
7-
controllers: [AppController],
8-
providers: [AppService],
18+
imports: [
19+
// Client
20+
RedisModule.forRoot({
21+
isGlobal: true,
22+
options: { url: 'redis://localhost:6379' },
23+
}),
24+
RedisModule.forRoot({
25+
isGlobal: true,
26+
options: { url: 'redis://localhost:6380' },
27+
connectionName: 'cluster',
28+
}),
29+
30+
// Locking
31+
RedlockModule.forRootAsync({
32+
inject: [RedisToken()],
33+
useFactory: (redis: RedisClientType) => ({
34+
clients: [redis],
35+
redlockConfig: { retryDelayMs: 100, retryCount: 2 },
36+
}),
37+
}),
38+
39+
// Throttling
40+
ThrottlerModule.forRootAsync({
41+
inject: [RedisToken()],
42+
useFactory: (redis: RedisClientType) => ({
43+
throttlers: [{ limit: 5111, ttl: seconds(60) }],
44+
storage: new RedisThrottlerStorage(redis),
45+
}),
46+
}),
47+
48+
// Health checks
49+
TerminusModule,
50+
],
51+
controllers: [AppController, HealthController],
52+
providers: [RedisHealthIndicator, AppService],
953
})
1054
export class AppModule {}
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
// app.service.ts
12
import { Injectable } from '@nestjs/common';
3+
import { InjectRedis, Redlock } from '@nestjs-redis/kit';
4+
import type { RedisClientType } from 'redis';
25

36
@Injectable()
47
export class AppService {
5-
getData(): { message: string } {
6-
return { message: 'Hello API' };
8+
constructor(@InjectRedis() private readonly redis: RedisClientType) {}
9+
10+
@Redlock(['locks:test'], 100)
11+
async setValue(key: string, value: string) {
12+
await new Promise((resolve) => setTimeout(resolve, 2000));
13+
await this.redis.set(key, value);
14+
}
15+
16+
async getValue(key: string) {
17+
return this.redis.get(key);
718
}
819
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Controller, Get } from '@nestjs/common';
2+
import { HealthCheck, HealthCheckService } from '@nestjs/terminus';
3+
import { InjectRedis, RedisHealthIndicator } from '@nestjs-redis/kit';
4+
import type { RedisClientType, RedisClusterType } from 'redis';
5+
6+
@Controller('health')
7+
export class HealthController {
8+
constructor(
9+
private readonly health: HealthCheckService,
10+
private readonly redis: RedisHealthIndicator,
11+
@InjectRedis() private readonly redisClient: RedisClientType,
12+
@InjectRedis('cluster')
13+
private readonly redisClusterClient: RedisClusterType,
14+
) {}
15+
16+
@Get()
17+
@HealthCheck()
18+
check() {
19+
return this.health.check([
20+
() =>
21+
this.redis.isHealthy('redis-client', {
22+
client: this.redisClient,
23+
}),
24+
() =>
25+
this.redis.isHealthy('redis-cluster', {
26+
client: this.redisClusterClient,
27+
}),
28+
]);
29+
}
30+
}

examples/full/tsconfig.app.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@
1010
"target": "es2021"
1111
},
1212
"include": ["src/**/*.ts"],
13-
"exclude": ["eslint.config.js", "eslint.config.cjs", "eslint.config.mjs"]
13+
"exclude": ["eslint.config.js", "eslint.config.cjs", "eslint.config.mjs"],
14+
"references": [
15+
{
16+
"path": "../../packages/kit/tsconfig.lib.json"
17+
}
18+
]
1419
}

examples/full/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"files": [],
44
"include": [],
55
"references": [
6+
{
7+
"path": "../../packages/kit"
8+
},
69
{
710
"path": "./tsconfig.app.json"
811
}

pnpm-lock.yaml

Lines changed: 109 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)