Skip to content

Commit 8c1150e

Browse files
committed
feat: added redis integration for the application
1 parent cc9b2e7 commit 8c1150e

File tree

6 files changed

+169
-20
lines changed

6 files changed

+169
-20
lines changed

.env.example

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ API_URL=
1818
FRONTEND_URL=
1919

2020
# Application Environment
21-
NODE_ENV=
21+
NODE_ENV=
22+
23+
# Redis Variables
24+
REDIS_HOST=
25+
REDIS_PORT=

package-lock.json

Lines changed: 132 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@nestjs/typeorm": "^10.0.2",
3939
"bcryptjs": "^2.4.3",
4040
"cache-manager": "^5.7.6",
41+
"cache-manager-redis-store": "^3.0.1",
4142
"class-transformer": "^0.5.1",
4243
"class-validator": "^0.14.1",
4344
"cors": "^2.8.5",

src/app/app.module.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import { UserActivitiesAnsweredModule } from '../modules/user-activities-answere
77
import { UserCoursesConcludedModule } from '../modules/user-courses-concluded/infra/modules/user-courses-concluded.module';
88
import { ActivityModule } from '../modules/activity/infra/modules/activity.module';
99
import { CacheModule } from '@nestjs/cache-manager';
10+
import { ConfigModule, ConfigService } from '@nestjs/config';
11+
import { RedisOptions } from '../shared/config/redis.config';
1012

1113
@Module({
1214
imports: [
13-
CacheModule.register({
14-
isGlobal: true,
15-
ttl: 60 * 10000,
16-
}),
15+
ConfigModule.forRoot({ isGlobal: true }),
16+
CacheModule.registerAsync(RedisOptions),
1717
DatabaseModule,
1818
UserModule,
1919
UserScoreModule,

src/shared/config/app.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ const appConfigurationsSchema = z.object({
2424
.enum(['development', 'production', 'test', 'local'])
2525
.default('development'),
2626
SSL: z.boolean().default(false),
27+
28+
REDIS_HOST: z.string().min(1),
29+
REDIS_PORT: z.number(),
2730
});
2831

2932
let appConfigurations: z.infer<typeof appConfigurationsSchema> = {};
@@ -53,6 +56,8 @@ try {
5356
JWT_KEY: process.env.JWT_KEY,
5457
API_PORT: parseInt(process.env.API_PORT),
5558
SSL: process.env.NODE_ENV === 'production' ? true : false,
59+
REDIS_HOST: process.env.REDIS_HOST,
60+
REDIS_PORT: parseInt(process.env.REDIS_PORT),
5661
});
5762
} catch (error) {
5863
if (error instanceof ZodError) {

src/shared/config/redis.config.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { CacheModuleAsyncOptions } from "@nestjs/cache-manager";
2+
import { ConfigModule } from "@nestjs/config";
3+
import { redisStore } from "cache-manager-redis-store";
4+
import { appConfigurations } from "./app.config";
5+
6+
export const RedisOptions: CacheModuleAsyncOptions = {
7+
isGlobal: true,
8+
imports: [ConfigModule],
9+
useFactory: async () => {
10+
const store = await redisStore({
11+
ttl: 60 * 10000,
12+
socket: {
13+
host: appConfigurations.REDIS_HOST,
14+
port: appConfigurations.REDIS_PORT,
15+
},
16+
});
17+
return {
18+
store: () => store,
19+
};
20+
},
21+
inject: [],
22+
};

0 commit comments

Comments
 (0)