REDIS

์žฅ์—ฌ์ง„ยท2022๋…„ 4์›” 23์ผ
0
post-thumbnail

Redis๋ž€?๐Ÿ‘€

  • Memcached(redis์˜ ์ด์ „ ๋ฒ„์ „-์ปดํ“จํ„ฐ ๊ป๋‹ค๊ฐ€ ํ‚ค๋ฉด ์ •๋ณด ๋‚ ๋ผ๊ฐ)์™€
    ๋น„์Šทํ•œ ์บ์‹œ ์‹œ์Šคํ…œ์œผ๋กœ์„œ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณต
  • ๋ฉ”๋ชจ๋ฆฌ ๊ธฐ๋ฐ˜ DB(ram)๋กœ ์†๋„๊ฐ€ ๋น ๋ฆ„
  • ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•˜๊ณ  ์กฐํšŒ(= ์ธ๋ฉ”๋ชจ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค)
  • Redis๋Š” ๋‹ค์–‘ํ•œ ์ž๋ฃŒ ๊ตฌ์กฐ -> ๊ฐœ๋ฐœ์˜ ํŽธ์˜์„ฑ์ด ์ข‹์•„์ง€๊ณ  ๋‚œ์ด๋„๊ฐ€ ๋‚ฎ์•„์ง!
    ๐Ÿ“Œ ๋ฐ์ดํ„ฐ ์ •๋ ฌ ์‹œ DBMS๋ฅผ ์ด์šฉํ•œ๋‹ค๋ฉด DB์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ ํ›„,
    ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌํ•˜์—ฌ ๋‹ค์‹œ ์ฝ์–ด์˜ค๋Š” ๊ณผ์ •์€ ๋””์Šคํฌ์— ์ง์ ‘ ์ ‘๊ทผ์„ ํ•ด์„œ ์‹œ๊ฐ„์ด ๋” ์˜ค๋ž˜ ๊ฑธ๋ฆผ
    ํ•˜์ง€๋งŒ In-Memoryย ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ธย Redis๋ฅผ ์ด์šฉํ•˜๊ณ  ๋ ˆ๋””์Šค์—์„œ ์ œ๊ณตํ•˜๋Š”ย Sorted-Set์ด๋ผ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ๋น ๋ฅด๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌ ๊ฐ€๋Šฅ

โ—โ—Redis๋Š” ๊ณ ์„ฑ๋Šฅ ํ‚ค-๊ฐ’ ์ €์žฅ์†Œ๋กœ์„œ ๋ฌธ์ž์—ด,List,Hash,Set,Sorted-Set ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์›ํ•˜๋Š” NoSQLโ—โ—

Redis vs Mencached

๐Ÿ”Ž๊ณตํ†ต์ 
1. 1ms์ดํ•˜์˜ ์‘๋‹ต๋Œ€๊ธฐ์‹œ๊ฐ„
2. ๊ฐœ๋ฐœ์˜ ์šฉ์ด์„ฑ
3. ๋ฐ์ดํ„ฐ ํŒŒ์ด์…”๋‹
4. ๋‹ค์–‘ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ง€์›

๐Ÿ”ŽRedis๋งŒ์˜ ํŠน์ง•
1. ๋”์šฑ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ
2. Snapshots
3. ๋ณต์ œ
4. ํŠธ๋žœ์ ์…˜
5. Pub / Sub messaging
6. ๋ฃจ์•„ ์Šคํฌ๋ฆฝํŠธ ์ง€์›
7. ์œ„์น˜๊ธฐ๋ฐ˜ ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ง€์›

[ํ•„์š”ํ•œ ๋ชจ๋“ˆ]
yarn add cache-manager
yarn add redis
yarn add cache-manager-redis-store

//docker-compose.yamlํŒŒ์ผ์— redis ์ถ”๊ฐ€
version: '3.7'

services:
  my-backend:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./src:/myfolder/src
    ports:
      - 3000:3000

  my-database:
    image: mysql:latest
    environment:
      MYSQL_DATABASE: 'mainproject'
      MYSQL_ROOT_PASSWORD: 'root'
    ports:
      - 3306:3306

  my-redis:
    image: redis:latest
    ports:
      - 6379:6379
// app.module.ts์— redis๋ฅผ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ์ฝ”๋“œ ์ถ”๊ฐ€
import { CacheModule, Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from './apis/auth/auth.module';
import { BoardModule } from './apis/board/board.module';
import { PointTransactionModule } from './apis/pointTransaction/pointTransaction.module';
import { ProductModule } from './apis/product/product.module';
import { ProductCategoryModule } from './apis/productCategory/productCategory.module';
import { FileModule } from './apis/file/file.module';
import { UserModule } from './apis/user/user.module';
import type { RedisClientOptions } from 'redis';
import * as redisStore from 'cache-manager-redis-store';

@Module({
  imports: [
    AuthModule,
    BoardModule,
    FileModule,
    PointTransactionModule,
    ProductModule,
    ProductCategoryModule,
    UserModule,
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'my_database',
      port: 3306,
      username: 'root',
      password: 'root',
      database: 'myproject',
      entities: [__dirname + '/apis/**/*.entity.*'],
      synchronize: true,
      logging: true,
    }),
    GraphQLModule.forRoot({
      autoSchemaFile: 'src/common/graphql/schema.gql',
      context: ({ req, res }) => ({ req, res }),
    }),
    CacheModule.register<RedisClientOptions>({
      store: redisStore,
      url: 'redis://my_redis:6379',
      isGlobal: true,
    }),
  ],
})
export class AppModule {}

๐Ÿ’กdocker์‹คํ–‰ํ•œ ํ„ฐ๋ฏธ๋„์ด ์•„๋‹Œ ๋‹ค๋ฅธ ํ„ฐ๋ฏธ๋„์—์„œ redis์ž˜ ์‹คํ–‰๋˜๋Š”์ง€ ํ™•์ธ

  • docker ps
  • docker exec -it (redis - containerID) redis-cil --raw
    -> redis ์ ‘์†
  • keys *
    -> redis์— ์ €์žฅ๋œ ๋ชจ๋“  key ์ถœ๋ ฅ
  • set [key][value]
    -> ๋ฐ์ดํ„ฐ ์ €์žฅ
  • get [key]
    -> key์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ ์กฐํšŒ
  • exists [key]
    -> key๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€ ํ™•์ธ
  • flushall
    -> ํ˜„์žฌ ์ €์žฅ๋˜์–ด์žˆ๋Š” ๋ชจ๋“  key ์‚ญ์ œ
  • ttl [key]
    -> ํ˜„์žฌ ๋‚จ์€ ์‹œ๊ฐ„ ํ™•์ธ

๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜๊ณ  ์žˆ๋Š” ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
์ž˜๋ชป๋œ ๋‚ด์šฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ˜น์‹œ ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€ ๋‹ฌ์•„์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿ˜Š

0๊ฐœ์˜ ๋Œ“๊ธ€