์ถ์ฒ: https://inpa.tistory.com/entry/REDIS-๐-Redis๋ฅผ-ํด๋ผ์ฐ๋๋ก-์ฌ์ฉํ์-Redislabs [Inpa Dev ๐จ๐ป:ํฐ์คํ ๋ฆฌ]
[REDIS] ๐ Node.js ์์ redis ๋ชจ๋ ์ฌ์ฉ๋ฒ (์บ์ฑ & ์ธ์ ์คํ ์ด)
Node ํ๋ก์ ํธ์์ pm2๋ก ๋ค์ค ํด๋ฌ์คํฐ ์ธํ๋ผ๋ฅผ ๊ตฌ์ถํ๋ค๋ฉด ์ธ์ ๋ถ์ผ์น ๋ฌธ์ ๊ฐ ์๊ธฐ๊ฒ ๋ง๋ จ์ด๋ค. ๋ง์ผ ์๋ฒ๊ฐ ์ข ๋ฃ๋์ด ๋ฉ๋ชจ๋ฆฌ๊ฐ ๋ ๋ผ๊ฐ๋ฉด ์ ์์๋ค์ ๋ก๊ทธ์ธ์ด ๋ชจ๋ ํ๋ ค๋ฒ๋ฆฌ๊ฒ ๋๋ค. ๋ฐ๋ผ์
inpa.tistory.com
์ด ๋ถ์ ๊ธ์ ํ ๋๋ก ์์ฑํ๋ค. ์ด๋ถ์ ๊ธ์๋ redis ์ค์ ๋ฟ๋ง ์๋๋ผ ์บ์ฑ ์ ๋ต์ด๋ redis๋ก ์ธ์ ๊ด๋ฆฌ๋ฅผ ํ์ฌ ๋ณต์ ์๋ฒ ํ๊ฒฝ(ํด๋ฌ์คํฐ๋ง)์์๋ ์ธ์ ์ ๊ณต์ ํ ์ ์๋ ๋ฐฉ๋ฒ์ด ๋์์์ด ๋์์ด ๋ง์ด ๋๋ค!
1. Redis Labs ๋ก ํด๋ผ์ฐ๋ ์๋ฒ ๊ฐ์ค
MySQL์ ์ฌ์ฉํ๊ธฐ ์ํด์ ์ํฌ๋ฒค์น๋ฅผ ์ค์นํ๋ ๊ฒ ์ฒ๋ผ, ๋ ๋์ค๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ๋ ๋์ค ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ค์นํด์ผ ํ๋ค.
Redislabs์์๋ 1๊ฐ์ db์์ 30mb ๋ฅผ ๋ฌด๋ฃ ์ ๊ณตํด์ค๋ค. (ram ์์ญ์์ ํ ์คํธ๋ฅผ ์ํด์๋ ์ถฉ๋ถํ๋ค.)
Redis | The Real-time Data Platform
Developers love Redis. Unlock the full potential of the Redis database with Redis Enterprise and start building blazing fast apps.
redis.com
- ์ ์ํ์ฌ ํ์๊ฐ์
- ํด๋ผ์ฐ๋ ์ ์ฒด ์ ํ (์ง์ฅ ์์ผ๋ ์๋ฌด ๊ฒ์ด๋ ์ฒดํฌ )
- DB ์์ฑ ์๋ฃ
Mysql ์ ์ฐ๊ฒฐํ ๋์ ๊ฐ์ด, ๋ ๋์ค๋ ์์ฑ๋ ํด๋ผ์ฐ๋ db์๋ฒ์ ์ ์ํ๊ธฐ ์ํด์ Host, Port, Password 3๊ฐ์ง ์ ๋ณด๊ฐ ํ์ํ๋ค.


2. Redis Cloud ์ ์
์ป์ ์ ๋ณด๋ก redis์ ์ ์ํด๋ณด์.
Mac ๊ธฐ์ค
$brew install redis # apt-get install redis-tools , redis ์ค์น
$redis-cli-v #๋ฒ์ ํ์ธ
$redis-cli -h [endpoint] -p [port] -a [password] #redis cloud ์ ์
- redis ์๋ฒ ๋ช ๋ น์ด
$redis-server #์๋ฒ ์์
$redis-cli ping # ์๋ฒ ํ์ธ, ์คํ์ค์ด๋ผ๋ฉด PONG
$redis-cli shutdown #์๋ฒ ์ ์ง
$redis-cli # redis ๋ช
๋ น์ฐฝ์ผ๋ก ์ ์
$redis-cli shutdown
#cli์์ ์ ์ง
> shutdown save #์ง๊ธ๊น์ง ๋ด์ฉ ์ ์ฅ
> shutdown nosave #์ง๊ธ๊น์ง ๋ด์ฉ ์ ์ฅ X
3. Node์ ์ฐ๊ฒฐ
npm i redis #node ์ Redis ํจํค์ง ์ค์น
redis ํด๋ผ์ฐ๋์ ์ฐ๊ฒฐํ๊ธฐ
redis db์ ๊ณ์ ์ ๋ณด๋ค ์ ์ฅ ํ์ผ ์์ฑ(.env)
redisenv.env
# redisEnv.env ํ์ผ
REDIS_HOST=[endpoint]
REDIS_PORT=[port]
REDIS_USERNAME=[user]
REDIS_PASSWORD=[password]
redis.js
#with cloud redis server
const redis = require("redis");
const redisEnv= require('redisEnv')
redisEnv.config() //ํ๊ฒฝ๋ณ์ ํ์ผ ๊ฐ์ ธ์ค๊ธฐ
// redis[s]://[[username][:password]@][host][:port][/db-number]
const redisClient = redis.createClient({
url: `redis://${process.env.REDIS_USERNAME}:${process.env.REDIS_PASSWORD}@${process.env.REDIS_HOST}:${process.env.REDIS_PORT}/0`,
legacyMode: true, // ๋ฐ๋์ ์ค์ !!
});
redis ๋ก์ปฌ์ ์ฐ๊ฒฐํ๊ธฐ + ์บ์ ๋ฑ๋ก, ์ ๊ฑฐ, ์กฐํ
const redis = require("redis");
const bluebird = require("bluebird");
bluebird.promisifyAll(redis);
//redis ์ฐ๊ฒฐ, ๋ก์ปฌ์์
const redisClient = redis.createClient({
host: `127.0.0.1`,
port: 6379,
legacyMode: true, //๋ ๊ฑฐ์ ๋ชจ๋ ๋ฐ๋์ ์ค์
});
redisClient.on(`connect`, () => {
console.info(`Redis connected!`);
});
redisClient.on(`error`, (err) => {
console.error(`Redis Client Error`, err);
});
redisClient.connect().then();//redis v4 ์ฐ๊ฒฐ ๋น๋๊ธฐ
const redisCli = redisClient.v4; // ๊ธฐ๋ณธ redisClient ๊ฐ์ฒด๋ ์ฝ๋ฐฑ๊ธฐ๋ฐ์ธ๋ฐ v4๋ฒ์ ผ์ ํ๋ก๋ฏธ์ค ๊ธฐ๋ฐ์ด๋ผ ์ฌ์ฉ
const setCache = async (key, value) => {
await redisCli.set(key, JSON.stringify(value)); // key, value ํํ๋ก redis์ ์ ์ฅ
await redisCli.expire(key, 3600); //
};
const deleteCache = async (key) => {
await redisCli.delAsync(key); //๋ฐ์ดํฐ ์ ๊ฑฐ
res.status(200).json({
isSuccess: "all cache deleted",
});
};
// ์ฑ
๋ฆฌ์คํธ ๊ฒฐ๊ณผ ์บ์์ ์ ์ฅํ๊ธฐ
const getBooksCache = async (req, res, next) => {
let key = "book-list/";
try {
const data = JSON.parse(await redisCli.get(key));
if (data) {
if (redisCli.ttl(key) < 1000) { // ttl ํ์์์๊น์ง ๋จ์ ์๊ฐ์ ์ด๋จ์๋ก ๋ฐํ
// ์๋ฉธ ์๊ฐ ์ฐ์ฅํ๊ธฐ
console.log("redis work1");
await redisCli.setex(key, 3600, data); //์๋ฉธ์๊ฐ ์ง์ ํด์ ์ ์ฅํ๊ธฐ , 3600์ด
console.log("redis work2");
}
res.status(200).json(
//๊ฒฐ๊ณผ ์ ์ก
data
);
} else {
next(); //๋ค์ ํจ์ ์คํ (์ปจํธ๋กค๋ฌ์ getBookList ํจ์ ์คํ, ์บ์ ์์ด ๊ฐ์ ธ์ค๋ ๊ฒฝ์ฐ(ํค ๊ฐ ์กด์ฌX) )
}
} catch (err) {
res.status(400).json({
ok: false,
message: err,
});
}
};
module.exports = {
setCache,
getBooksCache,
deleteCache,
};'[Server]' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| JavaScript (0) | 2023.07.05 |
|---|---|
| [Server] ec2 vscode ssh : Operation timed out, Permission denied (PublicKey) ์ค๋ฅ ํด๊ฒฐ (0) | 2023.04.13 |
| [Node] ๋ค์ค ํ์ผ ์ฌ๋ฆฌ๊ธฐ (0) | 2022.09.23 |
| ์คํ๋ง์ ๋ฌธ - ๋ฐ์ดํฐ ์กฐํํ๊ธฐ with JPA, ํ์ด์ง ์ด๋ํ๊ธฐ, ์์ , ์ญ์ (0) | 2022.09.08 |
| [server] Docker ๋ก Node.JS & Nginx๊ฐ๋ฐํ๊ฒฝ ๊ตฌ์ถ (0) | 2022.07.03 |