2024. 8. 12. 23:24ใTIL
๐กRedis-cache
- nestJs์์ redis ์ฌ์ฉํ๊ธฐ๐
redis
- redis๋ Key, Value ๊ตฌ์กฐ์ ๋น์ ํ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ ๊ด๋ฆฌํ๊ธฐ ์ํ ์คํ ์์ค ๊ธฐ๋ฐ์ ๋น๊ด๊ณํ ๋ฐ์ดํฐ ๋ฒ ์ด์ค ๊ด๋ฆฌ ์์คํ ์ผ๋ก, ๋ฐ์ดํฐ๋ฒ ์ด์ค, ์บ์, ๋ฉ์์ง ๋ธ๋ก์ปค๋ก ์ฌ์ฉ๋๋ฉฐ, ์ธ๋ฉ๋ชจ๋ฆฌ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง ์ ์ฅ์์ด๋ค.
(์ฌ์ฉ์ ๋ฐ๋ผ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ก ์ฌ์ฉํ ์๋ ์๊ณ , Cache Server๋ก ์ฌ์ฉํ ์๋ ์์)
์ธ ๋ฉ๋ชจ๋ฆฌ(In-memory)
- ์ธ๋ฉ๋ชจ๋ฆฌ๋ ๋ฐ์ดํฐ๋ฅผ ์ปดํจํฐ์ ์ฃผ ๋ฉ๋ชจ๋ฆฌ(RAM)์ ์ ์ฅํ๋ ๋ฐฉ๋ฒ์ ์๋ฏธํ๋ค.
RAM์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ฒ ๋๋ค๋ฉด ๋ฉ๋ชจ๋ฆฌ ๋ด๋ถ์์ ์ฒ๋ฆฌ๊ฐ ๋๋ฏ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ ์กฐํํ ๋ ํ๋๋์คํฌ๋ฅผ ์ค๊ณ ๊ฐ๋ ๊ณผ์ ์ ๊ฑฐ์น์ง ์์๋ ๋๊ธฐ๋๋ฌธ์ ๋์คํฌ๋ ๋ค๋ฅธ ์๊ตฌ์ ์ธ ์คํ ๋ฆฌ์ง ์ฅ์น์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๊ฒ์ ๋นํด ํจ์ฌ ๋น ๋ฅธ ์๋๋ก ๋ฐ์ดํฐ์ ์ ๊ทผํ ์ ์๋ค.
redis ์ฌ์ฉ ์ด์
• ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ ๋ฐ์ดํฐ๋ฅผ ๋ฌผ๋ฆฌ ๋์คํฌ์ ์ง์ ์ฐ๊ธฐ ๋๋ฌธ์ ์๋ฒ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ฌ ๋ค์ด๋๋๋ผ๋ ๋ฐ์ดํฐ๊ฐ ์์ค๋์ง ์๋๋ค.
• ์ฌ์ฉ์๊ฐ ๋ง์์ง์๋ก ๋งค๋ฒ ๋์คํฌ์ ์ ๊ทผํ๊ธฐ ๋๋ฌธ์ ๋ถํ ์ฆ๊ฐํ๋ค.
=> cache server๋ฅผ ๋์ ํ์ฌ ์ฌ์ฉํ์ฌ, cache server์์ ์ฒซ ๋ฒ์งธ ์์ฒญ ์ดํ ์ ์ฅ๋ ๊ฒฐ๊ณผ๊ฐ์ ๋ฐ๋ก ๋ฐํ์ํจ๋ค.
์ฌ์ฉ ๋ฐฉ๋ฒ
1. Redis ์ค์น
npm i --save ioredis
2. cache manager ์ค์น
npm install @nestjs/cache-manager cache-manager
3.redis.module.ts
import { Module } from '@nestjs/common';
import {
RedisModule as NestCacheModule,
RedisModuleOptions,
} from '@nestjs/cache-manager';
import * as redisStore from 'cache-manager-ioredis';
import { RedisService } from './cache.service';
import { ConfigService } from '@nestjs/config';
@Module({
imports: [
NestCacheModule.register<CacheModuleOptions>({
useFactory: async (configService: ConfigService) => ({
store: redisStore,
host: configService.get<string>('REDIS_HOST'), // Redis ํธ์คํธ
port: configService.get<number>('REDIS_PORT'), // Redis ํฌํธ
password: configService.get<string>('REDIS_PASSWORD'), // Redis ๋น๋ฐ๋ฒํธ
ttl: configService.get<number>('REDIS_TTL'), // ์บ์ ์ ์ง ์๊ฐ(์ด)
}),
inject: [ConfigService],
}),
],
exports: [NestCacheModule],
providers: [RedisService],
})
export class RedisModule {}
'TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
TIL( SQL ๊ธฐ๋ณธ๋ฌธ๋ฒ ) (0) | 2024.08.14 |
---|---|
TIL( reduce ) (0) | 2024.08.13 |
TIL ( GUBU ํ๋ก์ ํธ ์ค๊ฐ KPT ) (0) | 2024.08.09 |
TIL ( Faker ) (0) | 2024.08.07 |
TIL ( fk์ index) (0) | 2024.08.06 |