Skip to content

Commit

Permalink
redis and cache integration wip
Browse files Browse the repository at this point in the history
  • Loading branch information
AHS12 committed Oct 25, 2024
1 parent 227433f commit f023168
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 2 deletions.
11 changes: 11 additions & 0 deletions .env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ DB_NAME=ims-nest
DB_USERNAME=postgres
DB_PASSWORD=postgres

# Redis Config
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_USERNAME=
REDIS_DB=0
REDIS_MAX_RETRIES=3
REDIS_RETRY_DELAY=50
REDIS_RETRY_DELAY_MAX=2000
REDIS_CONNECT_TIMEOUT=10000


# SMTP Config
SMTP_HOST=smtp.mailtrap.io
Expand Down
10 changes: 10 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ DB_PASSWORD=
DB_POOL_MIN=2
DB_POOL_MAX=10

# Redis Config
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_USERNAME=
REDIS_DB=0
REDIS_MAX_RETRIES=3
REDIS_RETRY_DELAY=50
REDIS_RETRY_DELAY_MAX=2000
REDIS_CONNECT_TIMEOUT=10000

# SMTP Config
SMTP_HOST=smtp.mailtrap.io
Expand Down
13 changes: 12 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ services:
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
redis:
image: redis:7-alpine
container_name: ims-nest-redis
ports:
- "6379:6379"
volumes:
- redis_data:/data
command: redis-server --appendonly yes # For data persistence
restart: unless-stopped


volumes:
postgres_data:
postgres_data:
redis_data:
182 changes: 182 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@mikro-orm/nestjs": "^6.0.2",
"@mikro-orm/postgresql": "^6.3.13",
"@mikro-orm/seeder": "^6.3.13",
"@nestjs-modules/ioredis": "^2.0.2",
"@nestjs/axios": "^3.0.3",
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.2.3",
Expand All @@ -53,6 +54,7 @@
"class-validator": "^0.14.1",
"dotenv": "^16.4.5",
"ims-nest-api-starter": "file:",
"ioredis": "^5.4.1",
"nestjs-command": "^3.1.4",
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
Expand Down Expand Up @@ -116,4 +118,4 @@
"./src/config/mikro-orm.config.ts"
]
}
}
}
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { MiscModule } from './modules/misc/misc.module';
import { PermissionModule } from './modules/permission/permission.module';
import { RoleModule } from './modules/role/role.module';
import { UserModule } from './modules/user/user.module';
import { CacheModule } from './modules/cache/cache.module';

@Module({
imports: [
Expand Down Expand Up @@ -46,6 +47,7 @@ import { UserModule } from './modules/user/user.module';
RoleModule,
UserModule,
AuthModule,
CacheModule,
],
controllers: [AppController],
providers: [
Expand Down
36 changes: 36 additions & 0 deletions src/modules/cache/cache.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { RedisModule } from '@nestjs-modules/ioredis';
import { Module } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { CacheService } from './cache.service';

@Module({
imports: [
RedisModule.forRootAsync({
useFactory: (configService: ConfigService) => ({
type: 'single',
options: {
host: configService.get('REDIS_HOST'),
port: Number(configService.get<number>('REDIS_PORT')),
username: configService.get('REDIS_USERNAME'),
password: configService.get('REDIS_PASSWORD'),
db: Number(configService.get<number>('REDIS_DB')),
maxRetriesPerRequest: Number(
configService.get<number>('REDIS_MAX_RETRIES'),
),
connectTimeout: Number(
configService.get<number>('REDIS_CONNECT_TIMEOUT'),
),
retryStrategy: (times) =>
Math.min(
times * Number(configService.get<number>('REDIS_RETRY_DELAY')),
Number(configService.get<number>('REDIS_RETRY_DELAY_MAX')),
),
},
}),
inject: [ConfigService],
}),
],
providers: [CacheService],
exports: [CacheService],
})
export class CacheModule {}
Loading

0 comments on commit f023168

Please sign in to comment.