JSCODE Logo
프로그래밍 과외블로그후기멘토진
회사명 : JSCODE대표 : 박재성사업자 등록번호 : 244-22-01557통신판매업 : 제 2023-인천미추홀-0381 호
학원 명칭 : 제이에스코드(JSCODE)원격학원학원설립ㆍ운영 등록번호 : 제6063호

서울특별시 구로구 경인로 20가길 11(오류동, 아델리아)

Copyright ⓒ 2025 JSCODE - 최상위 현업 개발자들의 프로그래밍 교육 All rights reserved.

이용약관개인정보처리방침
← 블로그 목록으로 돌아가기

Nest.js 프로젝트에 Redis 셋팅 추가하기

JSCODE 박재성
JSCODE 박재성
2025-12-06
author
JSCODE 박재성
category
Redis
createdAt
Dec 6, 2025
series
비전공자도 이해할 수 있는 Redis 입문/실전
slug
add-redis-to-nestjs
type
post
updatedAt
Dec 6, 2025 04:33 AM

✅ Nest.js 프로젝트에 Redis 셋팅 추가하기

  1. 라이브러리 설치하기
    1. $ npm i @nestjs/cache-manager cache-manager cache-manager-ioredis $ npm i -D @types/cache-manager @types/cache-manager-ioredis
 
  1. AppModule 코드 수정하기
    1. app.module.ts
      import { BoardController } from './board.controller'; import { BoardService } from './board.service'; import { TypeOrmModule } from '@nestjs/typeorm'; import { Board } from './board.entity'; import { CacheModule } from '@nestjs/cache-manager'; import * as redisStore from 'cache-manager-ioredis'; @Module({ imports: [ TypeOrmModule.forRoot({ type: 'mysql', host: 'localhost', port: 3306, username: 'root', password: 'password', database: 'mydb', autoLoadEntities: true, synchronize: true, logging: true, }), TypeOrmModule.forFeature([Board]), CacheModule.register({ store: redisStore, host: 'localhost', port: 6379, ttl: 60, }), ], controllers: [BoardController], providers: [BoardService], }) export class AppModule {}
 
  1. BoardService 코드 수정하기
    1. board.service.ts
      import { Inject, Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Board } from './board.entity'; import { Repository } from 'typeorm'; import { CACHE_MANAGER, Cache } from '@nestjs/cache-manager'; @Injectable() export class BoardService { constructor( @InjectRepository(Board) private boardRepository: Repository<Board>, @Inject(CACHE_MANAGER) private cacheManager: Cache, ) {} async getBoards(page: number, size: number): Promise<Board[]> { const cacheKey = `boards:page:${page}:size:${size}`; const cachedData = await this.cacheManager.get<Board[]>(cacheKey); if (cachedData) { return cachedData; } const skip = (page - 1) * size; const boards = await this.boardRepository.find({ order: { createdAt: 'desc', }, skip: skip, take: size, }); await this.cacheManager.set(cacheKey, boards); return boards; } }
      참고) Spring에서는 어노테이션만 가지고 처리할 수 있는 부분이 굉장히 많지만 Nest.js는 한계점이 존재한다. 이 때문에 캐싱에 대한 처리를 일일이 로직으로 짜주어야 하는 경우가 많다.
 

✅ 테스트 해보기

  1. Nest.js 서버 실행시켜서 API 실행시켜보기
    1. notion image
 
  1. Redis-cli를 활용해 정상적으로 캐싱이 됐는 지 확인하기
    1. $ redis-cli $ keys * # Redis에 저장되어 있는 모든 key 조회 $ get getBoards::boards:page:1:size:10 # 특정 key의 Value 조회 $ ttl getBoards::boards:page:1:size:10 # 특정 key의 TTL 조회
 
 
author
category
Redis
createdAt
series
비전공자도 이해할 수 있는 Redis 입문/실전
slug
type
series-footer
updatedAt
Dec 6, 2025 04:33 AM
📎
이 글은 비전공자도 이해할 수 있는 Redis 입문/실전 (조회 성능 최적화편) 강의의 수업 자료 중 일부입니다.