diff --git a/packages/server/package.json b/packages/server/package.json index d2f6c5b..23eb38c 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -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", @@ -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", diff --git a/packages/server/src/app.module.ts b/packages/server/src/app.module.ts index 77bc776..1c3700a 100644 --- a/packages/server/src/app.module.ts +++ b/packages/server/src/app.module.ts @@ -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'; @@ -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: [ @@ -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('CACHE_TTL', '5s')), + max: config.get('CACHE_MAX', 100), + }), + inject: [ConfigService], + }), DatabaseModule, AuthModule, NoticeModule, diff --git a/packages/server/src/club/club.controller.ts b/packages/server/src/club/club.controller.ts index a85b8d9..6505660 100644 --- a/packages/server/src/club/club.controller.ts +++ b/packages/server/src/club/club.controller.ts @@ -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'; @@ -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) {} diff --git a/packages/server/src/notice/notice.controller.ts b/packages/server/src/notice/notice.controller.ts index 738d122..b011038 100644 --- a/packages/server/src/notice/notice.controller.ts +++ b/packages/server/src/notice/notice.controller.ts @@ -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'; @@ -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) {} diff --git a/packages/server/src/project/project.controller.ts b/packages/server/src/project/project.controller.ts index 2952470..c1d6e08 100644 --- a/packages/server/src/project/project.controller.ts +++ b/packages/server/src/project/project.controller.ts @@ -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'; @@ -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) {} diff --git a/yarn.lock b/yarn.lock index 2f16cfc..baa4195 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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" @@ -6186,9 +6209,9 @@ ms@2.1.2, ms@^2.1.1: resolved "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz" integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w== -ms@2.1.3, ms@^2.0.0: +ms@2.1.3, 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== multer@1.4.4-lts.1: