TIL

23/12/09 TIL __ Redis ์‚ฌ์šฉํ•˜๊ธฐ

GABOJOK 2023. 12. 10. 01:03

 

 

๐Ÿง  ์ƒํ™ฉ

 

๋ฆฌํ”„๋ ˆ์‰ฌ ํ† ํฐ์„ ์ €์žฅํ•˜๋Š” ๊ณณ์œผ๋กœ

๊ธฐ์กด์— ์‚ฌ์šฉํ•˜๋˜ aws rds ์ €์žฅ์†Œ์— prisma๋ฅผ ์‚ฌ์šฉํ•ด ์ปฌ๋Ÿผ์„ ๋Š˜๋ ค์„œ ์‚ฌ์šฉํ–ˆ๋‹ค. 

๊ทธ๋Ÿฐ๋ฐ ๋‹จ์ ์€ ๋งŒ๋ฃŒ์‹œ๊ฐ„์ด ๋˜๋ฉด ์ง์ ‘ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์ง€ ์•Š๋Š” ์ด์ƒ ์‚ญ์ œ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. 

 

์ด์— ๋ฐ˜ํ•ด ๋ ˆ๋””์Šค ๋ผ๋Š” ์ €์žฅ์†Œ๋Š” ๋งŒ๋ฃŒ์‹œ๊ฐ„์ด ๋˜๋ฉด ์ €์ ˆ๋กœ ์‚ญ์ œ๊ฐ€ ๋œ๋‹ค๊ณ  ํ•œ๋‹ค. 

ํ† ํฐ ์ €์žฅ์†Œ๋กœ ์ ํ•ฉํ•  ๊ฒƒ ๊ฐ™์•„ ๊ณต๋ถ€ํ•ด ๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค. 

 

 

 

 

๐Ÿž๏ธ   ๊ณผ์ •

Redis์— ๋Œ€ํ•ด ์•„๋Š”๊ฒŒ ์ •๋ง ๋‹จ 1๋„ ์—†์—ˆ๋˜ ์ƒํƒœ๋กœ ์‹œ์ž‘ํ–ˆ๋Š”๋ฐ,

์ฐพ์•„๋ณด๋‹ˆ ์ •๋ง ๋งŽ์€๊ฒŒ ๋‚˜์™”๋‹ค.

๋„ˆ๋ฌด ๋งŽ์€ ์ž๋ฃŒ๊ฐ€ ์žˆ์—ˆ์ง€๋งŒ, ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•œ ๊ณต๋ถ€๊ฐ€ ๋ถ€์กฑํ–ˆ๋˜ ๋‚˜๋Š” ํ•ด์‰ฌ๋ฅผ ์ œ์™ธํ•œ ๋Œ€๋ถ€๋ถ„์˜ ๊ตฌ์กฐ๋ฅผ ์ดํ•ดํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. 

 

 

 

์ผ๋‹จ ์ง€๊ธˆ๊นŒ์ง€ ์•Œ๊ฒŒ๋œ ๋ ˆ๋””์Šค์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด ๋ณด์ž๋ฉด, 

 

  • ์ธ๋ฉ”๋ชจ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋‹ค.
  • ์ž๋ฃŒ๋ฅผ ์ฐพ๋Š” ์†๋„๊ฐ€ ๋งค์šฐ ๋น ๋ฅด๋‹ค.
  • ์„œ๋ฒ„๋ฅผ ์žฌ์‹œ์ž‘ ํ•˜๊ฒŒ๋˜๋ฉด ๋ฐ์ดํ„ฐ ์œ ์‹ค . ๋”ฐ๋ผ์„œ ๋‚ ๋ผ๊ฐ€๋„ ์ƒ๊ด€ ์—†๋Š” ์• ๋“ค์„ ๋ณด๊ด€ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. 
  • ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ๋งŽ๋‹ค. hash, set, sorted set, list ๋“ฑ ์ด ๋ชจ๋“ ๊ฑธ ์ œ๊ณตํ•œ๋‹ค. 
  • ํ‚ค๋ฒจ๋ฅ˜ ๊ตฌ์กฐ๋ผ ์ฟผ๋ฆฌ๋ฌธ ํ•„์š” ์—†๋‹ค. 

 

๋”ฅํ•˜๊ฒŒ ๋“ค์–ด๊ฐ€๋ฉด ํ•œ๋„ ๋๋„ ์—†์ด ๊ฐˆ๊บผ ๊ฐ™์•„์„œ, ์ผ๋‹จ์€ ๊ตฌํ˜„์„ ์šฐ์„ ์œผ๋กœ ๋‘์—ˆ๋‹ค. 

์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•œ ๋ถ€๋ถ„์œผ๋ฅด ๊ณต๋ถ€ํ•ด์•ผ๊ฒ ๋‹ค๋Š” ํ•„์š”์„ฑ์„ ๋Š๊ผˆ๋‹ค. 

 

 

๋ ˆ๋””์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋จผ์ € ์„ค์น˜๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค. 

 

brew install redis
brew services start redis
redis-server -v # ๋ฒ„์ „ ํ™•์ธ

 

npm init -y
npm i redis -S # node.js redis client

 

 

 

๋ฉˆ์ถ”๊ฑฐ๋‚˜ ์žฌ์‹œ์ž‘ ํ• ๋•Œ๋Š” ์•„๋ž˜์— ๋ช…๋ น์–ด ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. 

brew services stop redis
brew services restart redis

 

 


ํ•‘ํ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ , 

import๋ฅผ ํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ค€๋‹ค. 

 

import redis from 'redis';
import RedisStore from 'connect-redis';
const redisClient = redis.createClient({
	url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`,
	password: process.env.REDIS_PASSWORD,
	lagacyMode: true
});

redisClient.connect();

 

 

๋ ˆ๋””์Šค ํ™ˆํŽ˜์ด์ง€์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” db ์ •๋ณด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ, 

ํ•ด๋‹น ์ •๋ณด๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ, ๋น„๋ฐ€๋ฒˆํ˜ธ, port, ์—”๋“œํฌ์ธํŠธ ๋ฅผ ๊ฐ€์ ธ์™€ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ ์„ค์ •ํ›„ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค. 

 

 

 

redis๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ช…๋ น์–ด ๋ช‡๊ฐœ๋งŒ ์•Œ์•„๋„ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์žˆ๋‹ค. 

 

//ํ† ํฐ ์‚ญ์ œ
await redisClient.del('refreshToken');

//ํ† ํฐ ์ƒ์„ฑ
await redisClient.set('refreshToken', getKakaoRefreshToken);

//ํ† ํฐ ๋งŒ๋ฃŒ๊ธฐํ•œ ์„ค์ •_ ์ดˆ๋‹จ์œ„๋กœ
await redisClient.expire('refreshToken', 60 * 60 * 24);

 

 

๋งŒ๋“  ๋ ˆ๋””์Šค ํ† ํฐ์€ ์•„๋ž˜ ์‚ฌ์ดํŠธ์—์„œ Redis Insight๋ฅผ ์„ค์น˜ํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

https://redis.com/redis-enterprise/redis-insight/

 

RedisInsight | The Best Redis GUI

RedisInsight provides an intuitive and efficient graphical interface for Redis, allowing you to interact with your databases and manage your data.

redis.com

 

 

 

 

 

๐Ÿซ   ๋Š๋‚€์ 

๋ ˆ๋””์Šค์— ๋Œ€ํ•ด ์•„๋ฌด๊ฒƒ๋„ ๋ชจ๋ฅด๋‹ค ๋ณด๋‹ˆ ์—ด์‹ฌํžˆ ์ฐพ์•„๋ณด์•˜๋Š”๋ฐ, 

๋„ˆ๋ฌด ๋งŽ์€ ์ •๋ณด๋“ค ์†์—

๋ชจ๋ฅด๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋“ค์ด ์™•์ฐฝ๋‚˜์˜ค๋‹ˆ๊นŒ ๋ง๊ทธ๋Œ€๋กœ ์นด์˜ค์Šค์˜€๋‹ค. 

๋ฉ˜๋ถ•์— ๋น ์ ธ์žˆ๋˜ ๋•Œ์— ๋‚ด๋ฐฐ์บ  ๋™๊ธฐ๋ถ„๊ป˜ ๋„์›€์„ ์š”์ฒญํ–ˆ๊ณ ,

๋‚ด๊ฐ€ ํ•˜๋ ค๊ณ  ํ•˜๋Š”๊ฑธ ๋˜‘๋ฐ”๋กœ ๋ณผ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ์…จ๋‹ค. 

๊ธฐ๋Šฅ ๊ตฌํ˜„. 

 

์•„์ง ๋งŽ์ด ๋ถ€์กฑํ•˜๊ธฐ๋•Œ๋ฌธ์— ํ•œ๋ฒˆ์— ์ด๋Ÿฐ ๋‚ด์šฉ๋“ค์„ ์ดํ•ดํ•  ์ˆ˜๋Š” ์—†์ง€๋งŒ, 

์ด๋ฒˆ์—๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด๋ณด์•˜์œผ๋‹ˆ, ๋‹ค์Œ์—๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•ด ๊ณต๋ถ€๋ฅผ ํ•˜๋ฉด์„œ ์ข€๋” ์ž˜ ์ดํ•ดํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค. 

 

 

 

 

 

์ฐธ๊ณ ํ•œ ์‚ฌ์ดํŠธ

https://velog.io/@inhwa1025/Redis-Redis%EB%9E%80-Redis%EC%9D%98-%EA%B0%9C%EB%85%90%EA%B3%BC-%ED%8A%B9%EC%A7%95

 

[Redis] Redis๋ž€? Redis์˜ ๊ฐœ๋…๊ณผ ํŠน์ง•

Redis : Remote Dictionary ServerKey-Value ๊ตฌ์กฐ์˜ ๋น„์ •ํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์˜คํ”ˆ์†Œ์Šค ๊ธฐ๋ฐ˜์˜ NoSQL DBMS์‚ฌ์šฉ์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ์‚ฌ์šฉ๋  ์ˆ˜๋„ ์žˆ๊ณ , Cache Server๋กœ ์‚ฌ์šฉ๋  ์ˆ˜๋„ ์žˆ์Œ๋ฐ์ดํ„ฐ๋ฒ 

velog.io

https://4sii.tistory.com/250

 

Redis

๊ฐœ์š” ์ฒ˜์Œ์œผ๋กœ ํ† ํฐ ๊ธฐ๋ฐ˜ ๋กœ๊ทธ์ธ ์ธ์ฆ์„ ๊ตฌํ˜„ํ•˜๋ฉฐ access token๊ณผ refresh token์„ ๋‹ค๋ฃจ๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ refresh token์„ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๊ทธ๋ƒฅ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋“ค ์ฒ˜๋Ÿผ MySQL์— Token ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด ์ €์žฅํ–ˆ์—ˆ๋‹ค.

4sii.tistory.com