Skip to content

Commit 726236d

Browse files
committed
feat(be): caching REST endpoints
1 parent 1157806 commit 726236d

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

backend-nest/src/app.module.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ApolloDriver, type ApolloDriverConfig } from "@nestjs/apollo";
2+
import { CacheModule } from "@nestjs/cache-manager";
23
import { Module } from "@nestjs/common";
34
import { ConfigModule } from "@nestjs/config";
45
import { GraphQLModule } from "@nestjs/graphql";
@@ -13,13 +14,17 @@ import { StopModule } from "src/modules/stop/stop.module";
1314

1415
@Module({
1516
imports: [
16-
PrismaModule,
1717
PlatformModule,
1818
DepartureModule,
1919
ImportModule,
2020
StopModule,
21+
22+
PrismaModule,
2123
ConfigModule.forRoot(),
2224
ScheduleModule.forRoot(),
25+
CacheModule.register({
26+
isGlobal: true,
27+
}),
2328
GraphQLModule.forRoot<ApolloDriverConfig>({
2429
driver: ApolloDriver,
2530
playground: true,

backend-nest/src/constants/swagger.const.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { GRAPHQL_API_ROOT } from "src/constants/graphql.const";
22

33
export const SWAGGER_API_ROOT = "/";
4-
export const SWAGGER_JSON_URL = "swagger-json";
4+
export const SWAGGER_JSON_URL = "/swagger-json";
55

66
export const SWAGGER_TITLE = "Metro Now API";
77
export const SWAGGER_VERSION = "1.0";
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { CacheInterceptor } from "@nestjs/cache-manager";
2+
import { ExecutionContext, Injectable } from "@nestjs/common";
3+
4+
import { GRAPHQL_API_ROOT } from "src/constants/graphql.const";
5+
import {
6+
SWAGGER_API_ROOT,
7+
SWAGGER_JSON_URL,
8+
} from "src/constants/swagger.const";
9+
10+
@Injectable()
11+
export class HttpCacheInterceptor extends CacheInterceptor {
12+
trackBy(context: ExecutionContext): string | undefined {
13+
const request = context.switchToHttp().getRequest();
14+
const { httpAdapter } = this.httpAdapterHost;
15+
16+
const isGetRequest = httpAdapter.getRequestMethod(request) === "GET";
17+
const excludePaths: string[] = [
18+
GRAPHQL_API_ROOT,
19+
SWAGGER_JSON_URL,
20+
SWAGGER_API_ROOT,
21+
];
22+
23+
if (!isGetRequest) {
24+
return undefined;
25+
}
26+
if (excludePaths.includes(httpAdapter.getRequestUrl(request))) {
27+
return undefined;
28+
}
29+
30+
return httpAdapter.getRequestUrl(request);
31+
}
32+
}

backend-nest/src/modules/platform/platform.controller.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { CacheInterceptor } from "@nestjs/cache-manager";
12
import {
23
Controller,
34
Get,
45
HttpException,
56
HttpStatus,
67
Query,
8+
UseInterceptors,
79
} from "@nestjs/common";
810
import { ApiQuery, ApiTags } from "@nestjs/swagger";
911
import { z } from "zod";
@@ -29,6 +31,7 @@ import {
2931

3032
@ApiTags("platform")
3133
@Controller("platform")
34+
@UseInterceptors(CacheInterceptor)
3235
export class PlatformController {
3336
constructor(private readonly platformService: PlatformService) {}
3437

backend-nest/src/modules/stop/stop.controller.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { Controller, Get, Query } from "@nestjs/common";
1+
import { CacheInterceptor } from "@nestjs/cache-manager";
2+
import { Controller, Get, Query, UseInterceptors } from "@nestjs/common";
23
import { ApiQuery, ApiTags } from "@nestjs/swagger";
34

45
import { StopService } from "src/modules/stop/stop.service";
56
import { metroOnlyQuery } from "src/swagger/query.swagger";
67

78
@ApiTags("stop")
89
@Controller("stop")
10+
@UseInterceptors(CacheInterceptor)
911
export class StopController {
1012
constructor(private readonly stopService: StopService) {}
1113

@@ -15,6 +17,7 @@ export class StopController {
1517
@Query("metroOnly")
1618
metroOnlyQuery: unknown,
1719
) {
20+
console.log("Fetching all stops");
1821
const metroOnly: boolean = metroOnlyQuery === "true";
1922
const stops = await this.stopService.getAll({ metroOnly });
2023

0 commit comments

Comments
 (0)