Skip to content

Commit

Permalink
feat: caching (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmagur1203 authored Feb 23, 2023
1 parent 884bf54 commit 7e1a75b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
3 changes: 3 additions & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@
"@types/passport-google-oauth20": "^2.0.11",
"@types/passport-jwt": "^3.0.8",
"aws-sdk": "^2.1317.0",
"cache-manager": "^5.1.7",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"cookie-parser": "^1.4.6",
"joi": "^17.7.1",
"ms": "^2.1.3",
"mysql2": "^3.1.0",
"passport": "^0.6.0",
"passport-google-oauth20": "^2.0.0",
Expand All @@ -68,6 +70,7 @@
"@types/cookie-parser": "^1.4.3",
"@types/express": "^4.17.14",
"@types/jest": "^29.1.2",
"@types/ms": "^0.7.31",
"@types/multer": "^1.4.7",
"@types/node": "^18.8.3",
"@types/supertest": "^2.0.12",
Expand Down
14 changes: 12 additions & 2 deletions packages/server/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { CacheModule, Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { NoticeModule } from './notice/notice.module';
import { ProjectModule } from './project/project.module';
import { ClubModule } from './club/club.module';
Expand All @@ -10,6 +10,7 @@ import { ConfigValidator } from './validators/config';
import { AdminModule } from './admin/admin.module';
import { AuthModule } from './auth/auth.module';
import { UploadModule } from './upload/upload.module';
import ms from 'ms';

@Module({
imports: [
Expand All @@ -18,6 +19,15 @@ import { UploadModule } from './upload/upload.module';
envFilePath: [`../../.env`, `.env`],
validationSchema: ConfigValidator,
}),
CacheModule.registerAsync({
isGlobal: true,
imports: [ConfigModule],
useFactory: async (config: ConfigService) => ({
ttl: ms(config.get<string>('CACHE_TTL', '5s')),
max: config.get<number>('CACHE_MAX', 100),
}),
inject: [ConfigService],
}),
DatabaseModule,
AuthModule,
NoticeModule,
Expand Down
6 changes: 6 additions & 0 deletions packages/server/src/club/club.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {
Delete,
UseGuards,
Req,
CacheInterceptor,
UseInterceptors,
CacheTTL,
} from '@nestjs/common';
import { ClubService } from './club.service';
import { CreateClubDto } from './dto/create-club.dto';
Expand All @@ -16,8 +19,11 @@ import { AdminGuard } from 'src/admin/guards/admin.guard';
import { Request } from 'express';
import { ApiBearerAuth } from '@nestjs/swagger';
import { Admin } from 'src/admin/entities/admin.entity';
import ms from 'ms';

@Controller('club')
@CacheTTL(ms('1h'))
@UseInterceptors(CacheInterceptor)
export class ClubController {
constructor(private readonly clubService: ClubService) {}

Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/notice/notice.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
Delete,
Query,
Req,
CacheInterceptor,
UseInterceptors,
} from '@nestjs/common';
import { NoticeService } from './notice.service';
import { CreateNoticeDto } from './dto/create-notice.dto';
Expand All @@ -20,6 +22,7 @@ import { Request } from 'express';
import { Admin } from 'src/admin/entities/admin.entity';

@Controller('notice')
@UseInterceptors(CacheInterceptor)
export class NoticeController {
constructor(private readonly noticeService: NoticeService) {}

Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/project/project.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
Req,
UseGuards,
Query,
CacheInterceptor,
UseInterceptors,
} from '@nestjs/common';
import { ProjectService } from './project.service';
import { CreateProjectDto } from './dto/create-project.dto';
Expand All @@ -20,6 +22,7 @@ import { Admin } from 'src/admin/entities/admin.entity';
import { ApiBearerAuth } from '@nestjs/swagger';

@Controller('project')
@UseInterceptors(CacheInterceptor)
export class ProjectController {
constructor(private readonly projectService: ProjectService) {}

Expand Down
27 changes: 25 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2077,6 +2077,11 @@
resolved "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz"
integrity sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==

"@types/ms@^0.7.31":
version "0.7.31"
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==

"@types/multer@^1.4.7":
version "1.4.7"
resolved "https://registry.yarnpkg.com/@types/multer/-/multer-1.4.7.tgz#89cf03547c28c7bbcc726f029e2a76a7232cc79e"
Expand Down Expand Up @@ -3014,6 +3019,14 @@ cacache@^16.0.0, cacache@^16.0.6, cacache@^16.1.0:
tar "^6.1.11"
unique-filename "^2.0.0"

cache-manager@^5.1.7:
version "5.1.7"
resolved "https://registry.yarnpkg.com/cache-manager/-/cache-manager-5.1.7.tgz#07946574567877bcc4224a3010f592d2443d0ba0"
integrity sha512-2W43F4ruaYb7dSiCOqkKtpnmn51/3DqxbKMMTxHfLa6qtj1letd8rr17/P/c7D7wYViqQLa+LetLCoKSVwBN1w==
dependencies:
lodash.clonedeep "^4.5.0"
lru-cache "^7.17.0"

call-bind@^1.0.0, call-bind@^1.0.2:
version "1.0.2"
resolved "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz"
Expand Down Expand Up @@ -5793,6 +5806,11 @@ locate-path@^6.0.0:
dependencies:
p-locate "^5.0.0"

lodash.clonedeep@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"
integrity sha512-H5ZhCF25riFd9uB5UCkVKo61m3S/xZk1x4wA6yp/L3RFP6Z/eHH1ymQcGLo7J3GMPfm0V/7m1tryHuGVxpqEBQ==

lodash.ismatch@^4.4.0:
version "4.4.0"
resolved "https://registry.npmjs.org/lodash.ismatch/-/lodash.ismatch-4.4.0.tgz"
Expand Down Expand Up @@ -5838,6 +5856,11 @@ lru-cache@^7.14.1:
resolved "https://registry.npmjs.org/lru-cache/-/lru-cache-7.14.1.tgz"
integrity sha512-ysxwsnTKdAx96aTRdhDOCQfDgbHnt8SK0KY8SEjO0wHinhWOFTESbjVCMPbU1uGXg/ch4lifqx0wfjOawU2+WA==

lru-cache@^7.17.0:
version "7.17.0"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-7.17.0.tgz#00c7ba5919e5ea7c69ff94ddabbf32cb09ab805c"
integrity sha512-zSxlVVwOabhVyTi6E8gYv2cr6bXK+8ifYz5/uyJb9feXX6NACVDwY4p5Ut3WC3Ivo/QhpARHU3iujx2xGAYHbQ==

lru-cache@^7.4.4, lru-cache@^7.5.1, lru-cache@^7.7.1:
version "7.14.0"
resolved "https://registry.npmjs.org/lru-cache/-/lru-cache-7.14.0.tgz"
Expand Down Expand Up @@ -6186,9 +6209,9 @@ [email protected], ms@^2.1.1:
resolved "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==

[email protected], ms@^2.0.0:
[email protected], ms@^2.0.0, ms@^2.1.3:
version "2.1.3"
resolved "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==

[email protected]:
Expand Down

0 comments on commit 7e1a75b

Please sign in to comment.